def _get_variable_names(self, section, option, raw_value): """Returns a list of placeholder names in a config value, if any. Adapted from SafeConfigParser._interpolate_some(). """ result = set() while raw_value: pos = raw_value.find('%') if pos < 0: return result if pos > 0: raw_value = raw_value[pos:] char = raw_value[1:2] if char == '%': raw_value = raw_value[2:] elif char == '(': match = self._interpolate_re.match(raw_value) if match is None: raise ConfigParser.InterpolationSyntaxError( option, section, 'Bad interpolation variable reference: %r.' % raw_value) result.add(match.group(1)) raw_value = raw_value[match.end():] else: raise ConfigParser.InterpolationSyntaxError( option, section, "'%%' must be followed by '%%' or '(', " "found: %r." % raw_value) return result
def test_interpolationsyntaxerror(self): import pickle e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg') pickled = pickle.dumps(e1) e2 = pickle.loads(pickled) self.assertEqual(e1.message, e2.message) self.assertEqual(e1.args, e2.args) self.assertEqual(e1.section, e2.section) self.assertEqual(e1.option, e2.option) self.assertEqual(repr(e1), repr(e2))
def test_interpolationsyntaxerror(self): import pickle e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg') for proto in range(pickle.HIGHEST_PROTOCOL + 1): pickled = pickle.dumps(e1, proto) e2 = pickle.loads(pickled) self.assertEqual(e1.message, e2.message) self.assertEqual(e1.args, e2.args) self.assertEqual(e1.section, e2.section) self.assertEqual(e1.option, e2.option) self.assertEqual(repr(e1), repr(e2))
def get_all_configs(cls, section): '''Get All configurations''' try: confs = cls.configParrser.items(section) except ConfigParser.NoSectionError: if len(cls.loaded_config_file) == 0: raise ConfigParser.NoSectionError( 'No configuration file find.') else: msg = 'Load config file failed: No section [{}]\nConfig file: {}'.format( section, cls.loaded_config_file) raise ConfigParser.NoSectionError(msg) except ConfigParser.InterpolationSyntaxError as e: raise ConfigParser.InterpolationSyntaxError( "Config file syntax error: {}".format(e)) except Exception as e: raise Exception(e) # https://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list # 环境变量(大写)优先于配置文件 return {key: os_getenv(key.upper(), val) for key, val in confs}