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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import _syck
  5.  
  6. try:
  7.     import cStringIO as StringIO
  8. except ImportError:
  9.     import StringIO
  10.  
  11. __all__ = [
  12.     'GenericDumper',
  13.     'Dumper',
  14.     'emit',
  15.     'dump',
  16.     'emit_documents',
  17.     'dump_documents']
  18.  
  19. class GenericDumper(_syck.Emitter):
  20.     
  21.     def dump(self, object):
  22.         self.emit(self._convert(object, { }))
  23.  
  24.     
  25.     def _convert(self, object, object_to_node):
  26.         if id(object) in object_to_node and self.allow_aliases(object):
  27.             return object_to_node[id(object)][1]
  28.         node = self.represent(object)
  29.         object_to_node[id(object)] = (object, node)
  30.         if node.kind == 'seq':
  31.             for index in range(len(node.value)):
  32.                 item = node.value[index]
  33.                 node.value[index] = self._convert(item, object_to_node)
  34.             
  35.         elif node.kind == 'map':
  36.             if isinstance(node.value, dict):
  37.                 for key in node.value.keys():
  38.                     value = node.value[key]
  39.                     del node.value[key]
  40.                     node.value[self._convert(key, object_to_node)] = self._convert(value, object_to_node)
  41.                 
  42.             elif isinstance(node.value, list):
  43.                 for index in range(len(node.value)):
  44.                     (key, value) = node.value[index]
  45.                     node.value[index] = (self._convert(key, object_to_node), self._convert(value, object_to_node))
  46.                 
  47.             
  48.         
  49.         return node
  50.  
  51.     
  52.     def represent(self, object):
  53.         if isinstance(object, dict):
  54.             return _syck.Map(object.copy(), tag = 'tag:yaml.org,2002:map')
  55.         if isinstance(object, list):
  56.             return _syck.Seq(object[:], tag = 'tag:yaml.org,2002:seq')
  57.         return _syck.Scalar(str(object), tag = 'tag:yaml.org,2002:str')
  58.  
  59.     
  60.     def allow_aliases(self, object):
  61.         return True
  62.  
  63.  
  64.  
  65. class Dumper(GenericDumper):
  66.     INF = float('inf')
  67.     inf_value = repr(INF)
  68.     neginf_value = repr(-INF)
  69.     nan_value = repr(INF / INF)
  70.     
  71.     def find_representer(self, object):
  72.         for object_type in type(object).__mro__:
  73.             if object_type.__module__ == '__builtin__':
  74.                 name = object_type.__name__
  75.             else:
  76.                 name = '%s.%s' % (object_type.__module__, object_type.__name__)
  77.             method = 'represent_' + name.replace('.', '_')
  78.             if hasattr(self, method):
  79.                 return getattr(self, method)
  80.         
  81.  
  82.     
  83.     def represent(self, object):
  84.         representer = self.find_representer(object)
  85.         if representer:
  86.             return representer(object)
  87.         return super(Dumper, self).represent(object)
  88.  
  89.     
  90.     def represent_object(self, object):
  91.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:str')
  92.  
  93.     
  94.     def represent_NoneType(self, object):
  95.         return _syck.Scalar('~', tag = 'tag:yaml.org,2002:null')
  96.  
  97.     
  98.     def represent_bool(self, object):
  99.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:bool')
  100.  
  101.     
  102.     def represent_str(self, object):
  103.         
  104.         try:
  105.             return _syck.Scalar(object.encode('ascii'), tag = 'tag:yaml.org,2002:str')
  106.         except (UnicodeDecodeError, UnicodeEncodeError):
  107.             
  108.             try:
  109.                 return _syck.Scalar(unicode(object, 'utf-8').encode('utf-8'), tag = 'tag:python.yaml.org,2002:str')
  110.             except (UnicodeDecodeError, UnicodeEncodeError):
  111.                 return _syck.Scalar(object.encode('base64'), tag = 'tag:yaml.org,2002:binary')
  112.             
  113.  
  114.             None<EXCEPTION MATCH>(UnicodeDecodeError, UnicodeEncodeError)
  115.  
  116.  
  117.     
  118.     def represent_unicode(self, object):
  119.         
  120.         try:
  121.             return _syck.Scalar(object.encode('ascii'), tag = 'tag:python.yaml.org,2002:unicode')
  122.         except UnicodeEncodeError:
  123.             return _syck.Scalar(object.encode('utf-8'), tag = 'tag:yaml.org,2002:str')
  124.  
  125.  
  126.     
  127.     def represent_list(self, object):
  128.         return _syck.Seq(object[:], tag = 'tag:yaml.org,2002:seq')
  129.  
  130.     
  131.     def represent_dict(self, object):
  132.         return _syck.Map(object.copy(), tag = 'tag:yaml.org,2002:map')
  133.  
  134.     
  135.     def represent_int(self, object):
  136.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:int')
  137.  
  138.     
  139.     def represent_float(self, object):
  140.         value = repr(object)
  141.         if value == self.inf_value:
  142.             value = '.inf'
  143.         elif value == self.neginf_value:
  144.             value = '-.inf'
  145.         elif value == self.nan_value:
  146.             value = '.nan'
  147.         
  148.         return _syck.Scalar(value, tag = 'tag:yaml.org,2002:float')
  149.  
  150.     
  151.     def represent_complex(self, object):
  152.         if object.real != 0:
  153.             value = '%s+%sj' % (repr(object.real), repr(object.imag))
  154.         else:
  155.             value = '%sj' % repr(object.imag)
  156.         return _syck.Scalar(value, tag = 'tag:python.yaml.org,2002:complex')
  157.  
  158.     
  159.     def represent_sets_Set(self, object):
  160.         return _syck.Seq(list(object), tag = 'tag:yaml.org,2002:set')
  161.  
  162.     represent_set = represent_sets_Set
  163.     
  164.     def represent_datetime_datetime(self, object):
  165.         return _syck.Scalar(object.isoformat(), tag = 'tag:yaml.org,2002:timestamp')
  166.  
  167.     
  168.     def represent_long(self, object):
  169.         return _syck.Scalar(repr(object), tag = 'tag:python.yaml.org,2002:long')
  170.  
  171.     
  172.     def represent_tuple(self, object):
  173.         return _syck.Seq(list(object), tag = 'tag:python.yaml.org,2002:tuple')
  174.  
  175.     
  176.     def represent_type(self, object):
  177.         name = '%s.%s' % (object.__module__, object.__name__)
  178.         return _syck.Scalar('', tag = 'tag:python.yaml.org,2002:name:' + name)
  179.  
  180.     represent_classobj = represent_type
  181.     represent_class = represent_type
  182.     represent_function = represent_type
  183.     represent_builtin_function_or_method = represent_type
  184.     
  185.     def represent_module(self, object):
  186.         return _syck.Scalar('', tag = 'tag:python.yaml.org,2002:module:' + object.__name__)
  187.  
  188.     
  189.     def represent_instance(self, object):
  190.         cls = object.__class__
  191.         class_name = '%s.%s' % (cls.__module__, cls.__name__)
  192.         args = ()
  193.         state = { }
  194.         if hasattr(object, '__getinitargs__'):
  195.             args = object.__getinitargs__()
  196.         
  197.         if hasattr(object, '__getstate__'):
  198.             state = object.__getstate__()
  199.         elif not hasattr(object, '__getinitargs__'):
  200.             state = object.__dict__.copy()
  201.         
  202.         if not args and isinstance(state, dict):
  203.             return _syck.Map(state.copy(), tag = 'tag:python.yaml.org,2002:object:' + class_name)
  204.         value = { }
  205.         if args:
  206.             value['args'] = list(args)
  207.         
  208.         if state or not isinstance(state, dict):
  209.             value['state'] = state
  210.         
  211.         return _syck.Map(value, tag = 'tag:python.yaml.org,2002:new:' + class_name)
  212.  
  213.     
  214.     def represent_object(self, object):
  215.         cls = type(object)
  216.         class_name = '%s.%s' % (cls.__module__, cls.__name__)
  217.         args = ()
  218.         state = { }
  219.         if cls.__reduce__ is type.__reduce__:
  220.             if hasattr(object, '__reduce_ex__'):
  221.                 reduce = object.__reduce_ex__(2)
  222.                 args = reduce[1][1:]
  223.             else:
  224.                 reduce = object.__reduce__()
  225.             if len(reduce) > 2:
  226.                 state = reduce[2]
  227.             
  228.             if state is None:
  229.                 state = { }
  230.             
  231.             if not args and isinstance(state, dict):
  232.                 return _syck.Map(state.copy(), tag = 'tag:python.yaml.org,2002:object:' + class_name)
  233.             if not state and isinstance(state, dict):
  234.                 return _syck.Seq(list(args), tag = 'tag:python.yaml.org,2002:new:' + class_name)
  235.             value = { }
  236.             if state or not isinstance(state, dict):
  237.                 value['state'] = state
  238.             
  239.             return _syck.Map(value, tag = 'tag:python.yaml.org,2002:new:' + class_name)
  240.         reduce = object.__reduce__()
  241.         cls = reduce[0]
  242.         class_name = '%s.%s' % (cls.__module__, cls.__name__)
  243.         args = reduce[1]
  244.         state = None
  245.         if len(reduce) > 2:
  246.             state = reduce[2]
  247.         
  248.         if state is None:
  249.             state = { }
  250.         
  251.         if not state and isinstance(state, dict):
  252.             return _syck.Seq(list(args), tag = 'tag:python.yaml.org,2002:apply:' + class_name)
  253.         value = { }
  254.         if args:
  255.             value['args'] = list(args)
  256.         
  257.         if state or not isinstance(state, dict):
  258.             value['state'] = state
  259.         
  260.         return _syck.Map(value, tag = 'tag:python.yaml.org,2002:apply:' + class_name)
  261.  
  262.     
  263.     def represent__syck_Node(self, object):
  264.         object_type = type(object)
  265.         type_name = '%s.%s' % (object_type.__module__, object_type.__name__)
  266.         state = []
  267.         if hasattr(object_type, '__slotnames__'):
  268.             for name in object_type.__slotnames__:
  269.                 value = getattr(object, name)
  270.                 if value:
  271.                     state.append((name, value))
  272.                     continue
  273.             
  274.         
  275.         return _syck.Map(state, tag = 'tag:python.yaml.org,2002:object:' + type_name)
  276.  
  277.     
  278.     def allow_aliases(self, object):
  279.         if object is None or type(object) in [
  280.             int,
  281.             bool,
  282.             float]:
  283.             return False
  284.         if type(object) is str:
  285.             if not object or object.isalnum():
  286.                 return False
  287.             if type(object) is tuple and not object:
  288.                 return False
  289.             return True
  290.  
  291.  
  292.  
  293. def emit(node, output = None, Dumper = Dumper, **parameters):
  294.     if output is None:
  295.         dumper = Dumper(StringIO.StringIO(), **parameters)
  296.     else:
  297.         dumper = Dumper(output, **parameters)
  298.     dumper.emit(node)
  299.     if output is None:
  300.         return dumper.output.getvalue()
  301.  
  302.  
  303. def dump(object, output = None, Dumper = Dumper, **parameters):
  304.     if output is None:
  305.         dumper = Dumper(StringIO.StringIO(), **parameters)
  306.     else:
  307.         dumper = Dumper(output, **parameters)
  308.     dumper.dump(object)
  309.     if output is None:
  310.         return dumper.output.getvalue()
  311.  
  312.  
  313. def emit_documents(nodes, output = None, Dumper = Dumper, **parameters):
  314.     if output is None:
  315.         dumper = Dumper(StringIO.StringIO(), **parameters)
  316.     else:
  317.         dumper = Dumper(output, **parameters)
  318.     for node in nodes:
  319.         dumper.emit(node)
  320.     
  321.     if output is None:
  322.         return dumper.output.getvalue()
  323.  
  324.  
  325. def dump_documents(objects, output = None, Dumper = Dumper, **parameters):
  326.     if output is None:
  327.         dumper = Dumper(StringIO.StringIO(), **parameters)
  328.     else:
  329.         dumper = Dumper(output, **parameters)
  330.     for object in objects:
  331.         dumper.dump(object)
  332.     
  333.     if output is None:
  334.         return dumper.output.getvalue()
  335.  
  336.