gpt4 book ai didi

python - 解析配置文件、环境和命令行参数,以获取单个选项集合

转载 作者:IT老高 更新时间:2023-10-28 21:07:24 27 4
gpt4 key购买 nike

Python 的标准库具有用于配置文件解析(configparser)、环境变量读取(os.environ)和命令行参数解析的模块(argparse)。我想编写一个程序来完成所有这些,并且:

  • 具有选项值的级联:

    • 默认选项值,被覆盖
    • 配置文件选项,被
    • 覆盖
    • 环境变量,被
    • 覆盖
    • 命令行选项。
  • 允许在命令行中指定一个或多个配置文件位置,例如--config-file foo.conf,并读取它(代替或附加到通常的配置文件)。这仍然必须遵守上述级联。

  • 允许在一个地方定义选项来确定配置文件和命令行的解析行为。

  • 将已解析的选项统一为一个选项值的单一集合,以供程序的其余部分访问,而无需关心它们来自何处。

我需要的一切显然都在 Python 标准库中,但它们不能顺利协同工作。

如何在与 Python 标准库的偏差最小的情况下实现这一目标?

最佳答案

更新:我终于有时间把它放在 pypi 上。通过以下方式安装最新版本:

   pip install configargparser

Full help and instructions are here .

原帖

这是我一起破解的一些小东西。欢迎在评论中提出改进建议/错误报告:

import argparse
import ConfigParser
import os

def _identity(x):
return x

_SENTINEL = object()


class AddConfigFile(argparse.Action):
def __call__(self,parser,namespace,values,option_string=None):
# I can never remember if `values` is a list all the time or if it
# can be a scalar string; this takes care of both.
if isinstance(values,basestring):
parser.config_files.append(values)
else:
parser.config_files.extend(values)


class ArgumentConfigEnvParser(argparse.ArgumentParser):
def __init__(self,*args,**kwargs):
"""
Added 2 new keyword arguments to the ArgumentParser constructor:

config --> List of filenames to parse for config goodness
default_section --> name of the default section in the config file
"""
self.config_files = kwargs.pop('config',[]) #Must be a list
self.default_section = kwargs.pop('default_section','MAIN')
self._action_defaults = {}
argparse.ArgumentParser.__init__(self,*args,**kwargs)


def add_argument(self,*args,**kwargs):
"""
Works like `ArgumentParser.add_argument`, except that we've added an action:

config: add a config file to the parser

This also adds the ability to specify which section of the config file to pull the
data from, via the `section` keyword. This relies on the (undocumented) fact that
`ArgumentParser.add_argument` actually returns the `Action` object that it creates.
We need this to reliably get `dest` (although we could probably write a simple
function to do this for us).
"""

if 'action' in kwargs and kwargs['action'] == 'config':
kwargs['action'] = AddConfigFile
kwargs['default'] = argparse.SUPPRESS

# argparse won't know what to do with the section, so
# we'll pop it out and add it back in later.
#
# We also have to prevent argparse from doing any type conversion,
# which is done explicitly in parse_known_args.
#
# This way, we can reliably check whether argparse has replaced the default.
#
section = kwargs.pop('section', self.default_section)
type = kwargs.pop('type', _identity)
default = kwargs.pop('default', _SENTINEL)

if default is not argparse.SUPPRESS:
kwargs.update(default=_SENTINEL)
else:
kwargs.update(default=argparse.SUPPRESS)

action = argparse.ArgumentParser.add_argument(self,*args,**kwargs)
kwargs.update(section=section, type=type, default=default)
self._action_defaults[action.dest] = (args,kwargs)
return action

def parse_known_args(self,args=None, namespace=None):
# `parse_args` calls `parse_known_args`, so we should be okay with this...
ns, argv = argparse.ArgumentParser.parse_known_args(self, args=args, namespace=namespace)
config_parser = ConfigParser.SafeConfigParser()
config_files = [os.path.expanduser(os.path.expandvars(x)) for x in self.config_files]
config_parser.read(config_files)

for dest,(args,init_dict) in self._action_defaults.items():
type_converter = init_dict['type']
default = init_dict['default']
obj = default

if getattr(ns,dest,_SENTINEL) is not _SENTINEL: # found on command line
obj = getattr(ns,dest)
else: # not found on commandline
try: # get from config file
obj = config_parser.get(init_dict['section'],dest)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): # Nope, not in config file
try: # get from environment
obj = os.environ[dest.upper()]
except KeyError:
pass

if obj is _SENTINEL:
setattr(ns,dest,None)
elif obj is argparse.SUPPRESS:
pass
else:
setattr(ns,dest,type_converter(obj))

return ns, argv


if __name__ == '__main__':
fake_config = """
[MAIN]
foo:bar
bar:1
"""
with open('_config.file','w') as fout:
fout.write(fake_config)

parser = ArgumentConfigEnvParser()
parser.add_argument('--config-file', action='config', help="location of config file")
parser.add_argument('--foo', type=str, action='store', default="grape", help="don't know what foo does ...")
parser.add_argument('--bar', type=int, default=7, action='store', help="This is an integer (I hope)")
parser.add_argument('--baz', type=float, action='store', help="This is an float(I hope)")
parser.add_argument('--qux', type=int, default='6', action='store', help="this is another int")
ns = parser.parse_args([])

parser_defaults = {'foo':"grape",'bar':7,'baz':None,'qux':6}
config_defaults = {'foo':'bar','bar':1}
env_defaults = {"baz":3.14159}

# This should be the defaults we gave the parser
print ns
assert ns.__dict__ == parser_defaults

# This should be the defaults we gave the parser + config defaults
d = parser_defaults.copy()
d.update(config_defaults)
ns = parser.parse_args(['--config-file','_config.file'])
print ns
assert ns.__dict__ == d

os.environ['BAZ'] = "3.14159"

# This should be the parser defaults + config defaults + env_defaults
d = parser_defaults.copy()
d.update(config_defaults)
d.update(env_defaults)
ns = parser.parse_args(['--config-file','_config.file'])
print ns
assert ns.__dict__ == d

# This should be the parser defaults + config defaults + env_defaults + commandline
commandline = {'foo':'3','qux':4}
d = parser_defaults.copy()
d.update(config_defaults)
d.update(env_defaults)
d.update(commandline)
ns = parser.parse_args(['--config-file','_config.file','--foo=3','--qux=4'])
print ns
assert ns.__dict__ == d

os.remove('_config.file')

待办事项

这个实现仍然不完整。这是部分 TODO 列表:

符合记录的行为

  • (easy) 编写一个函数,从 add_argument 中的 args 找出 dest,而不是依赖 Action对象
  • (琐碎)编写一个使用 parse_known_argsparse_args 函数。 (例如,从 cpython 实现中复制 parse_args 以保证它调用 parse_known_args。)

不太容易的东西……

我还没有尝试过这些。不太可能——但仍然有可能!——它可以正常工作……

  • (很难?)Mutual Exclusion
  • (很难?)Argument Groups (如果实现,这些组应该在配置文件中获得一个 section。)
  • (很难?)Sub Commands (子命令也应该在配置文件中获得一个 section。)

关于python - 解析配置文件、环境和命令行参数,以获取单个选项集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6133517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com