當前位置: 首頁>>代碼示例>>Python>>正文


Python configobj.ConfigObjError方法代碼示例

本文整理匯總了Python中configobj.ConfigObjError方法的典型用法代碼示例。如果您正苦於以下問題:Python configobj.ConfigObjError方法的具體用法?Python configobj.ConfigObjError怎麽用?Python configobj.ConfigObjError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在configobj的用法示例。


在下文中一共展示了configobj.ConfigObjError方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: parse

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def parse(self, config, defaults=None):
        """Parse the conf

        :param config: Configuration to parse
        :type config: str, list or File

        :param defaults: Default options
        :type defaults: dict
        """
        self.conf = {}
        self.conffile = config
        self.section = None
        if defaults is not None or not hasattr(self, 'defaults'):
            self.defaults = defaults
        self.validator = validate.Validator()
        try:
            self.conf = configobj.ConfigObj(config, encoding='utf-8')
            self.mtime = os.path.getmtime(self.conffile)
        except configobj.ConfigObjError as exp:
            # We were unable to parse the config
            self.logger.critical('Unable to parse configuration')
            raise exp 
開發者ID:ziirish,項目名稱:burp-ui,代碼行數:24,代碼來源:config.py

示例2: read_config_file

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def read_config_file(f):
    """Read a config file."""

    if isinstance(f, basestring):
        f = os.path.expanduser(f)

    try:
        config = ConfigObj(f, interpolation=False, encoding='utf8')
    except ConfigObjError as e:
        log(LOGGER, logging.ERROR, "Unable to parse line {0} of config file "
            "'{1}'.".format(e.line_number, f))
        log(LOGGER, logging.ERROR, "Using successfully parsed config values.")
        return e.config
    except (IOError, OSError) as e:
        log(LOGGER, logging.WARNING, "You don't have permission to read "
            "config file '{0}'.".format(e.filename))
        return None

    return config 
開發者ID:dbcli,項目名稱:athenacli,代碼行數:21,代碼來源:config.py

示例3: read_config_file

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def read_config_file(self, f):
        """Read a config file *f*.

        :param str f: The path to a file to read.
        """
        configspec = self.default_file if self.validate else None
        try:
            config = ConfigObj(infile=f, configspec=configspec,
                               interpolation=False, encoding='utf8')
        except ConfigObjError as e:
            logger.warning(
                'Unable to parse line {} of config file {}'.format(
                    e.line_number, f))
            config = e.config

        valid = True
        if self.validate:
            valid = config.validate(Validator(), preserve_errors=True,
                                    copy=True)
        if bool(config):
            self.config_filenames.append(config.filename)

        return config, valid 
開發者ID:dbcli,項目名稱:cli_helpers,代碼行數:25,代碼來源:config.py

示例4: parse_config_file

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def parse_config_file(config_file, config_spec_file):

    assert validators.PanoptesValidators.valid_nonempty_string(config_file), u'config_file must be a non-empty str'
    assert validators.PanoptesValidators.valid_nonempty_string(config_spec_file), \
        u'config_spec_file must be a non empty str'

    try:
        config = ConfigObj(config_file, configspec=config_spec_file, interpolation=u'template', file_error=True)
    except IOError as e:
        raise PanoptesConfigurationParsingError(u'Error reading file: %s' % str(e))
    except ConfigObjError as e:
        raise PanoptesConfigurationParsingError(u'Error parsing config file "%s": %s' % (config_file, str(e)))

    validator = Validator()
    result = config.validate(validator, preserve_errors=True)

    if result is not True:
        errors = u''
        for (section_list, key, error) in flatten_errors(config, result):
            if key is None:
                errors += u'Section(s) ' + u','.join(section_list) + u' are missing\n'
            else:
                errors += u'The "' + key + u'" key in section "' + u','.join(section_list) + u'" failed validation\n'

        raise PanoptesConfigurationParsingError(u'Error parsing the configuration file: %s' % errors)

    return config 
開發者ID:yahoo,項目名稱:panoptes,代碼行數:29,代碼來源:helpers.py

示例5: write_picochess_ini

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def write_picochess_ini(key: str, value):
    """Update picochess.ini config file with key/value."""
    try:
        config = ConfigObj('picochess.ini')
        config[key] = value
        config.write()
    except (ConfigObjError, DuplicateError) as conf_exc:
        logging.exception(conf_exc) 
開發者ID:jromang,項目名稱:picochess,代碼行數:10,代碼來源:utilities.py

示例6: test_config_init

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def test_config_init():
    casters = ['string_lower_list', 'force_string', 'boolean_or_string']
    fd, tmpfile = mkstemp()
    os.write(fd, TEST_CONFIG)
    os.close(fd)

    fd, wrong = mkstemp()
    os.write(fd, TEST_CONFIG_FAILURE)
    os.close(fd)

    config = BUIConfig(tmpfile)
    with pytest.raises(configobj.ConfigObjError):
        BUIConfig(wrong, defaults={})

    assert config.safe_get('backend', section='Global') == 'something'
    assert config.safe_get('timeout', 'integer', 'Global') == 12

    config.default_section('Production')

    assert config.safe_get('duplicate') == 'cat'
    assert config.safe_get('duplicate', section='Global') == 'nyan'
    assert config.safe_get('run', 'boolean_or_string') is True
    assert config.safe_get('sql', 'boolean_or_string') == 'none'

    array = config.safe_get('array', 'string_lower_list')
    assert array[1] == 'values'
    assert array[0] == 'some'
    assert isinstance(config.safe_get('array'), list)

    assert config.safe_get('array', 'force_string') == 'some,VALUES'

    for cast in casters:
        # safe_get is safe and shouldn't raise any exception
        assert config.safe_get('i iz not in ze config!', cast) is None

    os.unlink(tmpfile)
    os.unlink(wrong) 
開發者ID:ziirish,項目名稱:burp-ui,代碼行數:39,代碼來源:test_config.py

示例7: _get_config_object

# 需要導入模塊: import configobj [as 別名]
# 或者: from configobj import ConfigObjError [as 別名]
def _get_config_object(self, alternative_config=None):
        """Create a L{ConfigObj} consistent with our preferences.

        @param config_source: Optional readable source to read from instead of
            the default configuration file.
        """
        config_source = alternative_config or self.get_config_filename()
        # Setting list_values to False prevents ConfigObj from being "smart"
        # about lists (it now treats them as strings). See bug #1228301 for
        # more context.
        # Setting raise_errors to False causes ConfigObj to batch all parsing
        # errors into one ConfigObjError raised at the end of the parse instead
        # of raising the first one and then exiting.  This also allows us to
        # recover the good config values in the error handler below.
        # Setting write_empty_values to True prevents configObj writes
        # from writing "" as an empty value, which get_plugins interprets as
        # '""' which search for a plugin named "".  See bug #1241821.
        try:
            config_obj = ConfigObj(config_source, list_values=False,
                                   raise_errors=False, write_empty_values=True)
        except ConfigObjError as e:
            logger = getLogger()
            logger.warn(str(e))
            # Good configuration values are recovered here
            config_obj = e.config
        return config_obj 
開發者ID:CanonicalLtd,項目名稱:landscape-client,代碼行數:28,代碼來源:config.py


注:本文中的configobj.ConfigObjError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。