forked from nbfc-linux/nbfc-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_to_json.py
executable file
·52 lines (35 loc) · 1.63 KB
/
config_to_json.py
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
#!/usr/bin/python3 -B
import sys, os, json, config, argparse
argp = argparse.ArgumentParser()
argp.add_argument('--out-dir', default='.')
argp.add_argument('infile', metavar='INPUT FILE', nargs='+')
opts = argp.parse_args()
for infile in opts.infile:
try:
_, basename = os.path.split(infile)
if basename.lower().endswith('.json'):
continue
if not basename.lower().endswith('.xml'):
raise Exception('Not an xml file')
r = config.parse_xml_file(infile)
s = json.dumps(r, default=lambda o: o.to_json(), indent=1)
# reconfigure temperature thresholds to account for the new logic:
p = json.loads(s)
for fan_configuration in p['FanConfigurations']:
thresholds = fan_configuration.get('TemperatureThresholds')
if thresholds:
thresholds.sort(key=lambda x: x['UpThreshold'])
for i in range(len(thresholds)-1):
thresholds[i]['UpThreshold'] = thresholds[i+1]['UpThreshold']
thresholds[-1]['UpThreshold'] = p['CriticalTemperature']
#if thresholds[0]['UpThreshold'] == 0:
# raise Exception('UpThreshold of 0 detected.')
fan_configuration['TemperatureThresholds'] = thresholds
s = json.dumps(p, indent=1)
s = s.replace('\n ', '\n\t')
json_file = os.path.join(opts.out_dir, basename[0:-4] + '.json')
#print(infile, '->', json_file, file=sys.stderr)
with open(json_file, 'w') as fh:
fh.write(s)
except Exception as e:
print('Error:', infile, e, file=sys.stderr)