Python class to reads and write json files as a configuration file, supports nested json values.
from yet_another_json_config import Config
c = Config('./tests/test.json')
from yet_another_json_config import Config
c = Config('./tests/test.json')
print(c.settings)
{
"test": "test",
"varUserTest": "test_$user$",
"nestedTest": {
"test": "test"
},
"nestedTest2": {
"nested": {
"test": "test"
}
},
"get": "test"
}
Settings can be obtained two ways through the method get
# setting a basic setting
c.get('test')
# setting a nested setting via list of strings
c.get('nestedTest2', 'nested', 'test')
# setting a nested setting via tuple
c.get(('nestedTest2', 'nested', 'test'))
# setting a basic setting
print(c['test'])
# setting a nested setting
print(c['nestedTest2']['nested']['test'])
Settings 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.
This 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 setting
c.set('test', value='test2')
# setting a nested setting via list of strings
c.set('nestedTest2', 'nested', 'test', value='test2')
# setting a nested setting via tuple
c.set(('nestedTest2', 'nested', 'test'), value='test2')
# setting a basic setting
c['test'] = 'test2'
# setting a nested setting
c['nestedTest2']['nested']['test'] = 'test2'
Settings can be deleted two ways through the method delete
and the del
statement. As well, nested settings can also be deleted. Below are an example of each.
# deleting a basic setting
c.delete('test')
# deleting a nested setting
c.delete(('nestedTest2', 'nested', 'test'))
# deleting a basic setting
del c['test']
# deleting a nested setting
del c['nestedTest2']['nested']['test']
Below is an example of a custom config class that is derived off the Config
class. 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 the validate
method.
import getpass
from yet_another_json_config import Config
class CustomConfig(Config):
def _load(self):
super()._load()
user = getpass.getuser()
if 'varUserTest' in self._settings:
# replace special character in filename with the username
self.set('varUserTest', value=self.get('varUserTest').replace('$user$', user))
print(self._settings)
self.validate()
def validate(self):
pass
conf = CustomConfig('./tests/test.json')
print(conf.settings())
class Config(config_file_path, file_must_exist=False)
Create an instance of a configuration file.
-
config_file_path
(str
) - The path to the configuration file. -
file_must_exist
(bool, Optional
) - Raises aFileNotFoundError
exception if file does not exist. Default value isFalse
.
get(*keys: str)
Returns the value of the keys specified.
-
*keys
- (str
) - List ofstr
or Tuple ofstr
traversing the settings and return a value if the setting exists. If the setting does not exist, aKeyError
exception is raised.
set(*keys: str, value)
Sets the keys to the value specified.
-
*keys
- (str
) - List ofstr
or Tuple ofstr
traversing 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.