Package: yet-another-config-parser
Yet another python config libraryThis library is designed to unify configuration parser, environment
variables and argument parser.Usage:parser = ArgumentParser()
section = parser.add_section("my_section")
section.add_argument("--my-arg", dest="my_argument", type=int, default=42)

parser.read_args(["--my-arg", "15"])

# parser.my_section.my_argument is now an int equal to 15Every parameter that is expected in configuration file should be
declared. Any undeclared paremeters are available as strings but can
be declared to apply proper type conversion. Parameter can optionally
be bound to an argument flag and/or to environment variable. Setting
through argument flag has priority over environment variable which has
priority over any configuration file.ClassesArgumentParserArgumentParser(*,
               envvar_prefix=None,
               envvar_sep="__",
               root_section_name="yacp-root",
               converters=None)Creates a newArgumentParserobject. All arguments are keyword-only.envvar_prefix: If present will automatically process environment
variables for parameters.envvar_sep: Used to separate section name from variable name in
environment variable search.root_section_nameis the name of the section to store parameters
added directly to theArgumentParserobject.convertersis a dictionary of names and appropriate callables used
to convert the value from string.Ifenvvar_prefixis set to a string, all arguments withoutenvoption set will be searched among environment variables.ifenvvar_prefixis a non-empty string, environment variable name
must start with with<envvar_prefix>_(case-sensitive check)followed by the section name with all'-'characters replaced by'_'(case-insensitive check)followed by theenvvar_sepfollowed by the parameter name (case-insensitive check)For root section the section name could be skipped. In that case there
should be no additionalenvvar_sepbetween prefix (if any) and
variable name.If variable name matches the above and parameter has no explicitenvoption, that environment variable value will be used to override any
values from configuration files.ArgumentParser.add_argument([name_or_flags], **kwargs)Add new argument to the root section of the configuration. Seesection.add_argumentbelow.ArgumentParser.add_section(name)Create a new config section and returns a handle to created section.
If section with the same name was already declared, return that
section instead. SeeSectionclass below.ArgumentParser.parse_args(args=None)Loads configuration from supplied list of arguments orsys.argv[1:].ArgumentParser.read_string(config_string)Loads configuration from string as if it was loaded from file. Note:
root section parameters can be supplied without section header.ArgumentParser.read_file(fh)Loads configuration from an opened file handle. Note: root section
parameters can be supplied without section header.ArgumentPaser.read(filenames)Loads configuration from list of files. Iffilenamesis a single
string it is processed as a single configuration file. Note: unlikeread_stringandread_fileroot section needs an explicit section
header.ArgumentParser.write(fh)Writes current configuration to an opened file handle. Note: only
writes values loaded from files or default values, not values from
arguments or environment.ArgumentParser.get(option)orArgumentParser[option]orArgumentParser.optionReturns either a root option or a section. Order of lookup:declared sectiondeclared root parameterundeclared sectionundeclared root parameterSection or parameter is declared if it was registered usingadd_section/add_argumentand undeclared if it was only present in
a config file. It is possible to declare section or parameter after
reading the configuration. Getting an undeclared section automatically
declares it.SectionSection.add_argument([name_or_flags], **kwargs)Adds a new parameter to the section. Ifname_or_flagsis specified,
the argument is also registered as a command-line argument.**kwargscan include the following:action: Seeargparse action documentation. Supported actions arestore(default),store_true,store_falseandstore_const.dest: Name of the config parameter. Mandatory ifname_or_flagsis not specified.env: name of environment variable to load the parameter valueNote: the variable will only be checked once -- when the parameter
is registered.Note: settingenv=Nonewill disable any environment variable
check for that parameter, includingenvvar_prefixdescribed in
ArgumentParser constructor above.type: (type or string) parameter typestr: string parameter, no processingint: integer parameterfloat: float parameterbool: boolean parameteranything else: appropriate converter is required (see
ArgumentParser constructor above)default,const: Seeargparse documentation.Note: Ifname_or_flagsis not provided and action isstore_const,
actualconstvalue will only be used to infer the parameter type.Note: Iftypeis not provided, type ofdefaultorconstvalue
will be used. If neither is provided, type will be'str'.Note: It is possible to add an argument after reading a configuration
file. This would apply correct type and environment variable override
(if any) to the value. However to apply command line argument
overrides, command line arguments have to be parsed again.Note:defaultandconstvalues are internally stored as strings.
For types wherestr(value)is not compatible with provided
converter,defaultand/orconstshould be provided in a format
suitable for converter with explicittypeset.Example:# given converter 'timedelta': lambda s: timedelta(seconds=int(s))

section.add_argument("--delay", default=timedelta(seconds=10)) # ERROR
section.add_argument("--delay", type=timedelta, default=10)    # okSection.get(option)orSection[option]orSection.optionReturns value ofoptionin this section. If parameter was added
usingadd_argument, it will be converted to either declared or
inferred type. If parameter was only present in the configuration
file, it will be returned as string.