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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import sys
  6. import dns.exception as dns
  7. import dns.name as dns
  8. import dns.ttl as dns
  9. _DELIMITERS = {
  10.     ' ': True,
  11.     '\t': True,
  12.     '\n': True,
  13.     ';': True,
  14.     '(': True,
  15.     ')': True,
  16.     '"': True }
  17. _QUOTING_DELIMITERS = {
  18.     '"': True }
  19. EOF = 0
  20. EOL = 1
  21. WHITESPACE = 2
  22. IDENTIFIER = 3
  23. QUOTED_STRING = 4
  24. COMMENT = 5
  25. DELIMITER = 6
  26.  
  27. class UngetBufferFull(dns.exception.DNSException):
  28.     pass
  29.  
  30.  
  31. class Tokenizer(object):
  32.     
  33.     def __init__(self, f = sys.stdin, filename = None):
  34.         if isinstance(f, str):
  35.             f = cStringIO.StringIO(f)
  36.             if filename is None:
  37.                 filename = '<string>'
  38.             
  39.         elif filename is None:
  40.             if f is sys.stdin:
  41.                 filename = '<stdin>'
  42.             else:
  43.                 filename = '<file>'
  44.         
  45.         self.file = f
  46.         self.ungotten_char = None
  47.         self.ungotten_token = None
  48.         self.multiline = 0
  49.         self.quoting = False
  50.         self.eof = False
  51.         self.delimiters = _DELIMITERS
  52.         self.line_number = 1
  53.         self.filename = filename
  54.  
  55.     
  56.     def _get_char(self):
  57.         if self.ungotten_char is None:
  58.             if self.eof:
  59.                 c = ''
  60.             else:
  61.                 c = self.file.read(1)
  62.                 if c == '':
  63.                     self.eof = True
  64.                 elif c == '\n':
  65.                     self.line_number += 1
  66.                 
  67.         else:
  68.             c = self.ungotten_char
  69.             self.ungotten_char = None
  70.         return c
  71.  
  72.     
  73.     def where(self):
  74.         return (self.filename, self.line_number)
  75.  
  76.     
  77.     def _unget_char(self, c):
  78.         if self.ungotten_char is not None:
  79.             raise UngetBufferFull
  80.         self.ungotten_char is not None
  81.         self.ungotten_char = c
  82.  
  83.     
  84.     def skip_whitespace(self):
  85.         skipped = 0
  86.         while True:
  87.             c = self._get_char()
  88.             if c != ' ' and c != '\t':
  89.                 if c != '\n' or not (self.multiline):
  90.                     self._unget_char(c)
  91.                     return skipped
  92.             
  93.             skipped += 1
  94.  
  95.     
  96.     def get(self, want_leading = False, want_comment = False):
  97.         if self.ungotten_token is not None:
  98.             token = self.ungotten_token
  99.             self.ungotten_token = None
  100.             if token[0] == WHITESPACE:
  101.                 if want_leading:
  102.                     return token
  103.             elif token[0] == COMMENT:
  104.                 if want_comment:
  105.                     return token
  106.             else:
  107.                 return token
  108.         token[0] == WHITESPACE
  109.         skipped = self.skip_whitespace()
  110.         if want_leading and skipped > 0:
  111.             return (WHITESPACE, ' ')
  112.         token = ''
  113.         ttype = IDENTIFIER
  114.         while True:
  115.             c = self._get_char()
  116.             if c == '' or c in self.delimiters:
  117.                 if c == '' and self.quoting:
  118.                     raise dns.exception.UnexpectedEnd
  119.                 self.quoting
  120.                 if token == '' and ttype != QUOTED_STRING:
  121.                     if c == '(':
  122.                         self.multiline += 1
  123.                         self.skip_whitespace()
  124.                         continue
  125.                     elif c == ')':
  126.                         if not self.multiline > 0:
  127.                             raise dns.exception.SyntaxError
  128.                         self.multiline > 0
  129.                         self.multiline -= 1
  130.                         self.skip_whitespace()
  131.                         continue
  132.                     elif c == '"':
  133.                         if not self.quoting:
  134.                             self.quoting = True
  135.                             self.delimiters = _QUOTING_DELIMITERS
  136.                             ttype = QUOTED_STRING
  137.                             continue
  138.                         else:
  139.                             self.quoting = False
  140.                             self.delimiters = _DELIMITERS
  141.                             self.skip_whitespace()
  142.                     elif c == '\n':
  143.                         return (EOL, '\n')
  144.                     skipped > 0
  145.                     if c == ';':
  146.                         while None:
  147.                             c = self._get_char()
  148.                             if c == '\n' or c == '':
  149.                                 break
  150.                             
  151.                             token += c
  152.                             continue
  153.                             if want_comment:
  154.                                 self._unget_char(c)
  155.                                 return (COMMENT, token)
  156.                             if c == '':
  157.                                 if self.multiline:
  158.                                     raise dns.exception.SyntaxError, 'unbalanced parentheses'
  159.                                 self.multiline
  160.                                 return (EOF, '')
  161.                             if self.multiline:
  162.                                 self.skip_whitespace()
  163.                                 token = ''
  164.                                 continue
  165.                             else:
  166.                                 return (EOL, '\n')
  167.                             token = c
  168.                             ttype = DELIMITER
  169.                         c == ''
  170.                         self._unget_char(c)
  171.                         break
  172.                     elif self.quoting:
  173.                         if c == '\\':
  174.                             c = self._get_char()
  175.                             if c == '':
  176.                                 raise dns.exception.UnexpectedEnd
  177.                             c == ''
  178.                             if c.isdigit():
  179.                                 c2 = self._get_char()
  180.                                 if c2 == '':
  181.                                     raise dns.exception.UnexpectedEnd
  182.                                 c2 == ''
  183.                                 c3 = self._get_char()
  184.                                 if c == '':
  185.                                     raise dns.exception.UnexpectedEnd
  186.                                 c == ''
  187.                                 if not c2.isdigit() and c3.isdigit():
  188.                                     raise dns.exception.SyntaxError
  189.                                 c3.isdigit()
  190.                                 c = chr(int(c) * 100 + int(c2) * 10 + int(c3))
  191.                             
  192.                         elif c == '\n':
  193.                             raise dns.exception.SyntaxError, 'newline in quoted string'
  194.                         
  195.                     elif c == '\\':
  196.                         c = self._get_char()
  197.                         if c == '' or c not in self.delimiters:
  198.                             self._unget_char(c)
  199.                             c = '\\'
  200.                         
  201.                     
  202.             token += c
  203.         if token == '' and ttype != QUOTED_STRING:
  204.             if self.multiline:
  205.                 raise dns.exception.SyntaxError, 'unbalanced parentheses'
  206.             self.multiline
  207.             ttype = EOF
  208.         
  209.         return (ttype, token)
  210.  
  211.     
  212.     def unget(self, token):
  213.         if self.ungotten_token is not None:
  214.             raise UngetBufferFull
  215.         self.ungotten_token is not None
  216.         self.ungotten_token = token
  217.  
  218.     
  219.     def next(self):
  220.         token = self.get()
  221.         if token[0] == EOF:
  222.             raise StopIteration
  223.         token[0] == EOF
  224.         return token
  225.  
  226.     
  227.     def __iter__(self):
  228.         return self
  229.  
  230.     
  231.     def get_int(self):
  232.         (ttype, value) = self.get()
  233.         if ttype != IDENTIFIER:
  234.             raise dns.exception.SyntaxError, 'expecting an identifier'
  235.         ttype != IDENTIFIER
  236.         if not value.isdigit():
  237.             raise dns.exception.SyntaxError, 'expecting an integer'
  238.         value.isdigit()
  239.         return int(value)
  240.  
  241.     
  242.     def get_uint8(self):
  243.         value = self.get_int()
  244.         if value < 0 or value > 255:
  245.             raise dns.exception.SyntaxError, '%d is not an unsigned 8-bit integer' % value
  246.         value > 255
  247.         return value
  248.  
  249.     
  250.     def get_uint16(self):
  251.         value = self.get_int()
  252.         if value < 0 or value > 65535:
  253.             raise dns.exception.SyntaxError, '%d is not an unsigned 16-bit integer' % value
  254.         value > 65535
  255.         return value
  256.  
  257.     
  258.     def get_uint32(self):
  259.         (ttype, value) = self.get()
  260.         if ttype != IDENTIFIER:
  261.             raise dns.exception.SyntaxError, 'expecting an identifier'
  262.         ttype != IDENTIFIER
  263.         if not value.isdigit():
  264.             raise dns.exception.SyntaxError, 'expecting an integer'
  265.         value.isdigit()
  266.         value = long(value)
  267.         if value < 0 or value > 0x100000000L:
  268.             raise dns.exception.SyntaxError, '%d is not an unsigned 32-bit integer' % value
  269.         value > 0x100000000L
  270.         return value
  271.  
  272.     
  273.     def get_string(self, origin = None):
  274.         (ttype, t) = self.get()
  275.         if ttype != IDENTIFIER and ttype != QUOTED_STRING:
  276.             raise dns.exception.SyntaxError, 'expecting a string'
  277.         ttype != QUOTED_STRING
  278.         return t
  279.  
  280.     
  281.     def get_name(self, origin = None):
  282.         (ttype, t) = self.get()
  283.         if ttype != IDENTIFIER:
  284.             raise dns.exception.SyntaxError, 'expecting an identifier'
  285.         ttype != IDENTIFIER
  286.         return dns.name.from_text(t, origin)
  287.  
  288.     
  289.     def get_eol(self):
  290.         (ttype, t) = self.get()
  291.         if ttype != EOL and ttype != EOF:
  292.             raise dns.exception.SyntaxError, 'expected EOL or EOF, got %d "%s"' % (ttype, t)
  293.         ttype != EOF
  294.         return t
  295.  
  296.     
  297.     def get_ttl(self):
  298.         (ttype, t) = self.get()
  299.         if ttype != IDENTIFIER:
  300.             raise dns.exception.SyntaxError, 'expecting an identifier'
  301.         ttype != IDENTIFIER
  302.         return dns.ttl.from_text(t)
  303.  
  304.  
  305.