def test_interpolationerror(self): import pickle e1 = ConfigParser.InterpolationError('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_interpolationerror(self): import pickle e1 = ConfigParser.InterpolationError('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 _interpolate(self, section, option, raw_value, tried=None): """Performs variable substituition in a config value.""" variables = self._get_variable_names(section, option, raw_value) if not variables: return raw_value if tried is None: tried = [(section, option)] values = {} for var in variables: parts = var.split('|', 1) if len(parts) == 1: new_section, new_option = section, var else: new_section, new_option = parts if parts in tried: continue try: found = self._get(new_section, new_option) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): raise ConfigParser.InterpolationError( section, option, 'Could not find section %r and option %r.' % (new_section, new_option)) tried.append((new_section, new_option)) if not self.has_option(new_section, new_option): tried.append(('DEFAULT', new_option)) values[var] = self._interpolate(new_section, new_option, found, tried) try: return raw_value % values except KeyError, e: raise ConfigParser.InterpolationError( section, option, 'Cound not replace %r: variable %r is missing.' % (raw_value, e.args[0]))
def merge_config_with_options(section_name, config, options): """ Merge configuration options with a dictionary of options. Keys in the options dictionary take precedence. """ res = {} try: for (key, value) in config.items(section_name): if value.find("\n") != -1: res[key] = value.split("\n") else: res[key] = value except ConfigParser.NoSectionError: pass except ValueError, e: # incomplete format error usually means you forgot # to include the type for interpolation if "incomplete format" in e.message: msg = "Section '%s'. Double check that your formatting " \ "contains the format type after the closing parantheses. " \ "Example: %%(foo)s" % section_name raise ConfigParser.InterpolationError(options, section_name, msg)
def _interpolate(self, section, option, value, tried=None): if not '%(' in value: return value matches = self._interpolate_re.findall(value) if not matches: return value if tried is None: tried = [(section, option)] variables = {} matches = get_unique_sequence(matches) for match in matches: parts = tuple(match.split('|', 1)) if len(parts) == 1: new_section, new_option = section, match else: new_section, new_option = parts if parts in tried: continue tried.append(parts) try: found = self._get(new_section, new_option) tried.append(('DEFAULT', new_option)) variables[match] = self._interpolate(new_section, new_option, found, tried) except Exception: pass if len(matches) == len(variables): return value % variables raise ConfigParser.InterpolationError(section, option, 'Cound not replace %r.' % value)