home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.2)
-
- import re
- import types
- __all__ = [
- 'NoSectionError',
- 'DuplicateSectionError',
- 'NoOptionError',
- 'InterpolationError',
- 'InterpolationDepthError',
- 'ParsingError',
- 'MissingSectionHeaderError',
- 'ConfigParser',
- 'DEFAULTSECT',
- 'MAX_INTERPOLATION_DEPTH']
- DEFAULTSECT = 'DEFAULT'
- MAX_INTERPOLATION_DEPTH = 10
-
- class Error(Exception):
-
- def __init__(self, msg = ''):
- self._msg = msg
- Exception.__init__(self, msg)
-
-
- def __repr__(self):
- return self._msg
-
- __str__ = __repr__
-
-
- class NoSectionError(Error):
-
- def __init__(self, section):
- Error.__init__(self, 'No section: %s' % section)
- self.section = section
-
-
-
- class DuplicateSectionError(Error):
-
- def __init__(self, section):
- Error.__init__(self, 'Section %s already exists' % section)
- self.section = section
-
-
-
- class NoOptionError(Error):
-
- def __init__(self, option, section):
- Error.__init__(self, "No option `%s' in section: %s" % (option, section))
- self.option = option
- self.section = section
-
-
-
- class InterpolationError(Error):
-
- def __init__(self, reference, option, section, rawval):
- Error.__init__(self, 'Bad value substitution:\n\tsection: [%s]\n\toption : %s\n\tkey : %s\n\trawval : %s\n' % (section, option, reference, rawval))
- self.reference = reference
- self.option = option
- self.section = section
-
-
-
- class InterpolationDepthError(Error):
-
- def __init__(self, option, section, rawval):
- Error.__init__(self, 'Value interpolation too deeply recursive:\n\tsection: [%s]\n\toption : %s\n\trawval : %s\n' % (section, option, rawval))
- self.option = option
- self.section = section
-
-
-
- class ParsingError(Error):
-
- def __init__(self, filename):
- Error.__init__(self, 'File contains parsing errors: %s' % filename)
- self.filename = filename
- self.errors = []
-
-
- def append(self, lineno, line):
- self.errors.append((lineno, line))
- self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
-
-
-
- class MissingSectionHeaderError(ParsingError):
-
- def __init__(self, filename, lineno, line):
- Error.__init__(self, 'File contains no section headers.\nfile: %s, line: %d\n%s' % (filename, lineno, line))
- self.filename = filename
- self.lineno = lineno
- self.line = line
-
-
-
- class ConfigParser:
-
- def __init__(self, defaults = None):
- self._ConfigParser__sections = { }
- if defaults is None:
- self._ConfigParser__defaults = { }
- else:
- self._ConfigParser__defaults = defaults
-
-
- def defaults(self):
- return self._ConfigParser__defaults
-
-
- def sections(self):
- return self._ConfigParser__sections.keys()
-
-
- def add_section(self, section):
- if section in self._ConfigParser__sections:
- raise DuplicateSectionError(section)
-
- self._ConfigParser__sections[section] = { }
-
-
- def has_section(self, section):
- return section in self._ConfigParser__sections
-
-
- def options(self, section):
-
- try:
- opts = self._ConfigParser__sections[section].copy()
- except KeyError:
- raise NoSectionError(section)
-
- opts.update(self._ConfigParser__defaults)
- if '__name__' in opts:
- del opts['__name__']
-
- return opts.keys()
-
-
- def read(self, filenames):
- if isinstance(filenames, types.StringTypes):
- filenames = [
- filenames]
-
- for filename in filenames:
-
- try:
- fp = open(filename)
- except IOError:
- continue
-
- self._ConfigParser__read(fp, filename)
- fp.close()
-
-
-
- def readfp(self, fp, filename = None):
- if filename is None:
-
- try:
- filename = fp.name
- except AttributeError:
- filename = '<???>'
-
-
- self._ConfigParser__read(fp, filename)
-
-
- def get(self, section, option, raw = 0, vars = None):
- d = self._ConfigParser__defaults.copy()
-
- try:
- d.update(self._ConfigParser__sections[section])
- except KeyError:
- if section != DEFAULTSECT:
- raise NoSectionError(section)
-
- except:
- section != DEFAULTSECT
-
- if vars is not None:
- d.update(vars)
-
- option = self.optionxform(option)
-
- try:
- value = d[option]
- except KeyError:
- raise NoOptionError(option, section)
-
- if raw:
- return value
-
- return self._interpolate(section, option, value, d)
-
-
- def _interpolate(self, section, option, rawval, vars):
- value = rawval
- depth = MAX_INTERPOLATION_DEPTH
- while depth:
- depth -= 1
- if value.find('%(') != -1:
-
- try:
- value = value % vars
- except KeyError:
- key = None
- raise InterpolationError(key, option, section, rawval)
-
- else:
- break
- if value.find('%(') != -1:
- raise InterpolationDepthError(option, section, rawval)
-
- return value
-
-
- def __get(self, section, conv, option):
- return conv(self.get(section, option))
-
-
- def getint(self, section, option):
- return self._ConfigParser__get(section, int, option)
-
-
- def getfloat(self, section, option):
- return self._ConfigParser__get(section, float, option)
-
- _boolean_states = {
- '1': True,
- 'yes': True,
- 'true': True,
- 'on': True,
- '0': False,
- 'no': False,
- 'false': False,
- 'off': False }
-
- def getboolean(self, section, option):
- v = self.get(section, option)
- if v.lower() not in self._boolean_states:
- raise ValueError, 'Not a boolean: %s' % v
-
- return self._boolean_states[v.lower()]
-
-
- def optionxform(self, optionstr):
- return optionstr.lower()
-
-
- def has_option(self, section, option):
- if not section or section == DEFAULTSECT:
- option = self.optionxform(option)
- return option in self._ConfigParser__defaults
- elif section not in self._ConfigParser__sections:
- return 0
- else:
- option = self.optionxform(option)
- if not option in self._ConfigParser__sections[section]:
- pass
- return option in self._ConfigParser__defaults
-
-
- def set(self, section, option, value):
- if not section or section == DEFAULTSECT:
- sectdict = self._ConfigParser__defaults
- else:
-
- try:
- sectdict = self._ConfigParser__sections[section]
- except KeyError:
- raise NoSectionError(section)
-
- sectdict[self.optionxform(option)] = value
-
-
- def write(self, fp):
- if self._ConfigParser__defaults:
- fp.write('[%s]\n' % DEFAULTSECT)
- for key, value in self._ConfigParser__defaults.items():
- fp.write('%s = %s\n' % (key, str(value).replace('\n', '\n\t')))
-
- fp.write('\n')
-
- for section in self._ConfigParser__sections:
- fp.write('[%s]\n' % section)
- for key, value in self._ConfigParser__sections[section].items():
- if key != '__name__':
- fp.write('%s = %s\n' % (key, str(value).replace('\n', '\n\t')))
-
-
- fp.write('\n')
-
-
-
- def remove_option(self, section, option):
- if not section or section == DEFAULTSECT:
- sectdict = self._ConfigParser__defaults
- else:
-
- try:
- sectdict = self._ConfigParser__sections[section]
- except KeyError:
- raise NoSectionError(section)
-
- option = self.optionxform(option)
- existed = option in sectdict
- if existed:
- del sectdict[option]
-
- return existed
-
-
- def remove_section(self, section):
- existed = section in self._ConfigParser__sections
- if existed:
- del self._ConfigParser__sections[section]
-
- return existed
-
- SECTCRE = re.compile('\\[(?P<header>[^]]+)\\]')
- OPTCRE = re.compile('(?P<option>[^:=\\s][^:=]*)\\s*(?P<vi>[:=])\\s*(?P<value>.*)$')
-
- def __read(self, fp, fpname):
- cursect = None
- optname = None
- lineno = 0
- e = None
- while 1:
- line = fp.readline()
- if not line:
- break
-
- lineno = lineno + 1
- if line.strip() == '' or line[0] in '#;':
- continue
-
- if line.split(None, 1)[0].lower() == 'rem' and line[0] in 'rR':
- continue
-
- if line[0].isspace() and cursect is not None and optname:
- value = line.strip()
- if value:
- cursect[optname] = '%s\n%s' % (cursect[optname], value)
-
- else:
- mo = self.SECTCRE.match(line)
- if mo:
- sectname = mo.group('header')
- if sectname in self._ConfigParser__sections:
- cursect = self._ConfigParser__sections[sectname]
- elif sectname == DEFAULTSECT:
- cursect = self._ConfigParser__defaults
- else:
- cursect = {
- '__name__': sectname }
- self._ConfigParser__sections[sectname] = cursect
- optname = None
- elif cursect is None:
- raise MissingSectionHeaderError(fpname, lineno, `line`)
- else:
- mo = self.OPTCRE.match(line)
- if mo:
- (optname, vi, optval) = mo.group('option', 'vi', 'value')
- if vi in ('=', ':') and ';' in optval:
- pos = optval.find(';')
- if pos != -1 and optval[pos - 1].isspace():
- optval = optval[:pos]
-
-
- optval = optval.strip()
- if optval == '""':
- optval = ''
-
- optname = self.optionxform(optname.rstrip())
- cursect[optname] = optval
- elif not e:
- e = ParsingError(fpname)
-
- e.append(lineno, `line`)
- if e:
- raise e
-
-
-
-