2024-07-25 12:31:32 +00:00
|
|
|
class_name ClassFunctions extends Control
|
|
|
|
|
|
|
|
# This should be looked at again when GoDot 4.3 ships as has new OS.execute_with_pipe
|
|
|
|
|
|
|
|
func array_to_string(arr: Array) -> String:
|
2024-07-31 19:45:58 +00:00
|
|
|
var text: String
|
2024-07-25 12:31:32 +00:00
|
|
|
for line in arr:
|
2024-07-31 19:45:58 +00:00
|
|
|
#text += line + "\n"
|
|
|
|
text = line.strip_edges() + "\n"
|
2024-07-25 12:31:32 +00:00
|
|
|
return text
|
|
|
|
|
|
|
|
func execute_command(command: String, parameters: Array, console: bool) -> Dictionary:
|
|
|
|
var result = {}
|
|
|
|
var output = []
|
2024-07-28 15:09:37 +00:00
|
|
|
var exit_code = OS.execute(command, parameters, output, console) ## add if exit == 0 etc
|
2024-07-25 12:31:32 +00:00
|
|
|
result["output"] = array_to_string(output)
|
|
|
|
result["exit_code"] = exit_code
|
2024-07-31 19:45:58 +00:00
|
|
|
#print (array_to_string(output))
|
|
|
|
#print (result["exit_code"])
|
2024-07-25 12:31:32 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
func run_command_in_thread(command: String, paramaters: Array, _console: bool) -> Dictionary:
|
|
|
|
var thread = Thread.new()
|
|
|
|
thread.start(execute_command.bind(command,paramaters,false))
|
|
|
|
while thread.is_alive():
|
|
|
|
await get_tree().process_frame
|
|
|
|
var result = thread.wait_to_finish()
|
|
|
|
thread = null
|
|
|
|
return result
|
|
|
|
|
|
|
|
func _threaded_command_execution(command: String, parameters: Array, console: bool) -> Dictionary:
|
|
|
|
var result = execute_command(command, parameters, console)
|
|
|
|
return result
|
|
|
|
|
|
|
|
# Make this generic for command, path and naming
|
2024-07-31 19:45:58 +00:00
|
|
|
func get_text_file_from_system_path(file_path: String, command: String, etype: String) -> Dictionary:
|
|
|
|
var output: Array
|
|
|
|
var data_dict: Dictionary
|
2024-07-25 12:31:32 +00:00
|
|
|
command += file_path
|
|
|
|
var exit_code = OS.execute("sh", ["-c", command], output)
|
2024-07-31 19:45:58 +00:00
|
|
|
if exit_code == 0:
|
2024-07-25 12:31:32 +00:00
|
|
|
var content = array_to_string(output)
|
2024-07-31 19:45:58 +00:00
|
|
|
if etype == "emulist":
|
|
|
|
data_dict = parse_file_list(content)
|
|
|
|
elif etype == "normal":
|
|
|
|
data_dict = parse_imported_string(content)
|
|
|
|
else:
|
|
|
|
print ("Error in get text function")
|
|
|
|
return data_dict
|
2024-07-25 12:31:32 +00:00
|
|
|
else:
|
|
|
|
print("Error reading file: ", exit_code)
|
|
|
|
return {}
|
2024-07-31 19:45:58 +00:00
|
|
|
|
|
|
|
func parse_imported_string(input_string: String) -> Dictionary:
|
2024-08-01 10:47:55 +00:00
|
|
|
var _result: Dictionary
|
|
|
|
var _current_dict_key: String
|
2024-07-31 19:45:58 +00:00
|
|
|
var lines = input_string.strip_edges().split("\n", false)
|
2024-08-01 10:47:55 +00:00
|
|
|
#if lines.size() > 0:
|
|
|
|
# lines = lines.slice(1, lines.size()) # Skip the first line
|
2024-07-31 19:45:58 +00:00
|
|
|
for line in lines:
|
2024-08-01 10:47:55 +00:00
|
|
|
_current_dict_key = line
|
2024-07-31 19:45:58 +00:00
|
|
|
var parts = line.split("=", false)
|
|
|
|
if parts.size() == 2:
|
|
|
|
var key = parts[0]
|
|
|
|
var value_str = parts[1]
|
2024-08-01 10:47:55 +00:00
|
|
|
_result[key] = {"KEY": key, "Value": value_str}
|
|
|
|
return _result
|
2024-07-31 19:45:58 +00:00
|
|
|
|
2024-07-25 12:31:32 +00:00
|
|
|
func parse_file_list(content: String) -> Dictionary:
|
|
|
|
var file_dict = {}
|
|
|
|
var regex = RegEx.new()
|
|
|
|
regex.compile(r'"([^"]+)"\s*"([^"]+)"')
|
|
|
|
var matches = regex.search_all(content)
|
|
|
|
|
|
|
|
for match in matches:
|
|
|
|
var name = match.get_string(1)
|
|
|
|
var description = match.get_string(2)
|
|
|
|
file_dict[name] = description
|
|
|
|
return file_dict
|
|
|
|
|
|
|
|
func process_url_image(body) -> Texture:
|
|
|
|
var image = Image.new()
|
|
|
|
image.load_png_from_buffer(body)
|
|
|
|
var texture = ImageTexture.create_from_image(image)
|
|
|
|
return texture
|
|
|
|
|
|
|
|
func launch_help(url: String) -> void:
|
|
|
|
OS.shell_open(url)
|
2024-07-31 13:57:37 +00:00
|
|
|
|
|
|
|
func import_csv_data(file_path: String) -> Dictionary:
|
|
|
|
# check if file exists
|
2024-07-31 19:45:58 +00:00
|
|
|
var data_dict: Dictionary
|
2024-07-31 13:57:37 +00:00
|
|
|
var file = FileAccess.open(file_path,FileAccess.READ)
|
|
|
|
if file:
|
|
|
|
var csv_lines: PackedStringArray = file.get_as_text().strip_edges().split("\n")
|
|
|
|
for i in range(1, csv_lines.size()): # Start from 1 to skip the header
|
|
|
|
var line = csv_lines[i]
|
|
|
|
var columns = line.split(",")
|
2024-07-31 19:45:58 +00:00
|
|
|
if columns.size() >= 3:
|
2024-07-31 13:57:37 +00:00
|
|
|
var id = columns[0]
|
|
|
|
var url = columns[1]
|
|
|
|
var description = columns[2]
|
|
|
|
data_dict[id] = {"URL": url, "Description": description}
|
|
|
|
file.close()
|
|
|
|
else:
|
|
|
|
print ("Could not open file: %s", file_path)
|
|
|
|
|
|
|
|
return data_dict
|
|
|
|
|
|
|
|
func _import_data_lists(file_path: String) -> void:
|
|
|
|
var tk_about: Dictionary = import_csv_data(file_path)
|
|
|
|
|
|
|
|
for key in tk_about.keys():
|
|
|
|
var entry = tk_about[key]
|
|
|
|
print("ID: " + key)
|
|
|
|
print("URL: " + entry["URL"])
|
|
|
|
print("Description: " + entry["Description"])
|
|
|
|
print("---")
|