home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2866 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.5 KB  |  151 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. xpath_tokenizer = re.compile('(::|\\.\\.|\\(\\)|[/.*:\\[\\]\\(\\)@=])|((?:\\{[^}]+\\})?[^/:\\[\\]\\(\\)@=\\s]+)|\\s+').findall
  6.  
  7. class xpath_descendant_or_self:
  8.     pass
  9.  
  10.  
  11. class Path:
  12.     
  13.     def __init__(self, path):
  14.         tokens = xpath_tokenizer(path)
  15.         self.path = []
  16.         self.tag = None
  17.         if tokens and tokens[0][0] == '/':
  18.             raise SyntaxError('cannot use absolute path on element')
  19.         tokens[0][0] == '/'
  20.         while tokens:
  21.             (op, tag) = tokens.pop(0)
  22.             if tag or op == '*':
  23.                 if not tag:
  24.                     pass
  25.                 self.path.append(op)
  26.             elif op == '.':
  27.                 pass
  28.             elif op == '/':
  29.                 self.path.append(xpath_descendant_or_self())
  30.                 continue
  31.             else:
  32.                 raise SyntaxError('unsupported path syntax (%s)' % op)
  33.             if op == '*':
  34.                 (op, tag) = tokens.pop(0)
  35.                 if op != '/':
  36.                     if not op:
  37.                         pass
  38.                     raise SyntaxError('expected path separator (%s)' % tag)
  39.                 op != '/'
  40.                 continue
  41.         if self.path and isinstance(self.path[-1], xpath_descendant_or_self):
  42.             raise SyntaxError('path cannot end with //')
  43.         isinstance(self.path[-1], xpath_descendant_or_self)
  44.         if len(self.path) == 1 and isinstance(self.path[0], type('')):
  45.             self.tag = self.path[0]
  46.         
  47.  
  48.     
  49.     def find(self, element):
  50.         tag = self.tag
  51.         if tag is None:
  52.             nodeset = self.findall(element)
  53.             if not nodeset:
  54.                 return None
  55.             return nodeset[0]
  56.         for elem in element:
  57.             if elem.tag == tag:
  58.                 return elem
  59.         
  60.  
  61.     
  62.     def findtext(self, element, default = None):
  63.         tag = self.tag
  64.         if tag is None:
  65.             nodeset = self.findall(element)
  66.             if not nodeset:
  67.                 return default
  68.             if not nodeset[0].text:
  69.                 pass
  70.             return ''
  71.         for elem in element:
  72.             if elem.tag == tag:
  73.                 if not elem.text:
  74.                     pass
  75.                 return ''
  76.         
  77.         return default
  78.  
  79.     
  80.     def findall(self, element):
  81.         nodeset = [
  82.             element]
  83.         index = 0
  84.         while None:
  85.             
  86.             try:
  87.                 path = self.path[index]
  88.                 index = index + 1
  89.             except IndexError:
  90.                 return nodeset
  91.  
  92.             set = []
  93.             if isinstance(path, xpath_descendant_or_self):
  94.                 
  95.                 try:
  96.                     tag = self.path[index]
  97.                     if not isinstance(tag, type('')):
  98.                         tag = None
  99.                     else:
  100.                         index = index + 1
  101.                 except IndexError:
  102.                     tag = None
  103.  
  104.                 for node in nodeset:
  105.                     new = list(node.getiterator(tag))
  106.                     if new and new[0] is node:
  107.                         set.extend(new[1:])
  108.                         continue
  109.                     set.extend(new)
  110.                 
  111.             else:
  112.                 for node in nodeset:
  113.                     for node in node:
  114.                         if path == '*' or node.tag == path:
  115.                             set.append(node)
  116.                             continue
  117.                     
  118.                 
  119.             if not set:
  120.                 return []
  121.             nodeset = set
  122.             continue
  123.             return None
  124.  
  125.  
  126. _cache = { }
  127.  
  128. def _compile(path):
  129.     p = _cache.get(path)
  130.     if p is not None:
  131.         return p
  132.     p = Path(path)
  133.     if len(_cache) >= 100:
  134.         _cache.clear()
  135.     
  136.     _cache[path] = p
  137.     return p
  138.  
  139.  
  140. def find(element, path):
  141.     return _compile(path).find(element)
  142.  
  143.  
  144. def findtext(element, path, default = None):
  145.     return _compile(path).findtext(element, default)
  146.  
  147.  
  148. def findall(element, path):
  149.     return _compile(path).findall(element)
  150.  
  151.