11 alg_file = 'alg_sequence.json', output_file = 'my_analysis_config.json'):
12 """
13 Parse a tool configuration text file and merge it with an algorithm sequence JSON file.
14
15 Args:
16 combine_dictionaries (bool): Whether to load and merge with existing alg_file
17 text_file (str): Path to the tool configuration text file
18 alg_file (str): Path to the algorithm sequence JSON file
19 output_file (str): Path where the merged configuration will be saved
20
21 Returns:
22 dict: The resulting configuration, which is a merge of the tool configuration
23 and algorithm sequence JSON data.
24 """
25 log = logging.getLogger("SaveConfigUtils")
26 log.info("Calling COMBINE_JSON_FILES with the following arguments:")
27 log.info(f" - tool configuration = {text_file}")
28 log.info(f" - algorithm sequence = {alg_file}")
29 log.info(f" - full output config = {output_file}")
30
31 if not os.path.exists(text_file):
32 raise FileNotFoundError(f"The tool configuration file {text_file} was not found.")
33 if not os.path.exists(alg_file):
34 raise FileNotFoundError(f"The algorithm sequence file {alg_file} was not found.")
35
36
37 config = {}
38 if combine_dictionaries:
39 with open(alg_file, 'r', encoding='utf-8') as f:
40 try:
41 config = json.load(f)
42 except json.JSONDecodeError as e:
43 raise ValueError(f"Error parsing JSON in {alg_file}: {e}")
44
45
46 current_tool_path = None
47 current_tool_id = None
48 current_tool_properties = {}
49
50 with open(text_file, 'r') as f:
51 for line in f:
52 line = line.strip()
53 if not line or '=' not in line:
54 continue
55
56 path, value =
map(str.strip, line.split(
'=', 1))
57 parts = path.split('.')
58
59
60
61
62 if len(value.split("/")) == 2 and "'" not in value and current_tool_path != parts:
63
64
65 _register_settings(config, current_tool_path, current_tool_id, current_tool_properties)
66
67 tool_path = parts[:-1]
68 tool_id = parts[-1]
69 tool_name = value.split("/")[1]
70 _register_new_tool(config, tool_path, tool_id, tool_name)
71
72 current_tool_path = tool_path
73 current_tool_id = tool_id
74 current_tool_properties = {}
75 else:
76
77 property_name = parts[-1]
78 current_tool_properties[property_name] = value
79
80
81 if current_tool_properties and current_tool_id:
82 _register_settings(config, current_tool_path, current_tool_id, current_tool_properties)
83
84
85 with open(output_file, 'w', encoding='utf-8') as f:
86 json.dump(config, f, indent=4)
87
88 return config
89
90