Package: autoconfiguration
python-autoconfigurationLoad configuration files (.ini) automatically.UsageTheinitfunction of theautoconfigurationpackage has to be called first to initialize the configuration. Pass an arbitrary amount of configuration files to this function. All passed files will be loaded. Additionally the global configuration file (config.ini) will always be loaded by default. The name of the global configuration file has to beconfig.ini. All other files must start withconfig-and end with.ini. You don't have to use the full file names for theinitfunction. You can just use the name betweenconfig-and.ini.Theinitfunction expects a second parameterconfig_class. This should be a dataclass containing all sections of the configuration files. The types of the fields should be dataclasses too. These dataclasses should contain the keys of the respective sections.Supported data types for keys in the dataclasses:strintfloatcomplexboolListTupleDictOptional (value will be set toNoneif the key could not be found in the configuration)Default values are supported too which will be set if the respective key could not be found in the configuration.The third parameter of theinitfunction is the optionalconfig_dirparameter. This should be a path to the directory containing the configuration files. Absolute paths are supported. The default isconfig. This works if the name of the directory isconfigand it exists in the directory where the application was executed from.Exampleconfig files:config.ini:[section]
key=testconfig-dev.ini:[test]
# % needs to be escaped with another %, so a str containing %% will contain only %
test-complex-str=test %%d 1
    # Lines after the first line have to be indented deeper than the first line
    2 \n\ta
test-int=123
test-bool=False
test-float=0.987
test-complex=1j
test-list=["abc", 123]
test-tuple=(123, "abc")
test-dict={"test": 123, 2: "abc"}dataclasses:fromdataclassesimportdataclass,fieldfromtypingimportList,Tuple,Dict,Any,Optional@dataclassclassSection:key:str@dataclassclassTest:test_complex_str:strtest_int:inttest_bool:booltest_float:floattest_complex:complextest_list:List[Any]test_tuple:Tuple[int,str]test_dict:Dictoptional:Optional[str]default_int:int=987default_list:List[str]=field(default_factory=lambda:[1,2,3])@dataclassclassConfig:section:Sectiontest:TestInitialize autoconfiguration:fromautoconfigurationimportautoconfigurationconfig:Config=autoconfiguration.init("dev",config_class=Config)You can enable auto completion in IDEs by specifying the type of the variable (config: Config).After the autoconfiguration was initialized you can get the configuration from anywhere in your code by calling thegetfunction:fromautoconfigurationimportautoconfigurationconfig:Config=autoconfiguration.get()The instance created byinitis cached. That means that both theinitand thegetfunction always returns the same instance for a specific config class.The instance of the config class above looks like:Config(section=Section(key="test"),types=Types(test_complex_str="test%d1\n2\\n\\ta",test_int=123,test_bool=False,test_float=0.987,test_complex=1j,test_list=["abc",123],test_tuple=(123,"abc"),test_dict={"test":123,2:"abc"},optional=None,default_int=987,default_list=[1,2,3],),)Multiple instancesIfinitis called with another config class, autoconfiguration creates and returns a new instance and caches this instance too.Ifgetis called without the config class parameter, it always returns the first cached instance. Pass the config class togetto get another instance:fromautoconfigurationimportautoconfigurationconfig:SecondConfig=autoconfiguration.get(SecondConfig)Seeexample/main.pyfor a complete example.