Package: yet-another-json-config
yet-another-json-configPython class to reads and write json files as a configuration file, supports nested json values.ExamplesInitializingConfigClassfromyet_another_json_configimportConfigc=Config('./tests/test.json')Listing Settingsfromyet_another_json_configimportConfigc=Config('./tests/test.json')print(c.settings)Output{"test":"test","varUserTest":"test_$user$","nestedTest":{"test":"test"},"nestedTest2":{"nested":{"test":"test"}},"get":"test"}Get SettingSettings can be obtained two ways through the methodgetClass Methodget# setting a basic settingc.get('test')# setting a nested setting via list of stringsc.get('nestedTest2','nested','test')# setting a nested setting via tuplec.get(('nestedTest2','nested','test'))keyMethod# setting a basic settingprint(c['test'])# setting a nested settingprint(c['nestedTest2']['nested']['test'])Set SettingSettings can be both created and modified using the same methods. If a setting does not exist and one of the following methods is used, it will be created. If the setting already exists, it will be updated.Class MethodsetThis method supports *args for defining the keys for a setting. This means that a list of strings (not type of list) or a tuple can be passed to set the value.# setting a basic settingc.set('test',value='test2')# setting a nested setting via list of stringsc.set('nestedTest2','nested','test',value='test2')# setting a nested setting via tuplec.set(('nestedTest2','nested','test'),value='test2')keyMethod# setting a basic settingc['test']='test2'# setting a nested settingc['nestedTest2']['nested']['test']='test2'Delete SettingSettings can be deleted two ways through the methoddeleteand thedelstatement. As well, nested settings can also be deleted. Below are an example of each.Class Methoddelete# deleting a basic settingc.delete('test')# deleting a nested settingc.delete(('nestedTest2','nested','test'))delStatement Method# deleting a basic settingdelc['test']# deleting a nested settingdelc['nestedTest2']['nested']['test']Custom Config ClassBelow is an example of a custom config class that is derived off theConfigclass. In this example it allows for the variable$user$to be replaced at run time with the user id that is currently running the code. This could be expanded further as well as potential validation of the configuration file after loading via thevalidatemethod.importgetpassfromyet_another_json_configimportConfigclassCustomConfig(Config):def_load(self):super()._load()user=getpass.getuser()if'varUserTest'inself._settings:# replace special character in filename with the usernameself.set('varUserTest',value=self.get('varUserTest').replace('$user$',user))print(self._settings)self.validate()defvalidate(self):passconf=CustomConfig('./tests/test.json')print(conf.settings())API ReferenceclassConfig(config_file_path,file_must_exist=False)Create an instance of a configuration file.Argumentsconfig_file_path(str) - The path to the configuration file.file_must_exist(bool, Optional) - Raises aFileNotFoundErrorexception if file does not exist. Default value isFalse.get(*keys:str)Returns the value of the keys specified.*keys- (str) - List ofstror Tuple ofstrtraversing the settings and return a value if the setting exists. If the setting does not exist, aKeyErrorexception is raised.set(*keys:str,value)Sets the keys to the value specified.*keys- (str) - List ofstror Tuple ofstrtraversing the settings to set the specified key to the specified value. If the setting does not exist, the setting is created.settings()Returns all settings in the configuration.