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

big*_*ose 104 python environment-variables configuration-files command-line-arguments

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

  • 有一系列选项值:

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

  • 允许在一个位置使用选项定义来确定配置文件和命令行的解析行为.

  • 将解析的选项统一到一个选项值集合中,以便程序的其余部分可以访问,而无需关心它们来自何处.

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

如何以最小的Python标准库偏差实现这一目标?

Ale*_*ary 31

只要您对配置文件看起来像命令行感到满意,argparse模块就不会这么做.(我认为这是一个优势,因为用户只需要学习一种语法.)例如,将fromfile_prefix_chars设置为@,使得它,

my_prog --foo=bar
Run Code Online (Sandbox Code Playgroud)

相当于

my_prog @baz.conf
Run Code Online (Sandbox Code Playgroud)

如果@baz.conf是的话

--foo
bar
Run Code Online (Sandbox Code Playgroud)

您甚至可以foo.conf通过修改自动查找代码argv

if os.path.exists('foo.conf'):
    argv = ['@foo.conf'] + argv
args = argparser.parse_args(argv)
Run Code Online (Sandbox Code Playgroud)

这些配置文件的格式是通过使ArgumentParser的子类,添加可修改convert_arg_line_to_args方法.


mgi*_*son 23

更新: 我终于把它放在了pypi上.安装最新版本:

   pip install configargparser
Run Code Online (Sandbox Code Playgroud)

完整的帮助和说明在这里.

原帖

这是我一起入侵的一些东西.在评论中随意建议改进/错误报告:

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')
Run Code Online (Sandbox Code Playgroud)

去做

此实施仍然不完整.这是部分TODO列表:

符合记录的行为

  • (容易)编写计算出的功能destargsadd_argument,而不是依赖的上Action对象
  • (平凡)写一个parse_args使用的函数parse_known_args.(例如,parse_argscpython实现中复制以保证它调用parse_known_args.)

不太容易的东西......

我还没有尝试过这个.它不太可能 - 但仍然可能! - 它可以正常工作......

  • (难吗?)相互排斥
  • (硬?)参数组 (如果实现,这些组应该section在配置文件中获得.)
  • (硬?)子命令 (子命令也应该section在配置文件中获得.)

  • FWIW,我终于把它放在了pypi上 - 你_should_能够通过`pip install configargparser安装它 (2认同)

Pio*_*ost 11

有一个名为configglue的库.

configglue是一个将python的optparse.OptionParser和ConfigParser.ConfigParser粘合在一起的库,因此当您想要将相同的选项导出到配置文件和命令行界面时,您不必重复自己.

它还支持环境变量.

还有一种叫库ConfigArgParse这是

argparse的替代品,允许通过配置文件和/或环境变量设置选项.

您可能对PyCon谈论ŁukaszLanga的配置感兴趣 - 让他们配置!


rut*_*sky 9

虽然我没有亲自尝试过,但有一个ConfigArgParse库,它表明它可以完成你想要的大部分工作:

argparse的替代品,允许通过配置文件和/或环境变量设置选项.


big*_*ose 7

看来标准库不解决这个问题,让每个程序员凑齐configparser,并argparseos.environ在笨重的方式都在一起.


Rus*_*ove 5

为了满足所有这些要求,我建议编写您自己的库,该库同时使用 [opt|arg]parse 和 configparser 来实现底层功能。

考虑到前两个和最后一个要求,我想说你想要:

第一步:执行命令行解析器传递,仅查找 --config-file 选项。

第二步:解析配置文件。

第三步:使用配置文件传递的输出作为默认值设置第二个命令行解析器传递。

第三个要求可能意味着您必须设计自己的选项定义系统来公开您关心的 optparse 和 configparser 的所有功能,并编写一些管道来在两者之间进行转换。


小智 5

据我所知,Python标准库不提供此功能.我通过编写代码来解决这个问题optparseConfigParser解析命令行和配置文件,并在它们之上提供一个抽象层.但是,你需要将它作为一个单独的依赖项,从你之前的评论中看起来似乎是不合适的.

如果你想查看我写的代码,请访问http://liw.fi/cliapp/.它已集成到我的"命令行应用程序框架"库中,因为这是框架需要做的很大一部分.