""" JSON """ import argparse import json import os from qtpy.QtCore import QFile, QTemporaryDir class ReadJSON(object): """ Reads three json configuration files. 1) The style guide and default menu configuration 2) The accelerator facility configuration file 3) The user-specified configuration file """ def __init__(self, appname: str = ""): self.appname = 'user' if appname == "" else appname json_qrc = ["style", "base", self.appname] parser = argparse.ArgumentParser(description="configuration") parser.add_argument("-s", "--style", action="store", dest="style") parser.add_argument("-f", "--facility", action="store", dest="facility") parser.add_argument("-u", "--user", action="store", dest="user") parser.add_argument("-q", "--qss", action="store") result = parser.parse_args() temp_file = {} self.data = {} #Extract style and facility config files from qrc_resources temp_dir = QTemporaryDir() if temp_dir.isValid(): for dest in json_qrc[0:2]: temp_file[dest] = temp_dir.path() + "/" + dest + ".json" if QFile.copy(":/{0}.json".format(dest), temp_file[dest]): print(":/{0}.json successfully copied from qrc_resources". format(dest)) else: print("json file :/{0}.json was not found in qrc_resources". format(dest)) else: print("Invalid temporary directory: {0}".format(temp_dir)) #Overriding config files in qrc_resources with any supplied by user if result.style: if os.path.isfile(result.style): temp_file[json_qrc[0]] = result.style print("Config file {0} supplied by user".format( temp_file[json_qrc[0]])) else: print("User supplied config file {0} does not exist".format( result.style)) if result.facility: if os.path.isfile(result.facility): temp_file[json_qrc[1]] = result.facility print("Config file {0} supplied by user".format( temp_file[json_qrc[1]])) else: print("User supplied config file {0} does not exist".format( result.facility)) if result.user: if os.path.isfile(result.user): temp_file[json_qrc[2]] = result.user print("Config file {0} supplied by user".format( temp_file[json_qrc[2]])) else: print("User supplied config file {0} does not exist".format( result.user)) #Now open default config files try: with open(temp_file[json_qrc[0]]) as data_file: self.data = json.load(data_file) except IOError: print("Config file: {0} not found!".format( temp_file[json_qrc[0]])) try: with open(temp_file[json_qrc[1]]) as data_file: print(temp_file[json_qrc[1]]) self.data_facility = json.load(data_file) for key, val in self.data_facility.items(): if key in self.data.keys(): if hasattr(val, 'items'): for k, v in val.items(): #print(key, k, v, type(k), type(v)) self.data[key][k] = v else: self.data[key] = val #print(key, val, "////") else: self.data[key] = val #print(key, self.data[key]) except IOError: print("Config file: {0} not found".format( temp_file[json_qrc[1]])) #Now open user supplied config files json_file = self.appname + ".json" if result.user is not None: if os.path.isfile(result.user): json_file = result.user print("Local user config file: {0} found".format(json_file)) else: print("Local user config file {0} not found!".format(json_file)) print("Searching local directory for user config file {0}". format(json_file)) else: print("Searching local directory for user config file {0}". format(json_file)) try: with open(json_file) as data_file: data_user = json.load(data_file) for key, val in data_user.items(): #self.data[key] = val #print("======", key, val, type(key), type(val)) if key in self.data.keys(): if hasattr(val, 'items'): for k, v in val.items(): #print(key, k, v, type(k), type(v)) self.data[key][k] = v else: self.data[key] = val #print(key, val, "////") else: self.data[key] = val except IOError: print("User {0} config file not found!" .format(json_file)) #self.show() def show(self): """ print key values of jason file """ for key in self.data: print(str(key) + ': ' + str(self.data[key]))