home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / base / models.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  4.2 KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from base.g import *
  5. import os.path as os
  6. import re
  7. import glob
  8.  
  9. class ModelData:
  10.     
  11.     def __init__(self, root_path = None):
  12.         if root_path is None:
  13.             self.root_path = prop.models_dir
  14.         else:
  15.             self.root_path = root_path
  16.         self._ModelData__cache = { }
  17.         self.reset_includes()
  18.         self.sec = re.compile('^\\[(.*)\\]')
  19.         self.inc = re.compile('^\\%include (.*)', re.IGNORECASE)
  20.         self.inc_line = re.compile('^\\%(.*)\\%')
  21.         self.eq = re.compile('^([^=]+)=(.*)')
  22.  
  23.     
  24.     def read_all_files(self, unreleased = True):
  25.         released_dat = os.path.join(self.root_path, 'models.dat')
  26.         log.debug('Reading file: %s' % released_dat)
  27.         self.read_section(released_dat)
  28.         unreleased_dir = os.path.join(self.root_path, 'unreleased')
  29.         if unreleased and os.path.exists(unreleased_dir):
  30.             unreleased_dat = os.path.join(self.root_path, 'unreleased', 'unreleased.dat')
  31.             log.debug('Reading file: %s' % unreleased_dat)
  32.             self.read_section(unreleased_dat)
  33.         
  34.         return self._ModelData__cache
  35.  
  36.     
  37.     def read_section(self, filename, section = None, is_include = False):
  38.         found = False
  39.         in_section = False
  40.         if section is not None:
  41.             section = section.lower()
  42.             if is_include:
  43.                 log.debug('Searching for include [%s] in file %s' % (section, filename))
  44.             else:
  45.                 log.debug('Searching for section [%s] in file %s' % (section, filename))
  46.         
  47.         if is_include:
  48.             cache = self._ModelData__includes
  49.         else:
  50.             cache = self._ModelData__cache
  51.         
  52.         try:
  53.             fd = file(filename)
  54.         except IOError:
  55.             e = None
  56.             log.error('I/O Error: %s (%s)' % (filename, e.strerror))
  57.             return False
  58.  
  59.         while True:
  60.             line = fd.readline()
  61.             if not line:
  62.                 break
  63.             
  64.             if line[0] in ('#', ';'):
  65.                 continue
  66.             
  67.             if line[0] == '[':
  68.                 if in_section and section is not None:
  69.                     break
  70.                 
  71.                 match = self.sec.search(line)
  72.                 if match is not None:
  73.                     in_section = True
  74.                     read_section = match.group(1).lower()
  75.                     if section is not None:
  76.                         found = in_section = read_section == section
  77.                     
  78.                     if in_section:
  79.                         if section is not None:
  80.                             log.debug('Found section [%s] in file %s' % (read_section, filename))
  81.                         
  82.                         cache[read_section] = { }
  83.                     
  84.                 in_section
  85.                 continue
  86.             
  87.             if line[0] == '%':
  88.                 match = self.inc.match(line)
  89.                 if match is not None:
  90.                     inc_file = match.group(1)
  91.                     log.debug('Found include file directive: %%include %s' % inc_file)
  92.                     self._ModelData__include_files.append(os.path.join(os.path.dirname(filename), inc_file))
  93.                     continue
  94.                 
  95.                 if in_section:
  96.                     match = self.inc_line.match(line)
  97.                     if match is not None:
  98.                         inc_sect = match.group(1)
  99.                         log.debug('Found include directive %%%s%%' % inc_sect)
  100.                         
  101.                         try:
  102.                             self._ModelData__includes[inc_sect]
  103.                         except KeyError:
  104.                             for inc in self._ModelData__include_files:
  105.                                 if self.read_section(inc, inc_sect, True):
  106.                                     break
  107.                                     continue
  108.                             
  109.                         
  110.  
  111.                     None<EXCEPTION MATCH>KeyError
  112.                 
  113.             
  114.             if in_section:
  115.                 match = self.eq.search(line)
  116.                 if match is not None:
  117.                     value = match.group(2)
  118.                     
  119.                     try:
  120.                         value = int(value)
  121.                     except ValueError:
  122.                         pass
  123.  
  124.                     cache[read_section][match.group(1)] = value
  125.                 
  126.             match is not None
  127.         fd.close()
  128.         return found
  129.  
  130.     
  131.     def reset_includes(self):
  132.         self._ModelData__include_files = []
  133.         self._ModelData__includes = { }
  134.  
  135.     
  136.     def __getitem__(self, model):
  137.         model = model.lower()
  138.         
  139.         try:
  140.             return self._ModelData__cache[model]
  141.         except:
  142.             log.debug('Cache miss: %s' % model)
  143.             released_dat = os.path.join(self.root_path, 'models.dat')
  144.             log.debug('Reading file: %s' % released_dat)
  145.             if self.read_section(released_dat, model):
  146.                 return self._ModelData__cache[model]
  147.             
  148.             unreleased_dir = os.path.join(self.root_path, 'unreleased')
  149.             if os.path.exists(unreleased_dir):
  150.                 unreleased_dat = os.path.join(self.root_path, 'unreleased', 'unreleased.dat')
  151.                 log.debug('Reading file: %s' % unreleased_dat)
  152.                 if self.read_section(unreleased_dat, model):
  153.                     return self._ModelData__cache[model]
  154.                 
  155.             
  156.             return { }
  157.  
  158.  
  159.     
  160.     def all_models(self):
  161.         return self._ModelData__cache
  162.  
  163.  
  164.