Package: confj
ConfJPackage is used to load config from json files. Path to config can lead either
to directory with a bunch of json files, or to a single json file. Contents is
parsed and stored into config object, allowing attribute-based access to
different options.InstallationPackage can be installed with pip:pipinstallconfjUsageUsage of this utility is very simple: importConfigclass and load your config
withloadmethod. Path to configs might be set via environment variableJSON_CONFIG_PATHor passed directly to theloadcall:fromconfjimportConfigconfig=Config()config.load('/path/to/config')If path to config is a directory, each file will be available as an attribute
ofconfigobject. Otherwise, if path is a file,configobject will store
parsed json from given file, allowing access to data via attributes.Config can be loaded during initialization step, just setautoloadparameter
toTrue. Optionally path to load configs from might be passed during
initialization.fromconfjimportConfigconfig=Config(default_config_path='/path/to/config',autoload=True)After config is loaded, it's options are accessible via attributes ofconfigobject, otdict-like syntax:>>>config.username'user'>>>config['username']'user'>>>config.get('username')'user'Config search precedenceIf you directly callloadmethod withconfig_pathpassed as parameter,
then this path is used.If noconfig_pathprovided orautoloadoption was used,
thendefault_config_pathfrom initialization step is used.The last option is to setJSON_CONFIG_PATHenvironment value. If the search
is failed on all three steps, thenConfigExceptionis raised.Load from python objectConfig may be loaded from python object:fromconfjimportConfigconfig=Config()data={'username':'user','password':'pass'}config.load_from_obj(data)assertconfig.username=='username'assertconfig.password=='pass'Set/change config dataConfig data may be changed or added to current config withsetmethod:fromconfjimportConfigconfig=Config()data={'username':'user','password':'pass'}config.load_from_obj(data)config.set('connection',{'port':5432,'host':'localhost'})assertconfig.connetcion.port==5432assertconfig.connection.host=='localhost'Available methodsAllconfigobject's method names are starting withc_, to avoid possible
clash with possible config optionsc_formatreturns pretty formatted sting representing config;c_pprintoutputs formatted config to stdout;c_validatevalidates config against given json schema. SeeValidationsection below.ValidationConfig must be a valid json object, so we can validate it against provided
json schema. To use config validationjsonschemapackage must be installed.
It might be done as a separate step aspipinstalljsonschemaor duringconfjinstallation time:pipinstallconfj[validation]Then config validation can be done withc_validatemethod:fromconfjimportConfigconfig=Config(default_config_path='/path/to/config',autoload=True)schema={"type":"object"}is_valid=config.c_validate(schema)By defaultc_validatewill return eitherTrueofFalse. To see actual
validation error just passdo_raise=Trueas additional parameter, and catchValidationErrorlater:fromconfjimportConfigfromjsonschemaimportValidationErrorconfig=Config(default_config_path='/path/to/config',autoload=True)schema={"type":"object"}try:config.c_validate(schema,do_raise=True)exceptValidationErroraserr:print(f'Looks like config is invalid:{err}')To read more on json validation one can atjson-schema.org