| 1 | |
|---|
| 2 | from error import * |
|---|
| 3 | |
|---|
| 4 | from tokens import * |
|---|
| 5 | from events import * |
|---|
| 6 | from nodes import * |
|---|
| 7 | |
|---|
| 8 | from loader import * |
|---|
| 9 | from dumper import * |
|---|
| 10 | |
|---|
| 11 | __version__ = '3.07' |
|---|
| 12 | |
|---|
| 13 | try: |
|---|
| 14 | from cyaml import * |
|---|
| 15 | __with_libyaml__ = True |
|---|
| 16 | except ImportError: |
|---|
| 17 | __with_libyaml__ = False |
|---|
| 18 | |
|---|
| 19 | def scan(stream, Loader=Loader): |
|---|
| 20 | """ |
|---|
| 21 | Scan a YAML stream and produce scanning tokens. |
|---|
| 22 | """ |
|---|
| 23 | loader = Loader(stream) |
|---|
| 24 | while loader.check_token(): |
|---|
| 25 | yield loader.get_token() |
|---|
| 26 | |
|---|
| 27 | def parse(stream, Loader=Loader): |
|---|
| 28 | """ |
|---|
| 29 | Parse a YAML stream and produce parsing events. |
|---|
| 30 | """ |
|---|
| 31 | loader = Loader(stream) |
|---|
| 32 | while loader.check_event(): |
|---|
| 33 | yield loader.get_event() |
|---|
| 34 | |
|---|
| 35 | def compose(stream, Loader=Loader): |
|---|
| 36 | """ |
|---|
| 37 | Parse the first YAML document in a stream |
|---|
| 38 | and produce the corresponding representation tree. |
|---|
| 39 | """ |
|---|
| 40 | loader = Loader(stream) |
|---|
| 41 | return loader.get_single_node() |
|---|
| 42 | |
|---|
| 43 | def compose_all(stream, Loader=Loader): |
|---|
| 44 | """ |
|---|
| 45 | Parse all YAML documents in a stream |
|---|
| 46 | and produce corresponsing representation trees. |
|---|
| 47 | """ |
|---|
| 48 | loader = Loader(stream) |
|---|
| 49 | while loader.check_node(): |
|---|
| 50 | yield loader.get_node() |
|---|
| 51 | |
|---|
| 52 | def load(stream, Loader=Loader): |
|---|
| 53 | """ |
|---|
| 54 | Parse the first YAML document in a stream |
|---|
| 55 | and produce the corresponding Python object. |
|---|
| 56 | """ |
|---|
| 57 | loader = Loader(stream) |
|---|
| 58 | return loader.get_single_data() |
|---|
| 59 | |
|---|
| 60 | def load_all(stream, Loader=Loader): |
|---|
| 61 | """ |
|---|
| 62 | Parse all YAML documents in a stream |
|---|
| 63 | and produce corresponding Python objects. |
|---|
| 64 | """ |
|---|
| 65 | loader = Loader(stream) |
|---|
| 66 | while loader.check_data(): |
|---|
| 67 | yield loader.get_data() |
|---|
| 68 | |
|---|
| 69 | def safe_load(stream): |
|---|
| 70 | """ |
|---|
| 71 | Parse the first YAML document in a stream |
|---|
| 72 | and produce the corresponding Python object. |
|---|
| 73 | Resolve only basic YAML tags. |
|---|
| 74 | """ |
|---|
| 75 | return load(stream, SafeLoader) |
|---|
| 76 | |
|---|
| 77 | def safe_load_all(stream): |
|---|
| 78 | """ |
|---|
| 79 | Parse all YAML documents in a stream |
|---|
| 80 | and produce corresponding Python objects. |
|---|
| 81 | Resolve only basic YAML tags. |
|---|
| 82 | """ |
|---|
| 83 | return load_all(stream, SafeLoader) |
|---|
| 84 | |
|---|
| 85 | def emit(events, stream=None, Dumper=Dumper, |
|---|
| 86 | canonical=None, indent=None, width=None, |
|---|
| 87 | allow_unicode=None, line_break=None): |
|---|
| 88 | """ |
|---|
| 89 | Emit YAML parsing events into a stream. |
|---|
| 90 | If stream is None, return the produced string instead. |
|---|
| 91 | """ |
|---|
| 92 | getvalue = None |
|---|
| 93 | if stream is None: |
|---|
| 94 | try: |
|---|
| 95 | from cStringIO import StringIO |
|---|
| 96 | except ImportError: |
|---|
| 97 | from StringIO import StringIO |
|---|
| 98 | stream = StringIO() |
|---|
| 99 | getvalue = stream.getvalue |
|---|
| 100 | dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, |
|---|
| 101 | allow_unicode=allow_unicode, line_break=line_break) |
|---|
| 102 | for event in events: |
|---|
| 103 | dumper.emit(event) |
|---|
| 104 | if getvalue: |
|---|
| 105 | return getvalue() |
|---|
| 106 | |
|---|
| 107 | def serialize_all(nodes, stream=None, Dumper=Dumper, |
|---|
| 108 | canonical=None, indent=None, width=None, |
|---|
| 109 | allow_unicode=None, line_break=None, |
|---|
| 110 | encoding='utf-8', explicit_start=None, explicit_end=None, |
|---|
| 111 | version=None, tags=None): |
|---|
| 112 | """ |
|---|
| 113 | Serialize a sequence of representation trees into a YAML stream. |
|---|
| 114 | If stream is None, return the produced string instead. |
|---|
| 115 | """ |
|---|
| 116 | getvalue = None |
|---|
| 117 | if stream is None: |
|---|
| 118 | try: |
|---|
| 119 | from cStringIO import StringIO |
|---|
| 120 | except ImportError: |
|---|
| 121 | from StringIO import StringIO |
|---|
| 122 | stream = StringIO() |
|---|
| 123 | getvalue = stream.getvalue |
|---|
| 124 | dumper = Dumper(stream, canonical=canonical, indent=indent, width=width, |
|---|
| 125 | allow_unicode=allow_unicode, line_break=line_break, |
|---|
| 126 | encoding=encoding, version=version, tags=tags, |
|---|
| 127 | explicit_start=explicit_start, explicit_end=explicit_end) |
|---|
| 128 | dumper.open() |
|---|
| 129 | for node in nodes: |
|---|
| 130 | dumper.serialize(node) |
|---|
| 131 | dumper.close() |
|---|
| 132 | if getvalue: |
|---|
| 133 | return getvalue() |
|---|
| 134 | |
|---|
| 135 | def serialize(node, stream=None, Dumper=Dumper, **kwds): |
|---|
| 136 | """ |
|---|
| 137 | Serialize a representation tree into a YAML stream. |
|---|
| 138 | If stream is None, return the produced string instead. |
|---|
| 139 | """ |
|---|
| 140 | return serialize_all([node], stream, Dumper=Dumper, **kwds) |
|---|
| 141 | |
|---|
| 142 | def dump_all(documents, stream=None, Dumper=Dumper, |
|---|
| 143 | default_style=None, default_flow_style=None, |
|---|
| 144 | canonical=None, indent=None, width=None, |
|---|
| 145 | allow_unicode=None, line_break=None, |
|---|
| 146 | encoding='utf-8', explicit_start=None, explicit_end=None, |
|---|
| 147 | version=None, tags=None): |
|---|
| 148 | """ |
|---|
| 149 | Serialize a sequence of Python objects into a YAML stream. |
|---|
| 150 | If stream is None, return the produced string instead. |
|---|
| 151 | """ |
|---|
| 152 | getvalue = None |
|---|
| 153 | if stream is None: |
|---|
| 154 | try: |
|---|
| 155 | from cStringIO import StringIO |
|---|
| 156 | except ImportError: |
|---|
| 157 | from StringIO import StringIO |
|---|
| 158 | stream = StringIO() |
|---|
| 159 | getvalue = stream.getvalue |
|---|
| 160 | dumper = Dumper(stream, default_style=default_style, |
|---|
| 161 | default_flow_style=default_flow_style, |
|---|
| 162 | canonical=canonical, indent=indent, width=width, |
|---|
| 163 | allow_unicode=allow_unicode, line_break=line_break, |
|---|
| 164 | encoding=encoding, version=version, tags=tags, |
|---|
| 165 | explicit_start=explicit_start, explicit_end=explicit_end) |
|---|
| 166 | dumper.open() |
|---|
| 167 | for data in documents: |
|---|
| 168 | dumper.represent(data) |
|---|
| 169 | dumper.close() |
|---|
| 170 | if getvalue: |
|---|
| 171 | return getvalue() |
|---|
| 172 | |
|---|
| 173 | def dump(data, stream=None, Dumper=Dumper, **kwds): |
|---|
| 174 | """ |
|---|
| 175 | Serialize a Python object into a YAML stream. |
|---|
| 176 | If stream is None, return the produced string instead. |
|---|
| 177 | """ |
|---|
| 178 | return dump_all([data], stream, Dumper=Dumper, **kwds) |
|---|
| 179 | |
|---|
| 180 | def safe_dump_all(documents, stream=None, **kwds): |
|---|
| 181 | """ |
|---|
| 182 | Serialize a sequence of Python objects into a YAML stream. |
|---|
| 183 | Produce only basic YAML tags. |
|---|
| 184 | If stream is None, return the produced string instead. |
|---|
| 185 | """ |
|---|
| 186 | return dump_all(documents, stream, Dumper=SafeDumper, **kwds) |
|---|
| 187 | |
|---|
| 188 | def safe_dump(data, stream=None, **kwds): |
|---|
| 189 | """ |
|---|
| 190 | Serialize a Python object into a YAML stream. |
|---|
| 191 | Produce only basic YAML tags. |
|---|
| 192 | If stream is None, return the produced string instead. |
|---|
| 193 | """ |
|---|
| 194 | return dump_all([data], stream, Dumper=SafeDumper, **kwds) |
|---|
| 195 | |
|---|
| 196 | def add_implicit_resolver(tag, regexp, first=None, |
|---|
| 197 | Loader=Loader, Dumper=Dumper): |
|---|
| 198 | """ |
|---|
| 199 | Add an implicit scalar detector. |
|---|
| 200 | If an implicit scalar value matches the given regexp, |
|---|
| 201 | the corresponding tag is assigned to the scalar. |
|---|
| 202 | first is a sequence of possible initial characters or None. |
|---|
| 203 | """ |
|---|
| 204 | Loader.add_implicit_resolver(tag, regexp, first) |
|---|
| 205 | Dumper.add_implicit_resolver(tag, regexp, first) |
|---|
| 206 | |
|---|
| 207 | def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper): |
|---|
| 208 | """ |
|---|
| 209 | Add a path based resolver for the given tag. |
|---|
| 210 | A path is a list of keys that forms a path |
|---|
| 211 | to a node in the representation tree. |
|---|
| 212 | Keys can be string values, integers, or None. |
|---|
| 213 | """ |
|---|
| 214 | Loader.add_path_resolver(tag, path, kind) |
|---|
| 215 | Dumper.add_path_resolver(tag, path, kind) |
|---|
| 216 | |
|---|
| 217 | def add_constructor(tag, constructor, Loader=Loader): |
|---|
| 218 | """ |
|---|
| 219 | Add a constructor for the given tag. |
|---|
| 220 | Constructor is a function that accepts a Loader instance |
|---|
| 221 | and a node object and produces the corresponding Python object. |
|---|
| 222 | """ |
|---|
| 223 | Loader.add_constructor(tag, constructor) |
|---|
| 224 | |
|---|
| 225 | def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader): |
|---|
| 226 | """ |
|---|
| 227 | Add a multi-constructor for the given tag prefix. |
|---|
| 228 | Multi-constructor is called for a node if its tag starts with tag_prefix. |
|---|
| 229 | Multi-constructor accepts a Loader instance, a tag suffix, |
|---|
| 230 | and a node object and produces the corresponding Python object. |
|---|
| 231 | """ |
|---|
| 232 | Loader.add_multi_constructor(tag_prefix, multi_constructor) |
|---|
| 233 | |
|---|
| 234 | def add_representer(data_type, representer, Dumper=Dumper): |
|---|
| 235 | """ |
|---|
| 236 | Add a representer for the given type. |
|---|
| 237 | Representer is a function accepting a Dumper instance |
|---|
| 238 | and an instance of the given data type |
|---|
| 239 | and producing the corresponding representation node. |
|---|
| 240 | """ |
|---|
| 241 | Dumper.add_representer(data_type, representer) |
|---|
| 242 | |
|---|
| 243 | def add_multi_representer(data_type, multi_representer, Dumper=Dumper): |
|---|
| 244 | """ |
|---|
| 245 | Add a representer for the given type. |
|---|
| 246 | Multi-representer is a function accepting a Dumper instance |
|---|
| 247 | and an instance of the given data type or subtype |
|---|
| 248 | and producing the corresponding representation node. |
|---|
| 249 | """ |
|---|
| 250 | Dumper.add_multi_representer(data_type, multi_representer) |
|---|
| 251 | |
|---|
| 252 | class YAMLObjectMetaclass(type): |
|---|
| 253 | """ |
|---|
| 254 | The metaclass for YAMLObject. |
|---|
| 255 | """ |
|---|
| 256 | def __init__(cls, name, bases, kwds): |
|---|
| 257 | super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds) |
|---|
| 258 | if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None: |
|---|
| 259 | cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) |
|---|
| 260 | cls.yaml_dumper.add_representer(cls, cls.to_yaml) |
|---|
| 261 | |
|---|
| 262 | class YAMLObject(object): |
|---|
| 263 | """ |
|---|
| 264 | An object that can dump itself to a YAML stream |
|---|
| 265 | and load itself from a YAML stream. |
|---|
| 266 | """ |
|---|
| 267 | |
|---|
| 268 | __metaclass__ = YAMLObjectMetaclass |
|---|
| 269 | __slots__ = () # no direct instantiation, so allow immutable subclasses |
|---|
| 270 | |
|---|
| 271 | yaml_loader = Loader |
|---|
| 272 | yaml_dumper = Dumper |
|---|
| 273 | |
|---|
| 274 | yaml_tag = None |
|---|
| 275 | yaml_flow_style = None |
|---|
| 276 | |
|---|
| 277 | def from_yaml(cls, loader, node): |
|---|
| 278 | """ |
|---|
| 279 | Convert a representation node to a Python object. |
|---|
| 280 | """ |
|---|
| 281 | return loader.construct_yaml_object(node, cls) |
|---|
| 282 | from_yaml = classmethod(from_yaml) |
|---|
| 283 | |
|---|
| 284 | def to_yaml(cls, dumper, data): |
|---|
| 285 | """ |
|---|
| 286 | Convert a Python object to a representation node. |
|---|
| 287 | """ |
|---|
| 288 | return dumper.represent_yaml_object(cls.yaml_tag, data, cls, |
|---|
| 289 | flow_style=cls.yaml_flow_style) |
|---|
| 290 | to_yaml = classmethod(to_yaml) |
|---|
| 291 | |
|---|