Пример #1
0
 def testMap(self):
     self.assert_(issubclass(_syck.Map, _syck.Node))
     map = _syck.Map()
     self.assertEqual(map.kind, 'map')
     self.assertEqual(map.value, {})
     self.assertEqual(map.anchor, None)
     self.assertEqual(map.tag, None)
     self.assertEqual(map.inline, False)
     self.assertRaises(TypeError, lambda: setattr(map, 'kind', 'map'))
     self.assertRaises(TypeError, lambda: setattr(map, 'tag', []))
     self.assertRaises(TypeError, lambda: setattr(map, 'inline', 'block'))
     self.assertRaises(TypeError, lambda: setattr(map, 'inline', []))
     value = dict([(_syck.Scalar(str(k)), _syck.Scalar(str(-k)))
                   for k in range(10)])
     map = _syck.Map(value, tag='a_tag', inline=True)
     self.assertEqual(map.value, value)
     self.assertEqual(map.tag, 'a_tag')
     self.assertEqual(map.inline, True)
     value = [(_syck.Scalar(str(k)), _syck.Scalar(str(-k)))
              for k in range(20)]
     map.value = value
     map.tag = 'another_tag'
     map.inline = False
     self.assertEqual(map.value, value)
     self.assertEqual(map.tag, 'another_tag')
     self.assertEqual(map.inline, False)
     map.tag = None
     self.assertEqual(map.tag, None)
Пример #2
0
 def represent_object(self, object):  # Do you understand this? I don't.
     cls = type(object)
     class_name = '%s.%s' % (cls.__module__, cls.__name__)
     args = ()
     state = {}
     if cls.__reduce__ is type.__reduce__:
         if hasattr(object, '__reduce_ex__'):
             reduce = object.__reduce_ex__(2)
             args = reduce[1][1:]
         else:
             reduce = object.__reduce__()
         if len(reduce) > 2:
             state = reduce[2]
         if state is None:
             state = {}
         if not args and isinstance(state, dict):
             return _syck.Map(state.copy(),
                              tag="tag:python.yaml.org,2002:object:" +
                              class_name)
         if not state and isinstance(state, dict):
             return _syck.Seq(list(args),
                              tag="tag:python.yaml.org,2002:new:" +
                              class_name)
         value = {}
         if args:
             value['args'] = list(args)
         if state or not isinstance(state, dict):
             value['state'] = state
         return _syck.Map(value,
                          tag="tag:python.yaml.org,2002:new:" + class_name)
     else:
         reduce = object.__reduce__()
         cls = reduce[0]
         class_name = '%s.%s' % (cls.__module__, cls.__name__)
         args = reduce[1]
         state = None
         if len(reduce) > 2:
             state = reduce[2]
         if state is None:
             state = {}
         if not state and isinstance(state, dict):
             return _syck.Seq(list(args),
                              tag="tag:python.yaml.org,2002:apply:" +
                              class_name)
         value = {}
         if args:
             value['args'] = list(args)
         if state or not isinstance(state, dict):
             value['state'] = state
         return _syck.Map(value,
                          tag="tag:python.yaml.org,2002:apply:" +
                          class_name)
Пример #3
0
 def represent(self, object):
     """Represents the given Python object as a 'Node'."""
     if isinstance(object, dict):
         return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map")
     elif isinstance(object, list):
         return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq")
     else:
         return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str")
Пример #4
0
 def testSyckBugWithComplexKeys(self):
     broken = 0
     for key in COMPLEX_ITEMS:
         for value in COMPLEX_ITEMS:
             node = _syck.Map({key: value})
             emitter = _syck.Emitter(StringIO.StringIO())
             emitter.emit(node)
             parser = _syck.Parser(emitter.output.getvalue())
             self.assertEqual(strip(node), strip(parser.parse()))
Пример #5
0
 def testGarbage(self):
     gc.collect()
     seq = _syck.Seq()
     seq.value = [seq]
     del seq
     self.assertEqual(gc.collect(), 2)
     scalar1 = _syck.Scalar()
     scalar2 = _syck.Scalar()
     seq = _syck.Seq()
     map1 = _syck.Map()
     map2 = _syck.Map()
     map1.value[scalar1] = seq
     map1.value[scalar2] = map2
     map2.value[scalar1] = map1
     map2.value[scalar2] = seq
     seq.value.append(map1)
     seq.value.append(map2)
     del scalar1, scalar2, seq, map1, map2
     self.assertEqual(gc.collect(), 8)
Пример #6
0
 def represent__syck_Node(self, object):
     object_type = type(object)
     type_name = '%s.%s' % (object_type.__module__, object_type.__name__)
     state = []
     if hasattr(object_type, '__slotnames__'):
         for name in object_type.__slotnames__:
             value = getattr(object, name)
             if value:
                 state.append((name, value))
     return _syck.Map(state,
                      tag="tag:python.yaml.org,2002:object:" + type_name)
