-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconfigmerger.py
More file actions
executable file
·86 lines (72 loc) · 2.95 KB
/
Copy pathconfigmerger.py
File metadata and controls
executable file
·86 lines (72 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import re
from config_paths import CONFIG_PATH
def update_config(main_config_file, custom_config_file):
# Read the custom configuration into a dictionary
custom_config = {}
with open(custom_config_file, 'r') as file:
for line in file:
if "=" in line and not line.startswith("#"):
key, value = line.split('=', 1)
custom_config[key.strip()] = value.strip()
# Update the main configuration file
updated_lines = []
updated_keys = []
with open(main_config_file, 'r') as file:
for line in file:
if "=" in line and not line.startswith("#"):
key = line.split('=', 1)[0].strip()
if key in custom_config:
line = f"{key} = {custom_config[key]}\n"
updated_keys.append(key)
updated_lines.append(line)
# Write the updated lines back to the main config file
with open(main_config_file, 'w') as file:
file.writelines(updated_lines)
# Inform user about the updated keys
if updated_keys:
print("The following parameters have been updated:")
for key in updated_keys:
print(f"- {key}")
else:
print("No parameters were updated.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: configmerger.py <custom_config_file>")
sys.exit(1)
main_config_file = CONFIG_PATH
custom_config_file = sys.argv[1]
update_config(main_config_file, custom_config_file)
print(f"Configuration from {custom_config_file} has been merged into {main_config_file}.")
# ---
# # // (old method)
# import sys
# import re
# def update_config(main_config_file, custom_config_file):
# # Read the custom configuration into a dictionary
# custom_config = {}
# with open(custom_config_file, 'r') as file:
# for line in file:
# if "=" in line and not line.startswith("#"):
# key, value = line.split('=', 1)
# custom_config[key.strip()] = value.strip()
# # Update the main configuration file
# updated_lines = []
# with open(main_config_file, 'r') as file:
# for line in file:
# if "=" in line and not line.startswith("#"):
# key = line.split('=', 1)[0].strip()
# if key in custom_config:
# line = f"{key} = {custom_config[key]}\n"
# updated_lines.append(line)
# # Write the updated lines back to the main config file
# with open(main_config_file, 'w') as file:
# file.writelines(updated_lines)
# if __name__ == "__main__":
# if len(sys.argv) != 3:
# print("Usage: configmerger.py <main_config_file> <custom_config_file>")
# sys.exit(1)
# main_config_file = sys.argv[1]
# custom_config_file = sys.argv[2]
# update_config(main_config_file, custom_config_file)
# print(f"Configuration from {custom_config_file} has been merged into {main_config_file}.")