home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __author__ = 'Ka-Ping Yee <ping@lfw.org>'
- __license__ = 'MIT'
- import string
- import sys
- from tokenize import tokenprog
- from types import StringType
-
- class ItplError(ValueError):
-
- def __init__(self, text, pos):
- self.text = text
- self.pos = pos
-
-
- def __str__(self):
- return 'unfinished expression in %s at char %d' % (repr(self.text), self.pos)
-
-
-
- def matchorfail(text, pos):
- match = tokenprog.match(text, pos)
- if match is None:
- raise ItplError(text, pos)
- match is None
- return (match, match.end())
-
-
- class Itpl:
-
- def __init__(self, format, codec = 'utf_8', encoding_errors = 'backslashreplace'):
- if not isinstance(format, basestring):
- raise TypeError, 'needs string initializer'
- isinstance(format, basestring)
- self.format = format
- self.codec = codec
- self.encoding_errors = encoding_errors
- namechars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
- chunks = []
- pos = 0
- while None:
- dollar = string.find(format, '$', pos)
- if dollar < 0:
- break
-
- nextchar = format[dollar + 1]
- if nextchar == '{':
- chunks.append((0, format[pos:dollar]))
- pos = dollar + 2
- level = 1
- while level:
- (match, pos) = matchorfail(format, pos)
- (tstart, tend) = match.regs[3]
- token = format[tstart:tend]
- if token == '{':
- level = level + 1
- continue
- if token == '}':
- level = level - 1
- continue
- chunks.append((1, format[dollar + 2:pos - 1]))
- continue
- if nextchar in namechars:
- chunks.append((0, format[pos:dollar]))
- (match, pos) = matchorfail(format, dollar + 1)
- while pos < len(format):
- if format[pos] == '.' and pos + 1 < len(format) and format[pos + 1] in namechars:
- (match, pos) = matchorfail(format, pos + 1)
- continue
- if format[pos] in '([':
- pos = pos + 1
- level = 1
- while level:
- (match, pos) = matchorfail(format, pos)
- (tstart, tend) = match.regs[3]
- token = format[tstart:tend]
- if token[0] in '([':
- level = level + 1
- continue
- if token[0] in ')]':
- level = level - 1
- continue
- continue
- break
- chunks.append((1, format[dollar + 1:pos]))
- continue
- chunks.append((0, format[pos:dollar + 1]))
- pos = dollar + 1 + (nextchar == '$')
- continue
- if pos < len(format):
- chunks.append((0, format[pos:]))
-
- self.chunks = chunks
-
-
- def __repr__(self):
- return '<Itpl %s >' % repr(self.format)
-
-
- def _str(self, glob, loc):
- result = []
- app = result.append
- for live, chunk in self.chunks:
- if live:
- app(str(eval(chunk, glob, loc)))
- continue
- app(chunk)
-
- out = ''.join(result)
-
- try:
- return str(out)
- except UnicodeError:
- return out.encode(self.codec, self.encoding_errors)
-
-
-
- def __str__(self):
- frame = sys._getframe(1)
- while frame.f_globals['__name__'] == __name__:
- frame = frame.f_back
- loc = frame.f_locals
- glob = frame.f_globals
- return self._str(glob, loc)
-
-
-
- class ItplNS(Itpl):
-
- def __init__(self, format, globals, locals = None, codec = 'utf_8', encoding_errors = 'backslashreplace'):
- if locals is None:
- locals = globals
-
- self.globals = globals
- self.locals = locals
- Itpl.__init__(self, format, codec, encoding_errors)
-
-
- def __str__(self):
- return self._str(self.globals, self.locals)
-
-
- def __repr__(self):
- return '<ItplNS %s >' % repr(self.format)
-
-
-
- def itpl(text):
- return str(Itpl(text))
-
-
- def printpl(text):
- print itpl(text)
-
-
- def itplns(text, globals, locals = None):
- return str(ItplNS(text, globals, locals))
-
-
- def printplns(text, globals, locals = None):
- print itplns(text, globals, locals)
-
-
- class ItplFile:
-
- def __init__(self, file):
- self.file = file
-
-
- def __repr__(self):
- return '<interpolated ' + repr(self.file) + '>'
-
-
- def __getattr__(self, attr):
- return getattr(self.file, attr)
-
-
- def write(self, text):
- self.file.write(str(Itpl(text)))
-
-
-
- def filter(file = sys.stdout):
- return ItplFile(file)
-
-
- def unfilter(ifile = None):
- if not ifile or ifile.file:
- pass
- return sys.stdout.file
-
-