Пример #7
0
 def represent_instance(self, object):
     cls = object.__class__
     class_name = '%s.%s' % (cls.__module__, cls.__name__)
     args = ()
     state = {}
     if hasattr(object, '__getinitargs__'):
         args = object.__getinitargs__()
     if hasattr(object, '__getstate__'):
         state = object.__getstate__()
     elif not hasattr(object, '__getinitargs__'):
         state = object.__dict__.copy()
     if not args and isinstance(state, dict):
         return _syck.Map(state.copy(),
                          tag="tag:python.yaml.org,2002:object:" +
                          class_name)
     value = {}
     if args:
         value['args'] = list(args)
     if state or not isinstance(state, dict):
         value['state'] = state
     return _syck.Map(value,
                      tag="tag:python.yaml.org,2002:new:" + class_name)
Пример #8
0
 def represent_object(self, object):  # Borrowed from PyYAML.
     cls = type(object)
     if cls in copy_reg.dispatch_table:
         reduce = copy_reg.dispatch_table[cls](object)
     elif hasattr(object, '__reduce_ex__'):
         reduce = object.__reduce_ex__(2)
     elif hasattr(object, '__reduce__'):
         reduce = object.__reduce__()
     else:
         raise RuntimeError("cannot dump object: %r" % object)
     reduce = (list(reduce) + [None] * 3)[:3]
     function, args, state = reduce
     args = list(args)
     if state is None:
         state = {}
     if function.__name__ == '__newobj__':
         function = args[0]
         args = args[1:]
         tag = 'tag:python.yaml.org,2002:new:'
         newobj = True
     else:
         tag = 'tag:python.yaml.org,2002:apply:'
         newobj = False
     function_name = '%s.%s' % (function.__module__, function.__name__)
     if not args and isinstance(state, dict) and newobj:
         return _syck.Map(
             state.copy(),
             'tag:python.yaml.org,2002:object:' + function_name)
     if isinstance(state, dict) and not state:
         return _syck.Seq(args, tag + function_name)
     value = {}
     if args:
         value['args'] = args
     if state or not isinstance(state, dict):
         value['state'] = state
     return _syck.Map(value, tag + function_name)
Пример #9
0
 def represent_dict(self, object):
     return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map")
Пример #10
0
 def testScalarWithAliasKeyBug(self):
     node = _syck.Scalar('foo', tag='x-private:bug')
     tree = _syck.Map({node: node})
     self._testTree(tree)
Пример #11
0
 def testEmptyCollectionWithAliasKeyBug(self):
     node = _syck.Map()
     tree = _syck.Map({node: node})
     self._testTree(tree)
Пример #12
0
 def testFlowCollectionKeyBug(self):
     tree = _syck.Map({
         _syck.Seq([_syck.Scalar('foo')], inline=True):
         _syck.Scalar('bar')
     })
     self._testTree(tree)
Пример #13
0
 def testCollectionWithTagKeyBug(self):
     tree = _syck.Map({
         _syck.Seq([_syck.Scalar('foo')], tag='x-private:key'):
         _syck.Scalar('bar')
     })
     self._testTree(tree)
Пример #14
0
 def testEmptyCollectionKeyBug(self):
     tree1 = _syck.Map({_syck.Map(): _syck.Scalar('foo')})
     self._testTree(tree1)
     tree2 = _syck.Map({_syck.Seq(): _syck.Scalar('foo')})
     self._testTree(tree2)
Пример #15
0
COMPLEX_EXAMPLE = _syck.Map({
    _syck.Scalar('scalars'):
    _syck.Seq([
        _syck.Scalar(),
        _syck.Scalar('on'),
        _syck.Scalar('12345', tag="tag:yaml.org,2002:int"),
        # Syck bug with trailing spaces.
        #            _syck.Scalar('long '*100, width=20, indent=5),
        _syck.Scalar('long long long long long\n' * 100, width=20, indent=5),
        _syck.Scalar('"1quote"', style='1quote'),
        _syck.Scalar("'2quote'", style='2quote'),
        _syck.Scalar("folding\n" * 10, style='fold'),
        _syck.Scalar("literal\n" * 10, style='literal'),
        _syck.Scalar("plain text", style='plain'),
    ]),
    _syck.Scalar('sequences'):
    _syck.Seq([
        _syck.Seq([
            _syck.Seq(
                [_syck.Scalar('foo'),
                 _syck.Scalar('bar', style='literal')]),
        ],
                  inline=True),
        _syck.Seq([
            _syck.Scalar('foo'),
            _syck.Scalar('bar'),
        ],
                  tag="x-private:myprivatesequence")
    ]),
    _syck.Scalar('mappings'):
    _syck.Seq([
        _syck.Map({
            _syck.Seq([_syck.Scalar('1'), _syck.Scalar('2')]):
            _syck.Seq([_syck.Scalar('3'), _syck.Scalar('4')]),
            _syck.Scalar('5'):
            _syck.Scalar('6'),
        }),
        _syck.Map({_syck.Scalar('foo'): _syck.Seq([_syck.Scalar('bar')])},
                  inline=True,
                  tag="x-private:myprivatemapping"),
    ]),
    _syck.Scalar('empty'):
    _syck.Seq([
        _syck.Scalar(),
        _syck.Seq(),
        _syck.Map(),
        _syck.Map({
            _syck.Scalar(tag='tag:yaml.org,2002:str'):
            _syck.Scalar(tag='tag:yaml.org,2002:str')
        }),
        _syck.Map({_syck.Scalar('foo'): _syck.Scalar()}),
        _syck.Map(
            {_syck.Scalar(tag='tag:yaml.org,2002:str'): _syck.Scalar('bar')}),
    ]),
})