def testSeq(self): self.assert_(issubclass(_syck.Seq, _syck.Node)) seq = _syck.Seq() self.assertEqual(seq.kind, 'seq') self.assertEqual(seq.value, []) self.assertEqual(seq.anchor, None) self.assertEqual(seq.tag, None) self.assertEqual(seq.inline, False) self.assertRaises(TypeError, lambda: setattr(seq, 'kind', 'map')) self.assertRaises(TypeError, lambda: setattr(seq, 'tag', [])) self.assertRaises(TypeError, lambda: setattr(seq, 'inline', 'block')) self.assertRaises(TypeError, lambda: setattr(seq, 'inline', [])) value = [_syck.Scalar(str(k)) for k in range(10)] seq = _syck.Seq(value, tag='a_tag', inline=True) self.assertEqual(seq.value, value) self.assertEqual(seq.tag, 'a_tag') self.assertEqual(seq.inline, True) value = [_syck.Scalar(str(k)) for k in range(20)] seq.value = value seq.tag = 'another_tag' seq.inline = False self.assertEqual(seq.value, value) self.assertEqual(seq.tag, 'another_tag') self.assertEqual(seq.inline, False) seq.tag = None self.assertEqual(seq.tag, None)
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)
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")
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)
def testTags(self): document = _syck.Seq() for tag in TAGS: document.value.append(_syck.Scalar('foo', tag=tag)) emitter = _syck.Emitter(StringIO.StringIO()) emitter.emit(document) parser = _syck.Parser(emitter.output.getvalue()) document = parser.parse() self.assertEqual(len(document.value), len(TAGS)) for index in range(len(document.value)): node = document.value[index] self.assertEqual(node.tag, TAGS[index])
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)
def represent_tuple(self, object): return _syck.Seq(list(object), tag="tag:python.yaml.org,2002:tuple")
def represent_sets_Set(self, object): return _syck.Seq(list(object), tag="tag:yaml.org,2002:set")
def represent_list(self, object): return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq")
def testFlowCollectionKeyBug(self): tree = _syck.Map({ _syck.Seq([_syck.Scalar('foo')], inline=True): _syck.Scalar('bar') }) self._testTree(tree)
def testCollectionWithTagKeyBug(self): tree = _syck.Map({ _syck.Seq([_syck.Scalar('foo')], tag='x-private:key'): _syck.Scalar('bar') }) self._testTree(tree)
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)
import unittest import _syck import StringIO, gc EXAMPLE = _syck.Seq([ _syck.Scalar('Mark McGwire'), _syck.Scalar('Sammy Sosa'), _syck.Scalar('Ken Griffey'), ]) 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(