def _interpolate(self, section, option, rawval, vars): # special overloading of SafeConfigParser._interpolate: # do not attempt to interpolate if the string is (double-)quoted if is_quoted(rawval): return rawval # cannot use 'super' here as ConfigParser is outdated return ConfigParser._interpolate(self, section, option, rawval, vars)
def _interpolate(self, section, option, rawval, vars): value = ConfigParser._interpolate(self, section, option, rawval, vars) return EnvironmentInterpolation().before_get( parser=self, section=section, option=option, value=value, defaults=None, )
def _interpolate(self, section, option, rawval, vars): # Python < 3.2 try: return ConfigParser._interpolate( self, section, option, rawval, vars) except Exception: e = sys.exc_info()[1] args = list(e.args) args[0] = f'Error in file {self.filename}: {e}' e.args = tuple(args) e.message = args[0] raise
def diff_ini(first, second, diff=None, existing_only=False): """Diff ini files Generate a parser with any value in the second that is different in the first. Returns a ConfigParser. Takes interpolation into account. Does not include values that disappeared.""" from configparser import _Chainmap first = asParser(first) second = asParser(second) # TODO: Look at first both in raw and formatted versions diff = diff or ConfigParser(interpolation=None) interpolating = ConfigParser() for section in second.sections(): if section != 'DEFAULT' and not first.has_section(section): if not existing_only: diff.add_section(section) for option in second.options(section): value = second.get(section, option) diff.set(section, option, value) else: vars = _Chainmap(second._sections[section], first._sections[section], second._defaults, first._defaults) for option, value2 in second.items(section): if not first.has_option(section, option): if not existing_only: ensureSection(diff, section) diff.set(section, option, value2) continue value1 = first.get(section, option) if value1 != value2 and '%(' in value1: # try to interpolate, and see if it would amount to the same. try: value1 = interpolating._interpolate( section, option, value1, vars) except Exception as e: pass if value1 != value2: ensureSection(diff, section) diff.set(section, option, value2) return diff