Package: configuraptor
ConfiguraptorLoad config files into Python classes for a typed config (for type hinting etc.).
    Supported file types are toml/yaml/json, and .env/.ini to a lesser degree 
        (seeSupported Config File Types).Table of ContentsInstallationUsageSupported Config File TypesLicenseChangelogInstallationpip install configuraptorUsageConfiguraptor can be used to load your config files into structured Python classes.# examples/example_from_readme.toml[config]name="Hello World!"[config.reference]number=42numbers=[41,43]string="42"Could be loaded into Python classes using the following code:# examples/example_from_readme.pyfromconfiguraptorimportload_into,TypedConfig####################### with basic classes #######################classSomeRegularClass:number:intnumbers:list[int]string:strclassConfig:name:strreference:SomeRegularClassif__name__=='__main__':my_config=load_into(Config,"example_from_readme.toml")# or .json, .yaml, ...print(my_config.name)# Hello World!print(my_config.reference.numbers)# [41, 43]######################### alternative notation #########################classSomeOtherRegularClass:number:intnumbers:list[int]string:strclassOtherConfig(TypedConfig):name:strreference:SomeRegularClassif__name__=='__main__':my_config=OtherConfig.load("https://api.my-server.dev/v1/config.json?secret=token")# or .toml, .yaml, ...print(my_config.name)# Hello World!print(my_config.reference.numbers)# [41, 43]# TypedConfig has an extra benefit of allowing .update:my_config.update(numbers=[68,70])The second argument of.load_intoand the first argument of.load(which is"example_from_readme.toml"in the
examples above), can be either a string or a Path to a file, a raw dictionary with data, a URL or empty.
You can also use a list of these options to combine data sources.
If it is left empty, thepyproject.tomlwill be used. You can supply akey='tool.mytool.myconf'to specify which
section of the file should be read. For HTTP authentication, currently you can use http basic
auth (https://user:pass@hostor query parameters (like?token=...)).
Other authentication methods are not currently supported.More examples can be found inexamples.Supported Config File Types.toml: supports the most types (strings, numbers, booleans,
datetime, lists/arrays, dicts/tables);.json: supports roughly the same types as toml (except
datetime);.yaml: supports roughly the same types as toml, backwards compatible
with JSON;.env: only supports strings. You can useconvert_types=Trueto try to
convert to your annotated types;.ini: only supports strings. You can useconvert_types=Trueto try to convert to your annotated types;For other file types, a custom Loader can be written.
Seeexamples/readme.md#Custom File TypesBinary ConfigYou can also parse astruct-packedbytestring into a config class.
For this, you have to useBinaryConfigwithBinaryFields. Annotations are not supported in this case, because the order of properties is important for this type
of config.fromconfiguraptorimportBinaryConfig,BinaryFieldclassMyBinaryConfig(BinaryConfig):# annotations not supported! (because mixing annotation and __dict__ lookup messes with the order,# which is important for struct.(un)packnumber=BinaryField(int)string=BinaryField(str,length=5)decimal=BinaryField(float)double=BinaryField(float,format="d")other_string=BinaryField(str,format="10s")boolean=BinaryField(bool)MyBinaryConfig.load(b'*\x00\x00\x00Hello\x00\x00\x00fff@\xab\xaa\xaa\xaa\xaa\xaa\n@Hi\x00\x00\x00\x00\x00\x00\x00\x00\x01')Licenseconfiguraptoris distributed under the terms of theMITlicense.ChangelogSee CHANGELOG.md