一个python库,用于解析多种类型的配置文件、envvars和命令行参数,这使设置应用程序配置变得更加头疼。
parse-it的Python项目详细描述
解析它
一个python库,用于解析多种类型的配置文件、envvars和命令行参数,这使设置应用程序配置变得更加头疼。
代码覆盖率:rel="nofollow">
安装
首先安装parse_it,对于python 3.5及更高版本,只需使用pip:
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
如果您使用的是python 3.4或更高版本,您还需要输入后端口软件包,这可以通过以下可选安装来完成:
# Install from PyPi for Python version 3.4 & lower pip install parse_it[typing]
如何使用
# Load parse_itfromparse_itimportParseIt# Create parse_it object.parser=ParseIt()# Now you can read your configuration values no matter how they are configured (cli args, envvars, json/yaml/etc files)my_config_key=parser.read_configuration_variable("my_config_key")
默认情况下,所有配置文件都将被假定在workdir中,但如果您希望,也可以轻松地将其设置为递归地查找所有子文件夹:
# Load parse_itfromparse_itimportParseIt# cat /etc/my_config_folder/my_inner_conf_folder/my_config.json >>>## {# "my_int": 123# }# # Create parse_it object that will look for the config files in the "/etc/my_config_folder" and all of it's subfoldersparser=ParseIt(config_location="/etc/my_config_folder",recurse=True)my_config_key=parser.read_configuration_variable("my_int")# my_config_key will now be an int of 123
默认情况下,parse_将按以下顺序查找配置选项,并返回找到的第一个选项:
cli-args
-以以下格式传递的命令行参数--键值env-vars
-环境变量,您还可以使用envvars
作为它的别名json
-json格式化文件,配置文件夹中任何以.json扩展名结尾的文件都假定为yaml
-yaml格式化文件,配置文件夹中以.yaml扩展名结尾的任何文件都假定为yml
-yaml格式化文件,配置文件夹中以.yml扩展名结尾的任何文件都假定为toml
-toml格式化文件,配置文件夹中以.toml扩展名结尾的任何文件都假定是这样的tml
-toml格式化文件,配置文件夹中以.tml扩展名结尾的任何文件都假定是这样的hcl
-hcl格式化文件,配置文件夹中以.hcl扩展名结尾的任何文件都假定为此文件tf
-hcl格式化文件,配置文件夹中以.tf扩展名结尾的任何文件都假定为conf
-ini格式化文件,配置文件夹中以.conf扩展名结尾的任何文件都假定是这样的cfg
-ini格式化文件,配置文件夹中以.cfg扩展名结尾的任何文件都假定是这样的ini
-ini格式化文件,配置文件夹中以.ini扩展名结尾的任何文件都假定是这样的xml
-xml格式化文件,配置文件夹中以.xml扩展名结尾的任何文件都假定是这样的- 配置默认值-也可以选择使用默认值设置每个配置值
- 全局默认值-Parser对象还有一个可以设置的全局默认值
如果同一文件夹parse中存在同一类型的多个文件,则在转到下一类型之前,parse会按字母顺序查找所有文件,
您可以决定使用上述选项的任何子集的自定义顺序(默认值除外,它们将始终是最后一个):
# Load parse_itfromparse_itimportParseIt# Create parse_it object which will only look for envvars then yaml & yml files then json filesparser=ParseIt(config_type_priority=["env_vars","yaml","yml","json"])
默认情况下,全局默认值为"无",但如果需要,设置它很简单:
# Load parse_itfromparse_itimportParseIt# Create parse_it object with a custom default valueparser=ParseIt()my_config_key=parser.read_configuration_variable("my_undeclared_key")# my_config_key will now be a None# Create parse_it object with a custom default valueparser=ParseIt(global_default_value="my_default_value")my_config_key=parser.read_configuration_variable("my_undeclared_key")# my_config_key will now be an string of "my_default_value"
parse默认情况下,它会试图找出返回的值的类型,因此即使在envvars、cli args&ini文件中,也会得到strings/dicts/etc:
# Load parse_itfromparse_itimportParseIt# This is just for the exampleimportosos.environ["MY_INT"]="123"os.environ["MY_LIST"]="['first_item', 'second_item', 'third_item']"os.environ["MY_DICT"]="{'key': 'value'}"# Create parse_it objectparser=ParseIt()my_config_key=parser.read_configuration_variable("MY_INT")# my_config_key will now be an string of "123"my_config_key=parser.read_configuration_variable("MY_LIST")# my_config_key will now be an list of ['first_item', 'second_item', 'third_item']my_config_key=parser.read_configuration_variable("MY_DICT")# my_config_key will now be an dict of {'key': 'value'}# you can easily disable the type estimationparser=ParseIt(type_estimate=False)my_config_key=parser.read_configuration_variable("MY_INT")# my_config_key will now be an string of "123"my_config_key=parser.read_configuration_variable("MY_LIST")# my_config_key will now be an string of "['first_item', 'second_item', 'third_item']"my_config_key=parser.read_configuration_variable("MY_DICT")# my_config_key will now be an string of "{'key': 'value'}"
由于envvars建议的语法是让所有键都是大写的,这与其他配置文件的语法不同,因此当查看envvars中的匹配值时,它会自动将任何需要的配置值更改为大写,但如果需要,您当然可以禁用它。特点:
# Load parse_itfromparse_itimportParseIt# This is just for the exampleimportosos.environ["MY_STRING"]="UPPER"os.environ["my_string"]="lower"# Create parse_it objectparser=ParseIt()my_config_key=parser.read_configuration_variable("my_string")# my_config_key will now be an string of "UPPER"# disabling force envvar uppercaseparser=ParseIt(force_envvars_uppercase=False)my_config_key=parser.read_configuration_variable("my_string")# my_config_key will now be an string of "lower"
您还可以轻松地为所有envvars添加前缀(注意force_envvars_uppercase
也会影响给定的前缀):
# Load parse_itfromparse_itimportParseIt# This is just for the exampleimportosos.environ["PREFIX_MY_INT"]="123"# add a prefix to all envvars usedparser=ParseIt(envvar_prefix="prefix_")my_config_key=parser.read_configuration_variable("my_int")# my_config_key will now be a int of 123
您还可以根据每个配置密钥设置默认值:
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
0
您还可以将密钥声明为必需的(默认情况下禁用),这样,如果用户没有在任何地方声明密钥,它将引发一个valueerror:
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
1
虽然通常不是一个好主意,但有时您无法避免它,需要使用自定义的非标准文件后缀,您可以将后缀的自定义映射添加到任何受支持的文件格式,如下所示(请注意,还应设置config\u type\u priority
以配置优先级所述自定义后缀的值):
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
2
有时您可能希望检查最终用户是否向配置传递了特定类型的变量,parse它允许您通过设置allowed\u types
来轻松检查值是否属于给定的类型列表,如果给定的值类型不在列表中,则会引发typeerror在允许的类型中,默认设置为none,因此不会发生类型确保:
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
3
有时,您需要很多配置键才能拥有相同的parse_it配置参数,而不是自己循环它们,这可以通过read_multiple_configuration_variables
函数来实现,您将为它提供所需配置键的列表&;将对所有配置应用相同的配置,并返回带有配置键/值的dict。
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
4
您还可以读取单个文件,而不是配置目录。
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
5
另一种选择是将所有有效源中的所有配置读取到一个dict中,该dict将包含所有配置的组合结果(通过组合,这意味着它将只返回每个找到的密钥的最高优先级&;将来自不同来源的不同键组合成一个dict),这提供了较少的灵活性,然后逐个读取配置变量,速度稍慢(但只是稍慢),但对于某些用例来说使用起来更简单:
# Install from PyPi for Python version 3.5 & higher
pip install parse_it
6