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