home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / simplejson / encoder.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  8.7 KB  |  347 lines

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