source: pyyaml/trunk/lib/yaml/events.py @ 222

Revision 222, 2.4 KB checked in by xi, 8 years ago (diff)

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

RevLine 
[51]1
[130]2# Abstract classes.
3
[222]4class Event(object):
[130]5    def __init__(self, start_mark=None, end_mark=None):
[116]6        self.start_mark = start_mark
7        self.end_mark = end_mark
[51]8    def __repr__(self):
[137]9        attributes = [key for key in ['anchor', 'tag', 'implicit', 'value']
[130]10                if hasattr(self, key)]
[51]11        arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
12                for key in attributes])
13        return '%s(%s)' % (self.__class__.__name__, arguments)
14
15class NodeEvent(Event):
[130]16    def __init__(self, anchor, start_mark=None, end_mark=None):
[51]17        self.anchor = anchor
[116]18        self.start_mark = start_mark
19        self.end_mark = end_mark
[51]20
[130]21class CollectionStartEvent(NodeEvent):
[137]22    def __init__(self, anchor, tag, implicit, start_mark=None, end_mark=None,
[130]23            flow_style=None):
[51]24        self.anchor = anchor
25        self.tag = tag
[137]26        self.implicit = implicit
[116]27        self.start_mark = start_mark
28        self.end_mark = end_mark
[130]29        self.flow_style = flow_style
[51]30
[130]31class CollectionEndEvent(Event):
32    pass
33
34# Implementations.
35
36class StreamStartEvent(Event):
[136]37    def __init__(self, start_mark=None, end_mark=None, encoding=None):
[116]38        self.start_mark = start_mark
39        self.end_mark = end_mark
[130]40        self.encoding = encoding
[51]41
[130]42class StreamEndEvent(Event):
[51]43    pass
44
[130]45class DocumentStartEvent(Event):
46    def __init__(self, start_mark=None, end_mark=None,
[132]47            explicit=None, version=None, tags=None):
[130]48        self.start_mark = start_mark
49        self.end_mark = end_mark
[132]50        self.explicit = explicit
[130]51        self.version = version
52        self.tags = tags
[51]53
[130]54class DocumentEndEvent(Event):
55    def __init__(self, start_mark=None, end_mark=None,
[132]56            explicit=None):
[130]57        self.start_mark = start_mark
58        self.end_mark = end_mark
[132]59        self.explicit = explicit
[130]60
61class AliasEvent(NodeEvent):
[51]62    pass
63
[130]64class ScalarEvent(NodeEvent):
[136]65    def __init__(self, anchor, tag, implicit, value,
66            start_mark=None, end_mark=None, style=None):
[130]67        self.anchor = anchor
68        self.tag = tag
[136]69        self.implicit = implicit
[130]70        self.value = value
71        self.start_mark = start_mark
72        self.end_mark = end_mark
73        self.style = style
74
75class SequenceStartEvent(CollectionStartEvent):
[118]76    pass
77
[130]78class SequenceEndEvent(CollectionEndEvent):
[118]79    pass
80
[130]81class MappingStartEvent(CollectionStartEvent):
[118]82    pass
83
[130]84class MappingEndEvent(CollectionEndEvent):
[51]85    pass
86
Note: See TracBrowser for help on using the repository browser.