home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1783 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.4 KB  |  194 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. class Itpl:
  32.     
  33.     def __init__(self, format, codec = 'utf_8', encoding_errors = 'backslashreplace'):
  34.         if not isinstance(format, basestring):
  35.             raise TypeError, 'needs string initializer'
  36.         isinstance(format, basestring)
  37.         self.format = format
  38.         self.codec = codec
  39.         self.encoding_errors = encoding_errors
  40.         namechars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  41.         chunks = []
  42.         pos = 0
  43.         while None:
  44.             dollar = string.find(format, '$', pos)
  45.             if dollar < 0:
  46.                 break
  47.             
  48.             nextchar = format[dollar + 1]
  49.             if nextchar == '{':
  50.                 chunks.append((0, format[pos:dollar]))
  51.                 pos = dollar + 2
  52.                 level = 1
  53.                 while level:
  54.                     (match, pos) = matchorfail(format, pos)
  55.                     (tstart, tend) = match.regs[3]
  56.                     token = format[tstart:tend]
  57.                     if token == '{':
  58.                         level = level + 1
  59.                         continue
  60.                     if token == '}':
  61.                         level = level - 1
  62.                         continue
  63.                 chunks.append((1, format[dollar + 2:pos - 1]))
  64.                 continue
  65.             if nextchar in namechars:
  66.                 chunks.append((0, format[pos:dollar]))
  67.                 (match, pos) = matchorfail(format, dollar + 1)
  68.                 while pos < len(format):
  69.                     if format[pos] == '.' and pos + 1 < len(format) and format[pos + 1] in namechars:
  70.                         (match, pos) = matchorfail(format, pos + 1)
  71.                         continue
  72.                     if format[pos] in '([':
  73.                         pos = pos + 1
  74.                         level = 1
  75.                         while level:
  76.                             (match, pos) = matchorfail(format, pos)
  77.                             (tstart, tend) = match.regs[3]
  78.                             token = format[tstart:tend]
  79.                             if token[0] in '([':
  80.                                 level = level + 1
  81.                                 continue
  82.                             if token[0] in ')]':
  83.                                 level = level - 1
  84.                                 continue
  85.                         continue
  86.                     break
  87.                 chunks.append((1, format[dollar + 1:pos]))
  88.                 continue
  89.             chunks.append((0, format[pos:dollar + 1]))
  90.             pos = dollar + 1 + (nextchar == '$')
  91.             continue
  92.             if pos < len(format):
  93.                 chunks.append((0, format[pos:]))
  94.             
  95.         self.chunks = chunks
  96.  
  97.     
  98.     def __repr__(self):
  99.         return '<Itpl %s >' % repr(self.format)
  100.  
  101.     
  102.     def _str(self, glob, loc):
  103.         result = []
  104.         app = result.append
  105.         for live, chunk in self.chunks:
  106.             if live:
  107.                 app(str(eval(chunk, glob, loc)))
  108.                 continue
  109.             app(chunk)
  110.         
  111.         out = ''.join(result)
  112.         
  113.         try:
  114.             return str(out)
  115.         except UnicodeError:
  116.             return out.encode(self.codec, self.encoding_errors)
  117.  
  118.  
  119.     
  120.     def __str__(self):
  121.         frame = sys._getframe(1)
  122.         while frame.f_globals['__name__'] == __name__:
  123.             frame = frame.f_back
  124.         loc = frame.f_locals
  125.         glob = frame.f_globals
  126.         return self._str(glob, loc)
  127.  
  128.  
  129.  
  130. class ItplNS(Itpl):
  131.     
  132.     def __init__(self, format, globals, locals = None, codec = 'utf_8', encoding_errors = 'backslashreplace'):
  133.         if locals is None:
  134.             locals = globals
  135.         
  136.         self.globals = globals
  137.         self.locals = locals
  138.         Itpl.__init__(self, format, codec, encoding_errors)
  139.  
  140.     
  141.     def __str__(self):
  142.         return self._str(self.globals, self.locals)
  143.  
  144.     
  145.     def __repr__(self):
  146.         return '<ItplNS %s >' % repr(self.format)
  147.  
  148.  
  149.  
  150. def itpl(text):
  151.     return str(Itpl(text))
  152.  
  153.  
  154. def printpl(text):
  155.     print itpl(text)
  156.  
  157.  
  158. def itplns(text, globals, locals = None):
  159.     return str(ItplNS(text, globals, locals))
  160.  
  161.  
  162. def printplns(text, globals, locals = None):
  163.     print itplns(text, globals, locals)
  164.  
  165.  
  166. class ItplFile:
  167.     
  168.     def __init__(self, file):
  169.         self.file = file
  170.  
  171.     
  172.     def __repr__(self):
  173.         return '<interpolated ' + repr(self.file) + '>'
  174.  
  175.     
  176.     def __getattr__(self, attr):
  177.         return getattr(self.file, attr)
  178.  
  179.     
  180.     def write(self, text):
  181.         self.file.write(str(Itpl(text)))
  182.  
  183.  
  184.  
  185. def filter(file = sys.stdout):
  186.     return ItplFile(file)
  187.  
  188.  
  189. def unfilter(ifile = None):
  190.     if not ifile or ifile.file:
  191.         pass
  192.     return sys.stdout.file
  193.  
  194.