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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Ka-Ping Yee <ping@lfw.org>'
  5. __license__ = 'MIT'
  6. import string
  7. import sys
  8. from tokenize import tokenprog
  9. from types import StringType
  10.  
  11. class ItplError(ValueError):
  12.     
  13.     def __init__(self, text, pos):
  14.         self.text = text
  15.         self.pos = pos
  16.  
  17.     
  18.     def __str__(self):
  19.         return 'unfinished expression in %s at char %d' % (repr(self.text), self.pos)
  20.  
  21.  
  22.  
  23. def matchorfail(text, pos):
  24.     match = tokenprog.match(text, pos)
  25.     if match is None:
  26.         raise ItplError(text, pos)
  27.     match is None
  28.     return (match, match.end())
  29.  
  30.  
  31. try:
  32.     if not sys.stdin.encoding:
  33.         pass
  34.     itpl_encoding = 'ascii'
  35. except AttributeError:
  36.     itpl_encoding = 'ascii'
  37.  
  38.  
  39. class Itpl:
  40.     
  41.     def __init__(self, format, codec = itpl_encoding, encoding_errors = 'backslashreplace'):
  42.         if not isinstance(format, basestring):
  43.             raise TypeError, 'needs string initializer'
  44.         isinstance(format, basestring)
  45.         self.format = format
  46.         self.codec = codec
  47.         self.encoding_errors = encoding_errors
  48.         namechars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  49.         chunks = []
  50.         pos = 0
  51.         while None:
  52.             dollar = string.find(format, '$', pos)
  53.             if dollar < 0:
  54.                 break
  55.             
  56.             nextchar = format[dollar + 1]
  57.             if nextchar == '{':
  58.                 chunks.append((0, format[pos:dollar]))
  59.                 pos = dollar + 2
  60.                 level = 1
  61.                 while level:
  62.                     (match, pos) = matchorfail(format, pos)
  63.                     (tstart, tend) = match.regs[3]
  64.                     token = format[tstart:tend]
  65.                     if token == '{':
  66.                         level = level + 1
  67.                         continue
  68.                     if token == '}':
  69.                         level = level - 1
  70.                         continue
  71.                 chunks.append((1, format[dollar + 2:pos - 1]))
  72.                 continue
  73.             if nextchar in namechars:
  74.                 chunks.append((0, format[pos:dollar]))
  75.                 (match, pos) = matchorfail(format, dollar + 1)
  76.                 while pos < len(format):
  77.                     if format[pos] == '.' and pos + 1 < len(format) and format[pos + 1] in namechars:
  78.                         (match, pos) = matchorfail(format, pos + 1)
  79.                         continue
  80.                     if format[pos] in '([':
  81.                         pos = pos + 1
  82.                         level = 1
  83.                         while level:
  84.                             (match, pos) = matchorfail(format, pos)
  85.                             (tstart, tend) = match.regs[3]
  86.                             token = format[tstart:tend]
  87.                             if token[0] in '([':
  88.                                 level = level + 1
  89.                                 continue
  90.                             if token[0] in ')]':
  91.                                 level = level - 1
  92.                                 continue
  93.                         continue
  94.                     break
  95.                 chunks.append((1, format[dollar + 1:pos]))
  96.                 continue
  97.             chunks.append((0, format[pos:dollar + 1]))
  98.             pos = dollar + 1 + (nextchar == '$')
  99.             continue
  100.             if pos < len(format):
  101.                 chunks.append((0, format[pos:]))
  102.             
  103.         self.chunks = chunks
  104.  
  105.     
  106.     def __repr__(self):
  107.         return '<Itpl %s >' % repr(self.format)
  108.  
  109.     
  110.     def _str(self, glob, loc):
  111.         result = []
  112.         app = result.append
  113.         for live, chunk in self.chunks:
  114.             if live:
  115.                 val = eval(chunk, glob, loc)
  116.                 
  117.                 try:
  118.                     app(str(val))
  119.                 except UnicodeEncodeError:
  120.                     app(unicode(val))
  121.                 except:
  122.                     None<EXCEPTION MATCH>UnicodeEncodeError
  123.                 
  124.  
  125.             None<EXCEPTION MATCH>UnicodeEncodeError
  126.             app(chunk)
  127.         
  128.         out = ''.join(result)
  129.         
  130.         try:
  131.             return str(out)
  132.         except UnicodeError:
  133.             return out.encode(self.codec, self.encoding_errors)
  134.  
  135.  
  136.     
  137.     def __str__(self):
  138.         frame = sys._getframe(1)
  139.         while frame.f_globals['__name__'] == __name__:
  140.             frame = frame.f_back
  141.         loc = frame.f_locals
  142.         glob = frame.f_globals
  143.         return self._str(glob, loc)
  144.  
  145.  
  146.  
  147. class ItplNS(Itpl):
  148.     
  149.     def __init__(self, format, globals, locals = None, codec = 'utf_8', encoding_errors = 'backslashreplace'):
  150.         if locals is None:
  151.             locals = globals
  152.         
  153.         self.globals = globals
  154.         self.locals = locals
  155.         Itpl.__init__(self, format, codec, encoding_errors)
  156.  
  157.     
  158.     def __str__(self):
  159.         return self._str(self.globals, self.locals)
  160.  
  161.     
  162.     def __repr__(self):
  163.         return '<ItplNS %s >' % repr(self.format)
  164.  
  165.  
  166.  
  167. def itpl(text):
  168.     return str(Itpl(text))
  169.  
  170.  
  171. def printpl(text):
  172.     print itpl(text)
  173.  
  174.  
  175. def itplns(text, globals, locals = None):
  176.     return str(ItplNS(text, globals, locals))
  177.  
  178.  
  179. def printplns(text, globals, locals = None):
  180.     print itplns(text, globals, locals)
  181.  
  182.  
  183. class ItplFile:
  184.     
  185.     def __init__(self, file):
  186.         self.file = file
  187.  
  188.     
  189.     def __repr__(self):
  190.         return '<interpolated ' + repr(self.file) + '>'
  191.  
  192.     
  193.     def __getattr__(self, attr):
  194.         return getattr(self.file, attr)
  195.  
  196.     
  197.     def write(self, text):
  198.         self.file.write(str(Itpl(text)))
  199.  
  200.  
  201.  
  202. def filter(file = sys.stdout):
  203.     return ItplFile(file)
  204.  
  205.  
  206. def unfilter(ifile = None):
  207.     if not ifile or ifile.file:
  208.         pass
  209.     return sys.stdout.file
  210.  
  211.