将配置文件部分作为对象
项目描述
Fileconfig将配置文件部分转换为类的实例。创建一个引用INI文件并收集不同实例创建参数的类。使用部分名称作为参数调用类将返回指定部分中指定的参数实例。
链接
安装
此软件包在Python 3.7+上运行,使用pip进行安装
$ pip install fileconfig
用法
将配置创建为fileconfig.Config的子类,并将其实例的filename属性设置为INI文件的路径。
如果文件名是相对的,它将以你的类定义的模块的路径为基准进行解析(即,不是相对于当前工作目录,即使文件不位于该目录下)。
>>> import fileconfig
>>> class Cfg(fileconfig.Config):
... filename = 'docs/pet-shop.ini'
... def __init__(self, key, **kwargs):
... self.key = key
... self.__dict__.update(kwargs)
在实例创建时,将调用__init__方法,传入节名(键)和指定文件给定节的关键字参数。
假设你的INI文件开始如下
[parrot]
species = Norwegian blue
can_talk = yes
quantity = 0
characteristics = beautiful plumage, pining for the fjords
要检索此实例,请使用其节名调用类。
>>> c = Cfg('parrot')
>>> print(c)
{
'can_talk': 'yes',
'characteristics': 'beautiful plumage, pining for the fjords',
'key': 'parrot',
'quantity': '0',
'species': 'Norwegian blue'
}
单例
对于每个配置文件节(也称为单例模式),将只创建、缓存并返回一个实例。
>>> Cfg('parrot') is c
True
构造函数也是幂等的
>>> Cfg(c) is c
True
实例的默认__repr__允许循环往返
>>> c
__main__.Cfg('parrot')
别名
你可以为每个节指定一个由空格分隔的别名列表。
[slug]
aliases = snail special_offer
species = slug
can_talk = no
quantity = 1
有关更改分隔符的信息,请参阅以下内容。
别名映射到相同的实例
>>> s = Cfg('special_offer')
>>> s
__main__.Cfg('slug')
>>> s is Cfg('snail') is Cfg('slug')
True
检查实例名称(键 + 别名)
>>> s.key
'slug'
>>> s.aliases
['snail', 'special_offer']
>>> s.names
['slug', 'snail', 'special_offer']
继承
配置文件节可以继承自另一个节。
[Polly]
inherits = parrot
can_talk = no
characteristics = dead, totally stiff, ceased to exist
指定的键会覆盖继承的键。
>>> print(Cfg('Polly'))
{
'can_talk': 'no',
'characteristics': 'dead, totally stiff, ceased to exist',
'inherits': 'parrot',
'key': 'Polly',
'quantity': '0',
'species': 'Norwegian blue'
}
节可以继承自单个节。不支持多级或传递继承。
检查
使用类迭代所有节的实例。
>>> list(Cfg)
[__main__.Cfg('parrot'), __main__.Cfg('slug'), __main__.Cfg('Polly')]
打印所有实例的字符串表示形式。
>>> Cfg.pprint_all() # doctest: +ELLIPSIS
{
'can_talk': 'yes',
'characteristics': 'beautiful plumage, pining for the fjords',
'key': 'parrot',
...
提示
除了键、别名和继承参数外,你的__init__方法还接收配置文件解析器的未处理字符串。
使用__init__方法处理其他参数以适应你的需求。
>>> class Pet(Cfg):
... def __init__(self, can_talk, quantity, characteristics=None, **kwargs):
... self.can_talk = {'yes':True, 'no': False}[can_talk]
... self.quantity = int(quantity)
... if characteristics is not None and characteristics.strip():
... self.characteristics = [c.strip() for c in characteristics.split(',')]
... super().__init__(**kwargs)
>>> print(Pet('Polly'))
{
'can_talk': False,
'characteristics': ['dead', 'totally stiff', 'ceased to exist'],
'inherits': 'parrot',
'key': 'Polly',
'quantity': 0,
'species': 'Norwegian blue'
}
这样,__init__方法还定义了参数为必需或可选的,设置了它们的默认值等。
覆盖
有时需要将多个配置文件组合在一起,例如,将包含在包目录中的默认文件包含在内,并被不同位置的用户提供的文件覆盖。
为了支持这一点,子类化fileconfig.Stacked并将filename设置为默认配置的位置。
>>> class Settings(fileconfig.Stacked):
... filename = 'docs/pet-shop.ini'
使用add方法在顶部加载一个覆盖配置文件
>>> Settings.add('docs/lumberjack.ini')
如果文件名是相对的,它将以调用add方法的模块的路径为基准进行解析。
你可以访问所有文件中的节
>>> print(Settings('Bevis'))
{
'can_talk': 'yes',
'characteristics': "sleeps all night, works all day, puts on women's clothing",
'key': 'Bevis',
'species': 'human'
}
只要它们的名称是不同的
>>> print(Settings('Polly'))
{
'can_talk': 'no',
'characteristics': 'dead, totally stiff, ceased to exist',
'inherits': 'parrot',
'key': 'Polly',
'quantity': '0',
'species': 'Norwegian blue'
}
添加到堆栈顶部的配置文件将遮盖之前文件中具有相同名称的节
>>> print(Settings('parrot'))
{
'characteristics': 'unsolved problem',
'key': 'parrot'
}
定制
要为别名使用不同的分隔符,请覆盖你的类上的_split_aliases方法。使其成为一个接受字符串参数并返回分割列表的staticmethod或classmethod。
默认情况下,fileconfig将使用标准库中的ConfigParser.SafeConfigParser来解析配置文件。要使用不同的解析器,请覆盖你的fileconfig.Config子类中的_parser属性。
要指定配置解析器应该从配置文件解码的编码,请覆盖你的子类上的_encoding属性。
如果配置文件未找到,Fileconfig会引发错误。如果您希望这个错误静默地通过,请在您的子类中将_pass_notfound属性设置为True。
潜在问题
此包使用sys._getframe(与inspect.currentframe几乎相同,查看文档 文档)。在IronPython下,可能需要启用解释器的FullFrames选项。
许可证
Fileconfig遵循MIT许可协议。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。