home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / launchpadbugs / config.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.3 KB  |  111 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import ConfigParser
  5. import cStringIO as StringIO
  6. import os.path as os
  7. from lpconstants import CONFIG
  8.  
  9. class Config(dict):
  10.     ''' Config system for python-launchpad-bugs
  11.     
  12.     this is working in layers, first the default values (CONFIG.DEFAULT)
  13.     are used, hen the ones defined in the global config file located in
  14.     CONFIG.FILE and at last the configuration set in the file with the
  15.     given filename
  16.     '''
  17.     MAPPING = dict()
  18.     
  19.     def __init__(self, filename = None, ignore = None):
  20.         d = { }
  21.         self._Config__filename = filename
  22.         self._Config__config = ConfigParser.ConfigParser()
  23.         self._Config__config.readfp(StringIO.StringIO(CONFIG.DEFAULT))
  24.         
  25.         try:
  26.             self._Config__config.read(CONFIG.FILE)
  27.         except ConfigParser.MissingSectionHeaderError:
  28.             if os.path.exists(CONFIG.FILE):
  29.                 f = file(CONFIG.FILE, 'r')
  30.                 
  31.                 try:
  32.                     self._Config__config.set('user', 'username', f.read().strip(' \n'))
  33.                 finally:
  34.                     f.close()
  35.  
  36.             
  37.         except:
  38.             os.path.exists(CONFIG.FILE)
  39.  
  40.         if self._Config__filename is not None:
  41.             self._Config__config.read(self._Config__filename)
  42.         
  43.         ignore = self._create_ignore_dict(ignore)
  44.         for section in self._Config__config.sections():
  45.             d[section] = { }
  46.             for key, value in self._Config__config.items(section):
  47.                 if key in ignore[section]:
  48.                     value = ''
  49.                 
  50.                 
  51.                 try:
  52.                     func = self.MAPPING[section][key][0]
  53.                 except (KeyError, IndexError):
  54.                     d[section][key] = value
  55.                     continue
  56.  
  57.                 d[section][key] = func(value)
  58.             
  59.         
  60.         dict.__init__(self, d)
  61.  
  62.     
  63.     def save(self):
  64.         for section in self._Config__config.sections():
  65.             for key, value in self._Config__config.items(section):
  66.                 
  67.                 try:
  68.                     func = self.MAPPING[section][key][1]
  69.                 except (KeyError, IndexError):
  70.                     self._Config__config.set(section, key, self[section][key])
  71.                     continue
  72.  
  73.                 self._Config__config.set(section, key, func(self[section][key]))
  74.             
  75.         
  76.         if not self._Config__filename:
  77.             pass
  78.         f = file(CONFIG.FILE, 'w')
  79.         
  80.         try:
  81.             f.write(CONFIG.COMMENT)
  82.             self._Config__config.write(f)
  83.         finally:
  84.             f.close()
  85.  
  86.  
  87.     
  88.     def _create_ignore_dict(self, ignore_list):
  89.         ret_dict = dict((lambda .0: for s in .0:
  90. (s, set()))(self._Config__config.sections()))
  91.         if ignore_list is None:
  92.             return ret_dict
  93.         ignore = map((lambda x: x.split('.')), ignore_list)
  94.         for i in ignore:
  95.             if i[0] not in ret_dict:
  96.                 continue
  97.             
  98.             
  99.             try:
  100.                 ret_dict[i[0]].add(i[1])
  101.             continue
  102.             except IndexError:
  103.                 ret_dict[i[0]] = set(self._Config__config.options(i[0]))
  104.                 continue
  105.             
  106.  
  107.         
  108.         return ret_dict
  109.  
  110.  
  111.