当前位置: 首页>>代码示例>>Python>>正文


Python ConfigParser._sections['PACKAGES']方法代码示例

本文整理汇总了Python中ConfigParser.ConfigParser._sections['PACKAGES']方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigParser._sections['PACKAGES']方法的具体用法?Python ConfigParser._sections['PACKAGES']怎么用?Python ConfigParser._sections['PACKAGES']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConfigParser.ConfigParser的用法示例。


在下文中一共展示了ConfigParser._sections['PACKAGES']方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: checkConf

# 需要导入模块: from ConfigParser import ConfigParser [as 别名]
# 或者: from ConfigParser.ConfigParser import _sections['PACKAGES'] [as 别名]
def checkConf(fpath, ftemplate, remove=[], keep=[], update=False,notify=False, compare=False):
    """Check that all the variables in the template are in the config file too"""
    # Remove from the checks the sections in "remove", and if "keep"
    # is used only check those sections.

    # Read the config file fpath and the template ftemplate
    cf = ConfigParser()
    cf.optionxform = str  # keep case (stackoverflow.com/questions/1611799)
    assert cf.read(fpath) != [], 'Missing file %s' % fpath
    ct = ConfigParser()
    ct.optionxform = str
    assert ct.read(ftemplate) != [], 'Missing file %s' % ftemplate

    # Keep only the sections we want to compare from the files.
    for section in set(remove) - set(keep):
        ct.remove_section(section)
        cf.remove_section(section)
    if keep:
        for section in set(ct.sections()) - set(keep):
            ct.remove_section(section)
            cf.remove_section(section)

    df = dict([(s, set(cf.options(s))) for s in cf.sections()])
    dt = dict([(s, set(ct.options(s))) for s in ct.sections()])
    # That funny syntax to create the dictionaries works with old pythons.

    if compare:
        compareConfig(cf, ct, fpath, ftemplate)
        return

    confChanged = False

    if df == dt:
        print(green("All the expected sections and options found in " + fpath))
    else:
        print("Found differences between the configuration file\n  %s\n"
              "and the template file\n  %s" % (fpath, ftemplate))
        sf = set(df.keys())
        st = set(dt.keys())
        for s in sf - st:
            print("Section %s exists in the configuration file but "
                  "not in the template." % red(s))

        for s in st - sf:
            print("Section %s exists in the template but not in the configuration file. Use %s parameter to update  "
                  "local config files." % (yellow(s), UPDATE_PARAM))

            if update:
                # Update config file with missing section
                cf.add_section(s)
                # add it to the keys
                sf.add(s)
                df[s] = set()
                print("Section %s added to your config file."
                      % green(s))
                confChanged = True

        for s in st & sf:
            for o in df[s] - dt[s]:
                print("In section %s, option %s exists in the configuration "
                      "file but not in the template." % (red(s), red(o)))
            for o in dt[s] - df[s]:
                print("In section %s, option %s exists in the template but not in the configuration file. Use %s "
                      "parameter to update local config files." % (yellow(s), yellow(o), UPDATE_PARAM))

                if update:
                    if s=='VARIABLES' and o=='SCIPION_NOTIFY':
                        checkNotify(ct, notify=notify)
                    # Update config file with missing variable
                    value = ct.get(s, o)
                    cf.set(s, o, value)
                    confChanged = True
                    print("Variable %s -> %s added and set to %s in your config file."
                          % (s, green(o), value))

    if update:
        if confChanged:
            print("Changes detected: writing changes into %s. Please check values." % (fpath))
        else:
            print("Update requested no changes detected for %s." % (fpath))

        if 'PACKAGES' in cf._sections:
            # Order the content of packages section alphabetically
            print("Sorting packages section for %s." %(fpath))
            cf._sections['PACKAGES'] = collections.OrderedDict(
                sorted(cf._sections['PACKAGES'].items(), key=lambda t: t[0]))

        try:
            with open(fpath, 'wb') as f:
                cf.write(f)
        except Exception as e:
            print("Could not update the config: ", e)
开发者ID:I2PC,项目名称:scipion,代码行数:94,代码来源:pw_config.py


注:本文中的ConfigParser.ConfigParser._sections['PACKAGES']方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。