home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / shlex.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  6.6 KB  |  282 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         if instream is not None:
  23.             self.instream = instream
  24.             self.infile = infile
  25.         else:
  26.             self.instream = sys.stdin
  27.             self.infile = None
  28.         self.posix = posix
  29.         if posix:
  30.             self.eof = None
  31.         else:
  32.             self.eof = ''
  33.         self.commenters = '#'
  34.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  35.         if self.posix:
  36.             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'
  37.         self.whitespace = ' \t\r\n'
  38.         self.whitespace_split = False
  39.         self.quotes = '\'"'
  40.         self.escape = '\\'
  41.         self.escapedquotes = '"'
  42.         self.state = ' '
  43.         self.pushback = deque()
  44.         self.lineno = 1
  45.         self.debug = 0
  46.         self.token = ''
  47.         self.filestack = deque()
  48.         self.source = None
  49.         if self.debug:
  50.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  51.  
  52.     
  53.     def push_token(self, tok):
  54.         if self.debug >= 1:
  55.             print 'shlex: pushing token ' + repr(tok)
  56.         self.pushback.appendleft(tok)
  57.  
  58.     
  59.     def push_source(self, newstream, newfile = None):
  60.         if isinstance(newstream, basestring):
  61.             newstream = StringIO(newstream)
  62.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  63.         self.infile = newfile
  64.         self.instream = newstream
  65.         self.lineno = 1
  66.         if self.debug:
  67.             if newfile is not None:
  68.                 print 'shlex: pushing to file %s' % (self.infile,)
  69.             else:
  70.                 print 'shlex: pushing to stream %s' % (self.instream,)
  71.  
  72.     
  73.     def pop_source(self):
  74.         self.instream.close()
  75.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  76.         if self.debug:
  77.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  78.         self.state = ' '
  79.  
  80.     
  81.     def get_token(self):
  82.         if self.pushback:
  83.             tok = self.pushback.popleft()
  84.             if self.debug >= 1:
  85.                 print 'shlex: popping token ' + repr(tok)
  86.             return tok
  87.         raw = None.read_token()
  88.         if self.source is not None:
  89.             while raw == self.source:
  90.                 spec = self.sourcehook(self.read_token())
  91.                 if spec:
  92.                     (newfile, newstream) = spec
  93.                     self.push_source(newstream, newfile)
  94.                 raw = self.get_token()
  95.         while raw == self.eof:
  96.             if not self.filestack:
  97.                 return self.eof
  98.             None.pop_source()
  99.             raw = self.get_token()
  100.         if self.debug >= 1:
  101.             if raw != self.eof:
  102.                 print 'shlex: token=' + repr(raw)
  103.             else:
  104.                 print 'shlex: token=EOF'
  105.         return raw
  106.  
  107.     
  108.     def read_token(self):
  109.         quoted = False
  110.         escapedstate = ' '
  111.         while True:
  112.             nextchar = self.instream.read(1)
  113.             if nextchar == '\n':
  114.                 self.lineno = self.lineno + 1
  115.             if self.debug >= 3:
  116.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  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.                     if not self.token:
  129.                         if self.posix and quoted:
  130.                             break
  131.                         
  132.                     
  133.                 if nextchar in self.commenters:
  134.                     self.instream.readline()
  135.                     self.lineno = self.lineno + 1
  136.                 elif self.posix and nextchar in self.escape:
  137.                     escapedstate = 'a'
  138.                     self.state = nextchar
  139.                 elif nextchar in self.wordchars:
  140.                     self.token = nextchar
  141.                     self.state = 'a'
  142.                 elif nextchar in self.quotes:
  143.                     if not self.posix:
  144.                         self.token = nextchar
  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 not self.token:
  152.                         if self.posix and quoted:
  153.                             break
  154.                         
  155.                         continue
  156.                         if self.state in self.quotes:
  157.                             quoted = True
  158.                             if not nextchar:
  159.                                 if self.debug >= 2:
  160.                                     print 'shlex: I see EOF in quotes state'
  161.                                 raise ValueError, 'No closing quotation'
  162.                             if nextchar == self.state:
  163.                                 if not self.posix:
  164.                                     self.token = self.token + nextchar
  165.                                     self.state = ' '
  166.                                     break
  167.                                 else:
  168.                                     self.state = 'a'
  169.                             elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  170.                                 escapedstate = self.state
  171.                                 self.state = nextchar
  172.                             else:
  173.                                 self.token = self.token + nextchar
  174.             if self.state in self.escape:
  175.                 if not nextchar:
  176.                     if self.debug >= 2:
  177.                         print 'shlex: I see EOF in escape state'
  178.                     raise ValueError, 'No escaped character'
  179.                 if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate:
  180.                     self.token = self.token + self.state
  181.                 self.token = self.token + nextchar
  182.                 self.state = escapedstate
  183.                 continue
  184.             if not self.state == 'a' or nextchar:
  185.                 self.state = None
  186.                 break
  187.             elif nextchar in self.whitespace:
  188.                 if self.debug >= 2:
  189.                     print 'shlex: I see whitespace in word state'
  190.                 self.state = ' '
  191.                 if not self.token:
  192.                     if self.posix and quoted:
  193.                         break
  194.                     
  195.                 
  196.             if nextchar in self.commenters:
  197.                 self.instream.readline()
  198.                 self.lineno = self.lineno + 1
  199.                 if self.posix:
  200.                     self.state = ' '
  201.                     if not self.token:
  202.                         if self.posix and quoted:
  203.                             break
  204.                         
  205.                     
  206.                 
  207.             if self.posix and nextchar in self.quotes:
  208.                 self.state = nextchar
  209.             elif self.posix and nextchar in self.escape:
  210.                 escapedstate = 'a'
  211.                 self.state = nextchar
  212.             elif nextchar in self.wordchars and nextchar in self.quotes or self.whitespace_split:
  213.                 self.token = self.token + nextchar
  214.             else:
  215.                 self.pushback.appendleft(nextchar)
  216.                 if self.debug >= 2:
  217.                     print 'shlex: I see punctuation in word state'
  218.                 self.state = ' '
  219.                 if self.token:
  220.                     break
  221.                 
  222.         result = self.token
  223.         self.token = ''
  224.         if self.posix and not quoted and result == '':
  225.             result = None
  226.         if self.debug > 1:
  227.             if result:
  228.                 print 'shlex: raw token=' + repr(result)
  229.             else:
  230.                 print 'shlex: raw token=EOF'
  231.         return result
  232.  
  233.     
  234.     def sourcehook(self, newfile):
  235.         if newfile[0] == '"':
  236.             newfile = newfile[1:-1]
  237.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  238.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  239.         return (newfile, open(newfile, 'r'))
  240.  
  241.     
  242.     def error_leader(self, infile = None, lineno = None):
  243.         if infile is None:
  244.             infile = self.infile
  245.         if lineno is None:
  246.             lineno = self.lineno
  247.         return '"%s", line %d: ' % (infile, lineno)
  248.  
  249.     
  250.     def __iter__(self):
  251.         return self
  252.  
  253.     
  254.     def next(self):
  255.         token = self.get_token()
  256.         if token == self.eof:
  257.             raise StopIteration
  258.         return token
  259.  
  260.  
  261.  
  262. def split(s, comments = False, posix = True):
  263.     lex = shlex(s, posix = posix)
  264.     lex.whitespace_split = True
  265.     if not comments:
  266.         lex.commenters = ''
  267.     return list(lex)
  268.  
  269. if __name__ == '__main__':
  270.     if len(sys.argv) == 1:
  271.         lexer = shlex()
  272.     else:
  273.         file = sys.argv[1]
  274.         lexer = shlex(open(file), file)
  275.     while None:
  276.         tt = lexer.get_token()
  277.         if tt:
  278.             print 'Token: ' + repr(tt)
  279.             continue
  280.         break
  281.         continue
  282.