home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / simplejson / decoder.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  7.2 KB  |  312 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import re
  5. import sys
  6. import struct
  7. from simplejson.scanner import make_scanner
  8.  
  9. try:
  10.     from _jsonspeedups import scanstring as c_scanstring
  11. except ImportError:
  12.     c_scanstring = None
  13.  
  14. __all__ = [
  15.     'JSONDecoder']
  16. FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
  17.  
  18. def _floatconstants():
  19.     _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
  20.     if sys.byteorder != 'big':
  21.         _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
  22.     
  23.     (nan, inf) = struct.unpack('dd', _BYTES)
  24.     return (nan, inf, -inf)
  25.  
  26. (NaN, PosInf, NegInf) = _floatconstants()
  27.  
  28. def linecol(doc, pos):
  29.     lineno = doc.count('\n', 0, pos) + 1
  30.     if lineno == 1:
  31.         colno = pos
  32.     else:
  33.         colno = pos - doc.rindex('\n', 0, pos)
  34.     return (lineno, colno)
  35.  
  36.  
  37. def errmsg(msg, doc, pos, end = None):
  38.     (lineno, colno) = linecol(doc, pos)
  39.     if end is None:
  40.         return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
  41.     (endlineno, endcolno) = linecol(doc, end)
  42.     return '%s: line %d column %d - line %d column %d (char %d - %d)' % (msg, lineno, colno, endlineno, endcolno, pos, end)
  43.  
  44. _CONSTANTS = {
  45.     '-Infinity': NegInf,
  46.     'Infinity': PosInf,
  47.     'NaN': NaN }
  48. STRINGCHUNK = re.compile('(.*?)(["\\\\\\x00-\\x1f])', FLAGS)
  49. BACKSLASH = {
  50.     '"': u'"',
  51.     '\\': u'\\',
  52.     '/': u'/',
  53.     'b': u'\x08',
  54.     'f': u'\x0c',
  55.     'n': u'\n',
  56.     'r': u'\r',
  57.     't': u'\t' }
  58. DEFAULT_ENCODING = 'utf-8'
  59.  
  60. def py_scanstring(s, end, encoding = None, strict = True, _b = BACKSLASH, _m = STRINGCHUNK.match):
  61.     if encoding is None:
  62.         encoding = DEFAULT_ENCODING
  63.     
  64.     chunks = []
  65.     _append = chunks.append
  66.     begin = end - 1
  67.     while None:
  68.         chunk = _m(s, end)
  69.         if chunk is None:
  70.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  71.         end = chunk.end()
  72.         (content, terminator) = chunk.groups()
  73.         if content:
  74.             if not isinstance(content, unicode):
  75.                 content = unicode(content, encoding)
  76.             
  77.             _append(content)
  78.         
  79.         if terminator == '"':
  80.             break
  81.         elif terminator != '\\':
  82.             if strict:
  83.                 raise ValueError(errmsg('Invalid control character %r at', s, end))
  84.             strict
  85.             _append(terminator)
  86.             continue
  87.         
  88.         
  89.         try:
  90.             esc = s[end]
  91.         except IndexError:
  92.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  93.  
  94.         if esc != 'u':
  95.             
  96.             try:
  97.                 m = _b[esc]
  98.             except KeyError:
  99.                 raise ValueError(errmsg('Invalid \\escape: %r' % (esc,), s, end))
  100.  
  101.             end += 1
  102.         else:
  103.             esc = s[end + 1:end + 5]
  104.             next_end = end + 5
  105.             msg = 'Invalid \\uXXXX escape'
  106.             
  107.             try:
  108.                 if len(esc) != 4:
  109.                     raise ValueError
  110.                 len(esc) != 4
  111.                 uni = int(esc, 16)
  112.                 if uni <= uni:
  113.                     pass
  114.                 elif uni <= 56319 and sys.maxunicode > 65535:
  115.                     msg = 'Invalid \\uXXXX\\uXXXX surrogate pair'
  116.                     if not s[end + 5:end + 7] == '\\u':
  117.                         raise ValueError
  118.                     s[end + 5:end + 7] == '\\u'
  119.                     esc2 = s[end + 7:end + 11]
  120.                     if len(esc2) != 4:
  121.                         raise ValueError
  122.                     len(esc2) != 4
  123.                     uni2 = int(esc2, 16)
  124.                     uni = 65536 + (uni - 55296 << 10 | uni2 - 56320)
  125.                     next_end += 6
  126.                 
  127.                 m = unichr(uni)
  128.             except ValueError:
  129.                 raise ValueError(errmsg(msg, s, end))
  130.  
  131.             end = next_end
  132.         _append(m)
  133.         continue
  134.         return (u''.join(chunks), end)
  135.  
  136. if not c_scanstring:
  137.     pass
  138. scanstring = py_scanstring
  139. WHITESPACE = re.compile('[ \\t\\n\\r]*', FLAGS)
  140. WHITESPACE_STR = ' \t\n\r'
  141.  
  142. def JSONObject(.0, encoding, strict, scan_once, object_hook, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  143.     (s, end) = .0
  144.     pairs = { }
  145.     nextchar = s[end:end + 1]
  146.     if nextchar != '"':
  147.         if nextchar in _ws:
  148.             end = _w(s, end).end()
  149.             nextchar = s[end:end + 1]
  150.         
  151.         if nextchar == '}':
  152.             return (pairs, end + 1)
  153.         if nextchar != '"':
  154.             raise ValueError(errmsg('Expecting property name', s, end))
  155.         nextchar != '"'
  156.     
  157.     end += 1
  158.     while True:
  159.         (key, end) = scanstring(s, end, encoding, strict)
  160.         if s[end:end + 1] != ':':
  161.             end = _w(s, end).end()
  162.             if s[end:end + 1] != ':':
  163.                 raise ValueError(errmsg('Expecting : delimiter', s, end))
  164.             s[end:end + 1] != ':'
  165.         
  166.         end += 1
  167.         
  168.         try:
  169.             if s[end] in _ws:
  170.                 end += 1
  171.                 if s[end] in _ws:
  172.                     end = _w(s, end + 1).end()
  173.                 
  174.         except IndexError:
  175.             pass
  176.  
  177.         
  178.         try:
  179.             (value, end) = scan_once(s, end)
  180.         except StopIteration:
  181.             raise ValueError(errmsg('Expecting object', s, end))
  182.  
  183.         pairs[key] = value
  184.         
  185.         try:
  186.             nextchar = s[end]
  187.             if nextchar in _ws:
  188.                 end = _w(s, end + 1).end()
  189.                 nextchar = s[end]
  190.         except IndexError:
  191.             nextchar = ''
  192.  
  193.         end += 1
  194.         if nextchar == '}':
  195.             break
  196.         elif nextchar != ',':
  197.             raise ValueError(errmsg('Expecting , delimiter', s, end - 1))
  198.         
  199.         
  200.         try:
  201.             nextchar = s[end]
  202.             if nextchar in _ws:
  203.                 end += 1
  204.                 nextchar = s[end]
  205.                 if nextchar in _ws:
  206.                     end = _w(s, end + 1).end()
  207.                     nextchar = s[end]
  208.                 
  209.         except IndexError:
  210.             nextchar = ''
  211.  
  212.         end += 1
  213.         if nextchar != '"':
  214.             raise ValueError(errmsg('Expecting property name', s, end - 1))
  215.         nextchar != '"'
  216.     if object_hook is not None:
  217.         pairs = object_hook(pairs)
  218.     
  219.     return (pairs, end)
  220.  
  221.  
  222. def JSONArray(.0, scan_once, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  223.     (s, end) = .0
  224.     values = []
  225.     nextchar = s[end:end + 1]
  226.     if nextchar in _ws:
  227.         end = _w(s, end + 1).end()
  228.         nextchar = s[end:end + 1]
  229.     
  230.     if nextchar == ']':
  231.         return (values, end + 1)
  232.     _append = values.append
  233.     while True:
  234.         
  235.         try:
  236.             (value, end) = scan_once(s, end)
  237.         except StopIteration:
  238.             nextchar == ']'
  239.             nextchar == ']'
  240.             raise ValueError(errmsg('Expecting object', s, end))
  241.         except:
  242.             nextchar == ']'
  243.  
  244.         _append(value)
  245.         nextchar = s[end:end + 1]
  246.         if nextchar in _ws:
  247.             end = _w(s, end + 1).end()
  248.             nextchar = s[end:end + 1]
  249.         
  250.         end += 1
  251.         if nextchar == ']':
  252.             break
  253.         elif nextchar != ',':
  254.             raise ValueError(errmsg('Expecting , delimiter', s, end))
  255.         
  256.         
  257.         try:
  258.             if s[end] in _ws:
  259.                 end += 1
  260.                 if s[end] in _ws:
  261.                     end = _w(s, end + 1).end()
  262.                 
  263.         continue
  264.         except IndexError:
  265.             continue
  266.         
  267.  
  268.         None<EXCEPTION MATCH>IndexError
  269.     return (values, end)
  270.  
  271.  
  272. class JSONDecoder(object):
  273.     
  274.     def __init__(self, encoding = None, object_hook = None, parse_float = None, parse_int = None, parse_constant = None, strict = True):
  275.         self.encoding = encoding
  276.         self.object_hook = object_hook
  277.         if not parse_float:
  278.             pass
  279.         self.parse_float = float
  280.         if not parse_int:
  281.             pass
  282.         self.parse_int = int
  283.         if not parse_constant:
  284.             pass
  285.         self.parse_constant = _CONSTANTS.__getitem__
  286.         self.strict = strict
  287.         self.parse_object = JSONObject
  288.         self.parse_array = JSONArray
  289.         self.parse_string = scanstring
  290.         self.scan_once = make_scanner(self)
  291.  
  292.     
  293.     def decode(self, s, _w = WHITESPACE.match):
  294.         (obj, end) = self.raw_decode(s, idx = _w(s, 0).end())
  295.         end = _w(s, end).end()
  296.         if end != len(s):
  297.             raise ValueError(errmsg('Extra data', s, end, len(s)))
  298.         end != len(s)
  299.         return obj
  300.  
  301.     
  302.     def raw_decode(self, s, idx = 0):
  303.         
  304.         try:
  305.             (obj, end) = self.scan_once(s, idx)
  306.         except StopIteration:
  307.             raise ValueError('No JSON object could be decoded')
  308.  
  309.         return (obj, end)
  310.  
  311.  
  312.