home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / ConfigParser.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  13.7 KB  |  390 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import re
  5. import types
  6. __all__ = [
  7.     'NoSectionError',
  8.     'DuplicateSectionError',
  9.     'NoOptionError',
  10.     'InterpolationError',
  11.     'InterpolationDepthError',
  12.     'ParsingError',
  13.     'MissingSectionHeaderError',
  14.     'ConfigParser',
  15.     'DEFAULTSECT',
  16.     'MAX_INTERPOLATION_DEPTH']
  17. DEFAULTSECT = 'DEFAULT'
  18. MAX_INTERPOLATION_DEPTH = 10
  19.  
  20. class Error(Exception):
  21.     
  22.     def __init__(self, msg = ''):
  23.         self._msg = msg
  24.         Exception.__init__(self, msg)
  25.  
  26.     
  27.     def __repr__(self):
  28.         return self._msg
  29.  
  30.     __str__ = __repr__
  31.  
  32.  
  33. class NoSectionError(Error):
  34.     
  35.     def __init__(self, section):
  36.         Error.__init__(self, 'No section: %s' % section)
  37.         self.section = section
  38.  
  39.  
  40.  
  41. class DuplicateSectionError(Error):
  42.     
  43.     def __init__(self, section):
  44.         Error.__init__(self, 'Section %s already exists' % section)
  45.         self.section = section
  46.  
  47.  
  48.  
  49. class NoOptionError(Error):
  50.     
  51.     def __init__(self, option, section):
  52.         Error.__init__(self, "No option `%s' in section: %s" % (option, section))
  53.         self.option = option
  54.         self.section = section
  55.  
  56.  
  57.  
  58. class InterpolationError(Error):
  59.     
  60.     def __init__(self, reference, option, section, rawval):
  61.         Error.__init__(self, 'Bad value substitution:\n\tsection: [%s]\n\toption : %s\n\tkey    : %s\n\trawval : %s\n' % (section, option, reference, rawval))
  62.         self.reference = reference
  63.         self.option = option
  64.         self.section = section
  65.  
  66.  
  67.  
  68. class InterpolationDepthError(Error):
  69.     
  70.     def __init__(self, option, section, rawval):
  71.         Error.__init__(self, 'Value interpolation too deeply recursive:\n\tsection: [%s]\n\toption : %s\n\trawval : %s\n' % (section, option, rawval))
  72.         self.option = option
  73.         self.section = section
  74.  
  75.  
  76.  
  77. class ParsingError(Error):
  78.     
  79.     def __init__(self, filename):
  80.         Error.__init__(self, 'File contains parsing errors: %s' % filename)
  81.         self.filename = filename
  82.         self.errors = []
  83.  
  84.     
  85.     def append(self, lineno, line):
  86.         self.errors.append((lineno, line))
  87.         self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
  88.  
  89.  
  90.  
  91. class MissingSectionHeaderError(ParsingError):
  92.     
  93.     def __init__(self, filename, lineno, line):
  94.         Error.__init__(self, 'File contains no section headers.\nfile: %s, line: %d\n%s' % (filename, lineno, line))
  95.         self.filename = filename
  96.         self.lineno = lineno
  97.         self.line = line
  98.  
  99.  
  100.  
  101. class ConfigParser:
  102.     
  103.     def __init__(self, defaults = None):
  104.         self._ConfigParser__sections = { }
  105.         if defaults is None:
  106.             self._ConfigParser__defaults = { }
  107.         else:
  108.             self._ConfigParser__defaults = defaults
  109.  
  110.     
  111.     def defaults(self):
  112.         return self._ConfigParser__defaults
  113.  
  114.     
  115.     def sections(self):
  116.         return self._ConfigParser__sections.keys()
  117.  
  118.     
  119.     def add_section(self, section):
  120.         if section in self._ConfigParser__sections:
  121.             raise DuplicateSectionError(section)
  122.         
  123.         self._ConfigParser__sections[section] = { }
  124.  
  125.     
  126.     def has_section(self, section):
  127.         return section in self._ConfigParser__sections
  128.  
  129.     
  130.     def options(self, section):
  131.         
  132.         try:
  133.             opts = self._ConfigParser__sections[section].copy()
  134.         except KeyError:
  135.             raise NoSectionError(section)
  136.  
  137.         opts.update(self._ConfigParser__defaults)
  138.         if '__name__' in opts:
  139.             del opts['__name__']
  140.         
  141.         return opts.keys()
  142.  
  143.     
  144.     def read(self, filenames):
  145.         if isinstance(filenames, types.StringTypes):
  146.             filenames = [
  147.                 filenames]
  148.         
  149.         for filename in filenames:
  150.             
  151.             try:
  152.                 fp = open(filename)
  153.             except IOError:
  154.                 continue
  155.  
  156.             self._ConfigParser__read(fp, filename)
  157.             fp.close()
  158.         
  159.  
  160.     
  161.     def readfp(self, fp, filename = None):
  162.         if filename is None:
  163.             
  164.             try:
  165.                 filename = fp.name
  166.             except AttributeError:
  167.                 filename = '<???>'
  168.  
  169.         
  170.         self._ConfigParser__read(fp, filename)
  171.  
  172.     
  173.     def get(self, section, option, raw = 0, vars = None):
  174.         d = self._ConfigParser__defaults.copy()
  175.         
  176.         try:
  177.             d.update(self._ConfigParser__sections[section])
  178.         except KeyError:
  179.             if section != DEFAULTSECT:
  180.                 raise NoSectionError(section)
  181.             
  182.         except:
  183.             section != DEFAULTSECT
  184.  
  185.         if vars is not None:
  186.             d.update(vars)
  187.         
  188.         option = self.optionxform(option)
  189.         
  190.         try:
  191.             value = d[option]
  192.         except KeyError:
  193.             raise NoOptionError(option, section)
  194.  
  195.         if raw:
  196.             return value
  197.         
  198.         return self._interpolate(section, option, value, d)
  199.  
  200.     
  201.     def _interpolate(self, section, option, rawval, vars):
  202.         value = rawval
  203.         depth = MAX_INTERPOLATION_DEPTH
  204.         while depth:
  205.             depth -= 1
  206.             if value.find('%(') != -1:
  207.                 
  208.                 try:
  209.                     value = value % vars
  210.                 except KeyError:
  211.                     key = None
  212.                     raise InterpolationError(key, option, section, rawval)
  213.  
  214.             else:
  215.                 break
  216.         if value.find('%(') != -1:
  217.             raise InterpolationDepthError(option, section, rawval)
  218.         
  219.         return value
  220.  
  221.     
  222.     def __get(self, section, conv, option):
  223.         return conv(self.get(section, option))
  224.  
  225.     
  226.     def getint(self, section, option):
  227.         return self._ConfigParser__get(section, int, option)
  228.  
  229.     
  230.     def getfloat(self, section, option):
  231.         return self._ConfigParser__get(section, float, option)
  232.  
  233.     _boolean_states = {
  234.         '1': True,
  235.         'yes': True,
  236.         'true': True,
  237.         'on': True,
  238.         '0': False,
  239.         'no': False,
  240.         'false': False,
  241.         'off': False }
  242.     
  243.     def getboolean(self, section, option):
  244.         v = self.get(section, option)
  245.         if v.lower() not in self._boolean_states:
  246.             raise ValueError, 'Not a boolean: %s' % v
  247.         
  248.         return self._boolean_states[v.lower()]
  249.  
  250.     
  251.     def optionxform(self, optionstr):
  252.         return optionstr.lower()
  253.  
  254.     
  255.     def has_option(self, section, option):
  256.         if not section or section == DEFAULTSECT:
  257.             option = self.optionxform(option)
  258.             return option in self._ConfigParser__defaults
  259.         elif section not in self._ConfigParser__sections:
  260.             return 0
  261.         else:
  262.             option = self.optionxform(option)
  263.             if not option in self._ConfigParser__sections[section]:
  264.                 pass
  265.             return option in self._ConfigParser__defaults
  266.  
  267.     
  268.     def set(self, section, option, value):
  269.         if not section or section == DEFAULTSECT:
  270.             sectdict = self._ConfigParser__defaults
  271.         else:
  272.             
  273.             try:
  274.                 sectdict = self._ConfigParser__sections[section]
  275.             except KeyError:
  276.                 raise NoSectionError(section)
  277.  
  278.         sectdict[self.optionxform(option)] = value
  279.  
  280.     
  281.     def write(self, fp):
  282.         if self._ConfigParser__defaults:
  283.             fp.write('[%s]\n' % DEFAULTSECT)
  284.             for key, value in self._ConfigParser__defaults.items():
  285.                 fp.write('%s = %s\n' % (key, str(value).replace('\n', '\n\t')))
  286.             
  287.             fp.write('\n')
  288.         
  289.         for section in self._ConfigParser__sections:
  290.             fp.write('[%s]\n' % section)
  291.             for key, value in self._ConfigParser__sections[section].items():
  292.                 if key != '__name__':
  293.                     fp.write('%s = %s\n' % (key, str(value).replace('\n', '\n\t')))
  294.                 
  295.             
  296.             fp.write('\n')
  297.         
  298.  
  299.     
  300.     def remove_option(self, section, option):
  301.         if not section or section == DEFAULTSECT:
  302.             sectdict = self._ConfigParser__defaults
  303.         else:
  304.             
  305.             try:
  306.                 sectdict = self._ConfigParser__sections[section]
  307.             except KeyError:
  308.                 raise NoSectionError(section)
  309.  
  310.         option = self.optionxform(option)
  311.         existed = option in sectdict
  312.         if existed:
  313.             del sectdict[option]
  314.         
  315.         return existed
  316.  
  317.     
  318.     def remove_section(self, section):
  319.         existed = section in self._ConfigParser__sections
  320.         if existed:
  321.             del self._ConfigParser__sections[section]
  322.         
  323.         return existed
  324.  
  325.     SECTCRE = re.compile('\\[(?P<header>[^]]+)\\]')
  326.     OPTCRE = re.compile('(?P<option>[^:=\\s][^:=]*)\\s*(?P<vi>[:=])\\s*(?P<value>.*)$')
  327.     
  328.     def __read(self, fp, fpname):
  329.         cursect = None
  330.         optname = None
  331.         lineno = 0
  332.         e = None
  333.         while 1:
  334.             line = fp.readline()
  335.             if not line:
  336.                 break
  337.             
  338.             lineno = lineno + 1
  339.             if line.strip() == '' or line[0] in '#;':
  340.                 continue
  341.             
  342.             if line.split(None, 1)[0].lower() == 'rem' and line[0] in 'rR':
  343.                 continue
  344.             
  345.             if line[0].isspace() and cursect is not None and optname:
  346.                 value = line.strip()
  347.                 if value:
  348.                     cursect[optname] = '%s\n%s' % (cursect[optname], value)
  349.                 
  350.             else:
  351.                 mo = self.SECTCRE.match(line)
  352.                 if mo:
  353.                     sectname = mo.group('header')
  354.                     if sectname in self._ConfigParser__sections:
  355.                         cursect = self._ConfigParser__sections[sectname]
  356.                     elif sectname == DEFAULTSECT:
  357.                         cursect = self._ConfigParser__defaults
  358.                     else:
  359.                         cursect = {
  360.                             '__name__': sectname }
  361.                         self._ConfigParser__sections[sectname] = cursect
  362.                     optname = None
  363.                 elif cursect is None:
  364.                     raise MissingSectionHeaderError(fpname, lineno, `line`)
  365.                 else:
  366.                     mo = self.OPTCRE.match(line)
  367.                     if mo:
  368.                         (optname, vi, optval) = mo.group('option', 'vi', 'value')
  369.                         if vi in ('=', ':') and ';' in optval:
  370.                             pos = optval.find(';')
  371.                             if pos != -1 and optval[pos - 1].isspace():
  372.                                 optval = optval[:pos]
  373.                             
  374.                         
  375.                         optval = optval.strip()
  376.                         if optval == '""':
  377.                             optval = ''
  378.                         
  379.                         optname = self.optionxform(optname.rstrip())
  380.                         cursect[optname] = optval
  381.                     elif not e:
  382.                         e = ParsingError(fpname)
  383.                     
  384.                     e.append(lineno, `line`)
  385.         if e:
  386.             raise e
  387.         
  388.  
  389.  
  390.