(original) (raw)
--- ConfigParser.py.orig 2005-03-16 18:23:19.000000000 +0300 +++ ConfigParser.py 2005-03-17 17:16:53.000000000 +0300 @@ -24,11 +24,15 @@ methods: - __init__(defaults=None) + __init__(defaults=None, delimitier='=:') create the parser and specify a dictionary of intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. Note that `__name__' is always an intrinsic default; it's value is the section's name. + delimitier is string with all symbols which may be used as divisor + between key and value. Key may not contain delimitier symbol and no + escaping is possible. You may try to use space as delimitier. + When writing config file, first symbol from delimitier string is used. sections() return all the configuration section names, sans DEFAULT @@ -200,12 +204,28 @@ class RawConfigParser: - def __init__(self, defaults=None): + def __init__(self, defaults=None, delimitier='=:'): self._sections = {} if defaults is None: self._defaults = {} else: self._defaults = defaults + self.delim = delimitier + self.SECTCRE = re.compile( + r'\[' # [ + r'(?P
[^]]+)' # very permissive! + r'\]' # ] + ) + self.OPTCRE = re.compile( + r'(?P[^'+self.delim+'\s][^'+self.delim+']*)' + # very permissive! + r'\s*(?P['+self.delim+'])\s*' # any number of space/tab, + # followed by separator + # (either : or =), followed + # by any # space/tab + r'(?P.*)$' # everything up to eol + ) + self.delimmap = map(lambda x: x, self.delim) def defaults(self): return self._defaults @@ -357,14 +377,15 @@ if self._defaults: fp.write("[%s]\n" % DEFAULTSECT) for (key, value) in self._defaults.items(): - fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) + fp.write("%s %s %s\n" % + (key, self.delim[0], str(value).replace('\n', '\n\t'))) fp.write("\n") for section in self._sections: fp.write("[%s]\n" % section) for (key, value) in self._sections[section].items(): if key != "__name__": - fp.write("%s = %s\n" % - (key, str(value).replace('\n', '\n\t'))) + fp.write("%s %s %s\n" % + (key, self.delim[0], str(value).replace('\n', '\n\t'))) fp.write("\n") def remove_option(self, section, option): @@ -392,20 +413,6 @@ # # Regular expressions for parsing section headers and options. # - SECTCRE = re.compile( - r'\[' # [ - r'(?P
[^]]+)' # very permissive! - r'\]' # ] - ) - OPTCRE = re.compile( - r'(?P[^:=\s][^:=]*)' # very permissive! - r'\s*(?P[:=])\s*' # any number of space/tab, - # followed by separator - # (either : or =), followed - # by any # space/tab - r'(?P.*)$' # everything up to eol - ) - def _read(self, fp, fpname): """Parse a sectioned setup file. @@ -459,7 +466,7 @@ mo = self.OPTCRE.match(line) if mo: optname, vi, optval = mo.group('option', 'vi', 'value') - if vi in ('=', ':') and ';' in optval: + if vi in self.delimmap and ';' in optval: # ';' is a comment delimiter only if it follows # a spacing character pos = optval.find(';')