Package: zycelium.dataconfig
zycelium.dataconfigCreatedataclassesbacked by configuration files.Development Status :: 3 - AlphaUsageUse defaults:Create a new python script and name itexample.pyfromzycelium.dataconfigimportdataconfig@dataconfigclassConfig:name:str="World"config=Config().load()print(f"Hello,{config.name}!")Create aconfig.inifile in the same directory asexample.pyname="DataConfig"Finally, from the same directory, runpython example.py,
your console session should look something like this:$pythonexample.pyHello, DataConfig!The defaults here are:Config file name:"config.ini"Paths to look for the config file (current working directory):["."]Specify file-name for configuration:fromzycelium.dataconfigimportdataconfig@dataconfig(file="custom_config.ini")classConfig:name:str="World"config=Config().load()print(f"Hello,{config.name}!")In this example, we specify the file-name on this line:@dataconfig(file="custom_config.ini")with keyword argumentsfile="custom_config.ini"passed to@dataconfig().Specify file-lookup-paths:fromzycelium.dataconfigimportdataconfig@dataconfig(paths=[".","examples","/usr/local/etc"])classConfig:name:str="World"config=Config().load()print(f"Hello,{config.name}!")Here, we passpaths=[".", "examples"]to@dataconfig()to specify the paths on filesystem wheredataconfigshould
look for the default"config.ini"file. We can also specify
the filename along with the paths. Paths can be relative
to current working directory or absolute.Save configuration to file:fromzycelium.dataconfigimportdataconfigFILE_NAME="newconfig.ini"@dataconfig(file=FILE_NAME)classConfig:name:str="World"config=Config()config.save()print(f"Saved config to file:{FILE_NAME}.")Here, we set the config-file-name while creating the class,
whensave()is called, it will create the file and save
contents ofConfig.If we try running the same example again, we will get an error:FileExistsError: File newconfig.ini exists, refusing to overwrite.This is to protect us from accidentally overwriting an existing config file.
To overwrite it, passoverwrite=Truetosave()like this:config.save(overwrite=True)Frozen configuration:fromzycelium.dataconfigimportdataconfig@dataconfig(frozen=True)classConfig:name:str="World"config=Config().load(replace=True)print(f"Hello,{config.name}!")To load a frozen config, we need to passreplace=Truetoload(),
if we forget, we get the error:dataclasses.FrozenInstanceError: cannot assign to field 'name'Once loaded, we cannot overwrite the configuration.Use with Click Integration for CLI apps:Here, dataconfig will generate options for click CLI framework,
one to add defaults to all options with names that exist in
the dataconfig class, overridden by values found in the configuration
file. These options can be overridden by passing values as usual
to the command line.There's also a new option added to the command: "--conf", which
can be used to specify a different configuration file to load
defaults.And finally, any changes made in the command line are applied to
the dataconfig object, but not saved to the configuration file
unless thesave()method is called later.Frozen dataconfig does not work with commandline integration.importclickfromzycelium.dataconfigimportdataconfig@dataconfigclassConfig:name:str="World"config=Config()# No need to load() config when using click_option()@click.command()@click.option("--name")@config.click_option()defmain(name):print(f"Hello,{name}!")print(f"Hello,{config.name}!")main()For more examples:Read through thetests/directory, where you will find the
expected usage and how and why dataconfig can fail.InstallFromPyPIpip install zycelium.dataconfigFrom source:git clone https://github.com/zycelium/dataconfig.gitcd dataconfigpip install -e .