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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os.path as os
  5. import sys
  6. from collections import deque
  7.  
  8. try:
  9.     from cStringIO import StringIO
  10. except ImportError:
  11.     from StringIO import StringIO
  12.  
  13. __all__ = [
  14.     'shlex',
  15.     'split']
  16.  
  17. class shlex:
  18.     
  19.     def __init__(self, instream = None, infile = None, posix = False):
  20.         if isinstance(instream, basestring):
  21.             instream = StringIO(instream)
  22.         
  23.         if instream is not None:
  24.             self.instream = instream
  25.             self.infile = infile
  26.         else:
  27.             self.instream = sys.stdin
  28.             self.infile = None
  29.         self.posix = posix
  30.         if posix:
  31.             self.eof = None
  32.         else:
  33.             self.eof = ''
  34.         self.commenters = '#'
  35.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  36.         if self.posix:
  37.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  38.         
  39.         self.whitespace = ' \t\r\n'
  40.         self.whitespace_split = False
  41.         self.quotes = '\'"'
  42.         self.escape = '\\'
  43.         self.escapedquotes = '"'
  44.         self.state = ' '
  45.         self.pushback = deque()
  46.         self.lineno = 1
  47.         self.debug = 0
  48.         self.token = ''
  49.         self.filestack = deque()
  50.         self.source = None
  51.         if self.debug:
  52.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  53.         
  54.  
  55.     
  56.     def push_token(self, tok):
  57.         if self.debug >= 1:
  58.             print 'shlex: pushing token ' + repr(tok)
  59.         
  60.         self.pushback.appendleft(tok)
  61.  
  62.     
  63.     def push_source(self, newstream, newfile = None):
  64.         if isinstance(newstream, basestring):
  65.             newstream = StringIO(newstream)
  66.         
  67.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  68.         self.infile = newfile
  69.         self.instream = newstream
  70.         self.lineno = 1
  71.         if self.debug:
  72.             if newfile is not None:
  73.                 print 'shlex: pushing to file %s' % (self.infile,)
  74.             else:
  75.                 print 'shlex: pushing to stream %s' % (self.instream,)
  76.         
  77.  
  78.     
  79.     def pop_source(self):
  80.         self.instream.close()
  81.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  82.         if self.debug:
  83.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  84.         
  85.         self.state = ' '
  86.  
  87.     
  88.     def get_token(self):
  89.         if self.pushback:
  90.             tok = self.pushback.popleft()
  91.             if self.debug >= 1:
  92.                 print 'shlex: popping token ' + repr(tok)
  93.             
  94.             return tok
  95.         raw = self.read_token()
  96.         while raw == self.eof:
  97.             if not self.filestack:
  98.                 return self.eof
  99.             self.pop_source()
  100.             raw = self.get_token()
  101.             continue
  102.             self.filestack
  103.         return raw
  104.  
  105.     
  106.     def read_token(self):
  107.         quoted = False
  108.         escapedstate = ' '
  109.         while True:
  110.             nextchar = self.instream.read(1)
  111.             if nextchar == '\n':
  112.                 self.lineno = self.lineno + 1
  113.             
  114.             if self.debug >= 3:
  115.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  116.             
  117.             if self.state is None:
  118.                 self.token = ''
  119.                 break
  120.                 continue
  121.             if self.state == ' ':
  122.                 if not nextchar:
  123.                     self.state = None
  124.                     break
  125.                 elif nextchar in self.whitespace:
  126.                     if self.debug >= 2:
  127.                         print 'shlex: I see whitespace in whitespace state'
  128.                     
  129.                     if (self.token or self.posix) and quoted:
  130.                         break
  131.                     
  132.                 elif nextchar in self.commenters:
  133.                     self.instream.readline()
  134.                     self.lineno = self.lineno + 1
  135.                 elif self.posix and nextchar in self.escape:
  136.                     escapedstate = 'a'
  137.                     self.state = nextchar
  138.                 elif nextchar in self.wordchars:
  139.                     self.token = nextchar
  140.                     self.state = 'a'
  141.                 elif nextchar in self.quotes:
  142.                     if not self.posix:
  143.                         self.token = nextchar
  144.                     
  145.                     self.state = nextchar
  146.                 elif self.whitespace_split:
  147.                     self.token = nextchar
  148.                     self.state = 'a'
  149.                 else:
  150.                     self.token = nextchar
  151.                     if (self.token or self.posix) and quoted:
  152.                         break
  153.                     
  154.             nextchar in self.escape
  155.             if self.state in self.quotes:
  156.                 quoted = True
  157.                 if not nextchar:
  158.                     if self.debug >= 2:
  159.                         print 'shlex: I see EOF in quotes state'
  160.                     
  161.                     raise ValueError, 'No closing quotation'
  162.                 nextchar
  163.                 if nextchar == self.state:
  164.                     if not self.posix:
  165.                         self.token = self.token + nextchar
  166.                         self.state = ' '
  167.                         break
  168.                     else:
  169.                         self.state = 'a'
  170.                 elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  171.                     escapedstate = self.state
  172.                     self.state = nextchar
  173.                 else:
  174.                     self.token = self.token + nextchar
  175.             self.state in self.escapedquotes
  176.             if self.state in self.escape:
  177.                 if not nextchar:
  178.                     if self.debug >= 2:
  179.                         print 'shlex: I see EOF in escape state'
  180.                     
  181.                     raise ValueError, 'No escaped character'
  182.                 nextchar
  183.                 if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate:
  184.                     self.token = self.token + self.state
  185.                 
  186.                 self.token = self.token + nextchar
  187.                 self.state = escapedstate
  188.                 continue
  189.             if self.state == 'a':
  190.                 if not nextchar:
  191.                     self.state = None
  192.                     break
  193.                 elif nextchar in self.whitespace:
  194.                     if self.debug >= 2:
  195.                         print 'shlex: I see whitespace in word state'
  196.                     
  197.                     self.state = ' '
  198.                     if (self.token or self.posix) and quoted:
  199.                         break
  200.                     
  201.                 elif nextchar in self.commenters:
  202.                     self.instream.readline()
  203.                     self.lineno = self.lineno + 1
  204.                     if self.posix:
  205.                         self.state = ' '
  206.                         if (self.token or self.posix) and quoted:
  207.                             break
  208.                         
  209.                     
  210.                 elif self.posix and nextchar in self.quotes:
  211.                     self.state = nextchar
  212.                 elif self.posix and nextchar in self.escape:
  213.                     escapedstate = 'a'
  214.                     self.state = nextchar
  215.                 elif nextchar in self.wordchars and nextchar in self.quotes or self.whitespace_split:
  216.                     self.token = self.token + nextchar
  217.                 else:
  218.                     self.pushback.appendleft(nextchar)
  219.                     if self.debug >= 2:
  220.                         print 'shlex: I see punctuation in word state'
  221.                     
  222.                     self.state = ' '
  223.                     if self.token:
  224.                         break
  225.                     
  226.             self.whitespace_split
  227.         result = self.token
  228.         self.token = ''
  229.         if self.posix and not quoted and result == '':
  230.             result = None
  231.         
  232.         if self.debug > 1:
  233.             if result:
  234.                 print 'shlex: raw token=' + repr(result)
  235.             else:
  236.                 print 'shlex: raw token=EOF'
  237.         
  238.         return result
  239.  
  240.     
  241.     def sourcehook(self, newfile):
  242.         if newfile[0] == '"':
  243.             newfile = newfile[1:-1]
  244.         
  245.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  246.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  247.         
  248.         return (newfile, open(newfile, 'r'))
  249.  
  250.     
  251.     def error_leader(self, infile = None, lineno = None):
  252.         if infile is None:
  253.             infile = self.infile
  254.         
  255.         if lineno is None:
  256.             lineno = self.lineno
  257.         
  258.         return '"%s", line %d: ' % (infile, lineno)
  259.  
  260.     
  261.     def __iter__(self):
  262.         return self
  263.  
  264.     
  265.     def next(self):
  266.         token = self.get_token()
  267.         if token == self.eof:
  268.             raise StopIteration
  269.         token == self.eof
  270.         return token
  271.  
  272.  
  273.  
  274. def split(s, comments = False, posix = True):
  275.     lex = shlex(s, posix = posix)
  276.     lex.whitespace_split = True
  277.     if not comments:
  278.         lex.commenters = ''
  279.     
  280.     return list(lex)
  281.  
  282. if __name__ == '__main__':
  283.     if len(sys.argv) == 1:
  284.         lexer = shlex()
  285.     else:
  286.         file = sys.argv[1]
  287.         lexer = shlex(open(file), file)
  288.     while None:
  289.         tt = lexer.get_token()
  290.         if tt:
  291.             print 'Token: ' + repr(tt)
  292.             continue
  293.         break
  294.         continue
  295. __name__ == '__main__'
  296.