Files
qt5-legatus-legacy/Lts/Tools/ProjectSettingsParser/project_settings.py
T
2026-08-13 18:33:10 +02:00

196 lines
6.9 KiB
Python
Executable File

#! /usr/bin/env python3
import argparse
import os
import sys
import json
import csv
def _vlog(text):
if verbose:
print(text)
def _add_translation(key):
value = input('translate "' + key + '" to: ')
if len(value) == 0:
print(' --> skipped')
return key
translation_dictionay.update({ key : value })
with open(translation_file, 'w') as tf:
json.dump(translation_dictionay, tf, sort_keys=True, indent=4)
return value
def _translated(t):
if t in translation_dictionay:
return translation_dictionay[t]
elif translation_input == True:
return _add_translation(t)
else:
return t
def _read_translation_file(translation_file):
if os.path.exists(translation_file):
with open(translation_file, 'r') as tf:
return json.load(tf)
else:
return {}
def _get_setting_value(project_name, setting_name, project_settings):
single_project_settings = project_settings[project_name]
for family_name, settings in single_project_settings.items():
for setting, value in settings.items():
if setting == setting_name:
if type(value) is type(''):
return value;
else:
if value == True:
return 'x'
else:
return ''
return ''
def _get_project_name(settings_file):
l = settings_file.split('/')
return l[len(l) - 3]
def _write_settings_to_csv(project_settings):
setting_list = []
field_names = []
for project_name, settings_family in project_settings.items():
field_names.append(project_name)
for family_name, settings in settings_family.items():
for setting, value in settings.items():
if not setting in setting_list:
setting_list.append(setting)
field_names.sort()
field_names.insert(0, 'Setting')
_vlog(str(len(setting_list)) + ' different settings in total')
with open(output_file, mode='w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=field_names, delimiter=';')
writer.writeheader()
rows = []
for setting in setting_list:
row = {}
for field_name in field_names:
if field_name == field_names[0]:
row.update({field_name : _translated(setting)})
else:
row.update({field_name : _get_setting_value(field_name, setting, project_settings)})
rows.append(row)
rows.sort(key = lambda row : row['Setting'].upper())
for row in rows:
writer.writerow(row)
def _parse_settings_file(settings_file):
project_name = _get_project_name(settings_file)
_vlog('#####################################################')
_vlog('# Parsing settings file for project ' + project_name)
_vlog('#####################################################')
sf = open(settings_file, 'r')
project_settings = json.load(sf)['ProjectSettings']
sf.close()
ignore_list = [
'nagios',
'sps',
'recording',
'EquipmentLanguages',
'egcuUsesOnlyOneRelay',
'useExactPositioning'
]
settings = {} # [setting_family][setting][value]
for setting_family in project_settings:
_vlog('Found setting family "' + setting_family + '"')
if setting_family == 'Functionalities':
settings[setting_family] = {}
for setting in project_settings[setting_family]['IdentifierList']['Default']:
settings[setting_family][setting] = True
elif setting_family == 'SituationMap':
continue
elif setting_family == 'Office':
settings[setting_family] = {'Office' : project_settings[setting_family]['Type']['Default']}
else:
if not setting_family in settings:
settings[setting_family] = {}
for setting in project_settings[setting_family]:
_vlog(' - setting ' + setting)
if setting in ignore_list:
_vlog(' --> ignored')
continue
if setting.startswith('enable') or setting == 'SystemLanguages':
value = project_settings[setting_family][setting]['Default']
settings[setting_family][setting] = value
_vlog(' --> stored with value "' + str(value) + '"')
else:
print(_get_project_name(settings_file) + ' > ' + setting_family + ' > ' + setting + ' is not treated!!!')
return { project_name : settings }
def _find_settings(directory):
setting_files = []
entries = os.walk(directory)
for root, dirs, files in os.walk(directory, topdown=True):
for name in files:
if name == 'project_settings.json':
settings_file = os.path.join(root, name)
setting_files.append(settings_file)
_vlog('Found file: "' + settings_file + '"')
return setting_files
def _parse_args():
parser = argparse.ArgumentParser(description='Starts searching recursivle for "project_settings.json" files in a given directory and converts them to CSV format.')
parser.add_argument('-d', '--directory', default='.', help='starts searching in the given directory (default: ".", best value is "...<source-dir>/Lts/Projects/")', type=str)
parser.add_argument('-v', '--verbose', help='verbose logging', action='store_true', dest='verbose')
parser.add_argument('-t', '--translation_file', default='./project_settings_translation.json', help='file for translation of setting names (default: "./project_settings_translation.json")', type=str)
parser.add_argument('-i', '--translation_input', help='ask for terms which are not translated', action='store_true', dest='translation_input')
parser.add_argument('-o', '--output_file', default='./project_settings_overview.csv', help='output file (default: "./project_settings_overview.csv")', type=str)
return parser.parse_args()
def _main():
args = _parse_args()
global verbose
global translation_dictionay
global translation_input
global translation_file
global output_file
verbose = args.verbose
translation_input = args.translation_input
translation_file = args.translation_file
translation_dictionay = _read_translation_file(translation_file)
output_file = args.output_file
if not output_file.endswith('.csv'):
output_file = output_file + '.csv'
if not os.path.isdir(args.directory):
print('ERROR: "' + args.directory + '" is not a valid directory. Aborting.')
exit()
project_settings = {}
for settings_file in _find_settings(args.directory):
project_settings.update(_parse_settings_file(settings_file))
_write_settings_to_csv(project_settings)
if __name__ == '__main__':
sys.exit(_main())