home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / simplejson / encoder.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  8.6 KB  |  350 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5.  
  6. try:
  7.     from simplejson._speedups import encode_basestring_ascii as c_encode_basestring_ascii
  8. except ImportError:
  9.     c_encode_basestring_ascii = None
  10.  
  11.  
  12. try:
  13.     from simplejson._speedups import make_encoder as c_make_encoder
  14. except ImportError:
  15.     c_make_encoder = None
  16.  
  17. ESCAPE = re.compile('[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t]')
  18. ESCAPE_ASCII = re.compile('([\\\\"]|[^\\ -~])')
  19. HAS_UTF8 = re.compile('[\\x80-\\xff]')
  20. ESCAPE_DCT = {
  21.     '\\': '\\\\',
  22.     '"': '\\"',
  23.     '\x08': '\\b',
  24.     '\x0c': '\\f',
  25.     '\n': '\\n',
  26.     '\r': '\\r',
  27.     '\t': '\\t' }
  28. for i in range(32):
  29.     ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
  30.  
  31. INFINITY = float('1e66666')
  32. FLOAT_REPR = repr
  33.  
  34. def encode_basestring(s):
  35.     
  36.     def replace(match):
  37.         return ESCAPE_DCT[match.group(0)]
  38.  
  39.     return '"' + ESCAPE.sub(replace, s) + '"'
  40.  
  41.  
  42. def py_encode_basestring_ascii(s):
  43.     if isinstance(s, str) and HAS_UTF8.search(s) is not None:
  44.         s = s.decode('utf-8')
  45.     
  46.     
  47.     def replace(match):
  48.         s = match.group(0)
  49.         
  50.         try:
  51.             return ESCAPE_DCT[s]
  52.         except KeyError:
  53.             n = ord(s)
  54.             if n < 65536:
  55.                 return '\\u%04x' % (n,)
  56.             else:
  57.                 n -= 65536
  58.                 s1 = 55296 | n >> 10 & 1023
  59.                 s2 = 56320 | n & 1023
  60.                 return '\\u%04x\\u%04x' % (s1, s2)
  61.         except:
  62.             n < 65536
  63.  
  64.  
  65.     return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"'
  66.  
  67. if not c_encode_basestring_ascii:
  68.     pass
  69. encode_basestring_ascii = py_encode_basestring_ascii
  70.  
  71. class JSONEncoder(object):
  72.     item_separator = ', '
  73.     key_separator = ': '
  74.     
  75.     def __init__(self, skipkeys = False, ensure_ascii = True, check_circular = True, allow_nan = True, sort_keys = False, indent = None, separators = None, encoding = 'utf-8', default = None):
  76.         self.skipkeys = skipkeys
  77.         self.ensure_ascii = ensure_ascii
  78.         self.check_circular = check_circular
  79.         self.allow_nan = allow_nan
  80.         self.sort_keys = sort_keys
  81.         self.indent = indent
  82.         if separators is not None:
  83.             (self.item_separator, self.key_separator) = separators
  84.         
  85.         if default is not None:
  86.             self.default = default
  87.         
  88.         self.encoding = encoding
  89.  
  90.     
  91.     def default(self, o):
  92.         raise TypeError('%r is not JSON serializable' % (o,))
  93.  
  94.     
  95.     def encode(self, o):
  96.         if isinstance(o, basestring):
  97.             if isinstance(o, str):
  98.                 _encoding = self.encoding
  99.                 if _encoding is not None and not (_encoding == 'utf-8'):
  100.                     o = o.decode(_encoding)
  101.                 
  102.             
  103.             if self.ensure_ascii:
  104.                 return encode_basestring_ascii(o)
  105.             else:
  106.                 return encode_basestring(o)
  107.         
  108.         chunks = self.iterencode(o, _one_shot = True)
  109.         if not isinstance(chunks, (list, tuple)):
  110.             chunks = list(chunks)
  111.         
  112.         return ''.join(chunks)
  113.  
  114.     
  115.     def iterencode(self, o, _one_shot = False):
  116.         if self.check_circular:
  117.             markers = { }
  118.         else:
  119.             markers = None
  120.         if self.ensure_ascii:
  121.             _encoder = encode_basestring_ascii
  122.         else:
  123.             _encoder = encode_basestring
  124.         if self.encoding != 'utf-8':
  125.             
  126.             def _encoder(o, _orig_encoder = _encoder, _encoding = self.encoding):
  127.                 if isinstance(o, str):
  128.                     o = o.decode(_encoding)
  129.                 
  130.                 return _orig_encoder(o)
  131.  
  132.         
  133.         
  134.         def floatstr(o, allow_nan = self.allow_nan, _repr = FLOAT_REPR, _inf = INFINITY, _neginf = -INFINITY):
  135.             if o != o:
  136.                 text = 'NaN'
  137.             elif o == _inf:
  138.                 text = 'Infinity'
  139.             elif o == _neginf:
  140.                 text = '-Infinity'
  141.             else:
  142.                 return _repr(o)
  143.             if not allow_nan:
  144.                 raise ValueError('Out of range float values are not JSON compliant: %r' % (o,))
  145.             
  146.             return text
  147.  
  148.         if _one_shot and c_make_encoder is not None and not (self.indent) and not (self.sort_keys):
  149.             _iterencode = c_make_encoder(markers, self.default, _encoder, self.indent, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, self.allow_nan)
  150.         else:
  151.             _iterencode = _make_iterencode(markers, self.default, _encoder, self.indent, floatstr, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, _one_shot)
  152.         return _iterencode(o, 0)
  153.  
  154.  
  155.  
  156. def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, False = False, True = True, ValueError = ValueError, basestring = basestring, dict = dict, float = float, id = id, int = int, isinstance = isinstance, list = list, long = long, str = str, tuple = tuple):
  157.     
  158.     def _iterencode_list(lst, _current_indent_level):
  159.         if not lst:
  160.             yield '[]'
  161.             return None
  162.         
  163.         if markers is not None:
  164.             markerid = id(lst)
  165.             if markerid in markers:
  166.                 raise ValueError('Circular reference detected')
  167.             
  168.             markers[markerid] = lst
  169.         
  170.         buf = '['
  171.         if _indent is not None:
  172.             _current_indent_level += 1
  173.             newline_indent = '\n' + ' ' * _indent * _current_indent_level
  174.             separator = _item_separator + newline_indent
  175.             buf += newline_indent
  176.         else:
  177.             newline_indent = None
  178.             separator = _item_separator
  179.         first = True
  180.         for value in lst:
  181.             if first:
  182.                 first = False
  183.             else:
  184.                 buf = separator
  185.             if isinstance(value, basestring):
  186.                 yield buf + _encoder(value)
  187.                 continue
  188.             if value is None:
  189.                 yield buf + 'null'
  190.                 continue
  191.             if value is True:
  192.                 yield buf + 'true'
  193.                 continue
  194.             if value is False:
  195.                 yield buf + 'false'
  196.                 continue
  197.             if isinstance(value, (int, long)):
  198.                 yield buf + str(value)
  199.                 continue
  200.             if isinstance(value, float):
  201.                 yield buf + _floatstr(value)
  202.                 continue
  203.             yield buf
  204.             if isinstance(value, (list, tuple)):
  205.                 chunks = _iterencode_list(value, _current_indent_level)
  206.             elif isinstance(value, dict):
  207.                 chunks = _iterencode_dict(value, _current_indent_level)
  208.             else:
  209.                 chunks = _iterencode(value, _current_indent_level)
  210.             for chunk in chunks:
  211.                 yield chunk
  212.             
  213.         
  214.         if newline_indent is not None:
  215.             _current_indent_level -= 1
  216.             yield '\n' + ' ' * _indent * _current_indent_level
  217.         
  218.         yield ']'
  219.         if markers is not None:
  220.             del markers[markerid]
  221.         
  222.  
  223.     
  224.     def _iterencode_dict(dct, _current_indent_level):
  225.         if not dct:
  226.             yield '{}'
  227.             return None
  228.         
  229.         if markers is not None:
  230.             markerid = id(dct)
  231.             if markerid in markers:
  232.                 raise ValueError('Circular reference detected')
  233.             
  234.             markers[markerid] = dct
  235.         
  236.         yield '{'
  237.         if _indent is not None:
  238.             _current_indent_level += 1
  239.             newline_indent = '\n' + ' ' * _indent * _current_indent_level
  240.             item_separator = _item_separator + newline_indent
  241.             yield newline_indent
  242.         else:
  243.             newline_indent = None
  244.             item_separator = _item_separator
  245.         first = True
  246.         if _sort_keys:
  247.             items = dct.items()
  248.             items.sort(key = (lambda kv: kv[0]))
  249.         else:
  250.             items = dct.iteritems()
  251.         for key, value in items:
  252.             if isinstance(key, basestring):
  253.                 pass
  254.             elif isinstance(key, float):
  255.                 key = _floatstr(key)
  256.             elif isinstance(key, (int, long)):
  257.                 key = str(key)
  258.             elif key is True:
  259.                 key = 'true'
  260.             elif key is False:
  261.                 key = 'false'
  262.             elif key is None:
  263.                 key = 'null'
  264.             elif _skipkeys:
  265.                 continue
  266.             else:
  267.                 raise TypeError('key %r is not a string' % (key,))
  268.             if first:
  269.                 first = False
  270.             else:
  271.                 yield item_separator
  272.             yield _encoder(key)
  273.             yield _key_separator
  274.             if isinstance(value, basestring):
  275.                 yield _encoder(value)
  276.                 continue
  277.             if value is None:
  278.                 yield 'null'
  279.                 continue
  280.             if value is True:
  281.                 yield 'true'
  282.                 continue
  283.             if value is False:
  284.                 yield 'false'
  285.                 continue
  286.             if isinstance(value, (int, long)):
  287.                 yield str(value)
  288.                 continue
  289.             if isinstance(value, float):
  290.                 yield _floatstr(value)
  291.                 continue
  292.             if isinstance(value, (list, tuple)):
  293.                 chunks = _iterencode_list(value, _current_indent_level)
  294.             elif isinstance(value, dict):
  295.                 chunks = _iterencode_dict(value, _current_indent_level)
  296.             else:
  297.                 chunks = _iterencode(value, _current_indent_level)
  298.             for chunk in chunks:
  299.                 yield chunk
  300.             
  301.         
  302.         if newline_indent is not None:
  303.             _current_indent_level -= 1
  304.             yield '\n' + ' ' * _indent * _current_indent_level
  305.         
  306.         yield '}'
  307.         if markers is not None:
  308.             del markers[markerid]
  309.         
  310.  
  311.     
  312.     def _iterencode(o, _current_indent_level):
  313.         if isinstance(o, basestring):
  314.             yield _encoder(o)
  315.         elif o is None:
  316.             yield 'null'
  317.         elif o is True:
  318.             yield 'true'
  319.         elif o is False:
  320.             yield 'false'
  321.         elif isinstance(o, (int, long)):
  322.             yield str(o)
  323.         elif isinstance(o, float):
  324.             yield _floatstr(o)
  325.         elif isinstance(o, (list, tuple)):
  326.             for chunk in _iterencode_list(o, _current_indent_level):
  327.                 yield chunk
  328.             
  329.         elif isinstance(o, dict):
  330.             for chunk in _iterencode_dict(o, _current_indent_level):
  331.                 yield chunk
  332.             
  333.         elif markers is not None:
  334.             markerid = id(o)
  335.             if markerid in markers:
  336.                 raise ValueError('Circular reference detected')
  337.             
  338.             markers[markerid] = o
  339.         
  340.         o = _default(o)
  341.         for chunk in _iterencode(o, _current_indent_level):
  342.             yield chunk
  343.         
  344.         if markers is not None:
  345.             del markers[markerid]
  346.         
  347.  
  348.     return _iterencode
  349.  
  350.