Source

pysyck / README.txt

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
============================================================
PySyck: Python bindings for the Syck YAML parser and emitter
============================================================

:Author: Kirill Simonov
:Contact: xi@resolvent.net
:Web site: http://pyyaml.org/wiki/PySyck

.. contents::


Overview
========

YAML_ is a data serialization format designed for human readability and
interaction with scripting languages.

Syck_ is an extension for reading and writing YAML in scripting languages. Syck
provides bindings to the Python_ programming language, but they are somewhat
limited and leak memory.

PySyck_ is aimed to update the current Python bindings for Syck. The new
bindings provide a wrapper for the Syck emitter and give access to YAML
representation graphs. Hopefully it will not leak memory as well.

PySyck_ may be used for various tasks, in particular, as a replacement of the
module pickle_.

.. _YAML: http://yaml.org/
.. _Syck: http://whytheluckystiff.net/syck/
.. _Python: http://python.org/
.. _PySyck: http://pyyaml.org/wiki/PySyck
.. _pickle: http://docs.python.org/lib/module-pickle.html


Requirements
============

PySyck requires Syck 0.55 or higher and Python 2.3 or higher.


Installation
============

Please note that Syck 0.55 or higher must be installed. We recommend to use
Syck_ from `the Syck SVN repository`_ together with `my Syck patches`_. For
your convenience, a tarball is provided:
http://pyyaml.org/download/pysyck/syck-0.61+svn232+patches.tar.gz.

If you install PySyck from source, unpack the source tarball and type::

  $ python setup.py install

Windows binaries for Python 2.3 and 2.4 are provided. Windows binaries are
linked against Syck_ statically.

.. _the Syck SVN repository: http://code.whytheluckystiff.net/svn/syck/trunk
.. _my Syck patches: http://pyyaml.org/wiki/SyckPatches


Usage
=====

The documentation is still rough and incomplete. See `the source code`_ for
more information.

.. _the source code: http://pyyaml.org/browser/pysyck/

Quick Example
-------------

::

  >>> from syck import *
  >>> print load("""
  ... - foo
  ... - bar
  ... - baz
  ... """)
  ['foo', 'bar', 'baz']
  >>> print dump(['foo', 'bar', 'baz'])
  ---
  - foo
  - bar
  - baz

YAML syntax
-----------

We do not describe the YAML syntax here. Please check http://yaml.org/ for the
reference.

In addition to the tags defined in `the YAML types repository`_, PySyck understands
the following Python-specific tags:

* ``tag:python.yaml.org,2002:none``,
* ``tag:python.yaml.org,2002:bool``,
* ``tag:python.yaml.org,2002:int``,
* ``tag:python.yaml.org,2002:float``,
* ``tag:python.yaml.org,2002:str``,
* ``tag:python.yaml.org,2002:unicode``,
* ``tag:python.yaml.org,2002:list``,
* ``tag:python.yaml.org,2002:tuple``,
* ``tag:python.yaml.org,2002:dict``,
* ``tag:python.yaml.org,2002:name:...``,
* ``tag:python.yaml.org,2002:object:...``,
* ``tag:python.yaml.org,2002:new:...``,
* ``tag:python.yaml.org,2002:apply:...``.

Most of these tags are self-explanatory. The tags ``!python/name:...``,
``!python/object:...``, ``!python/new:...``, and ``!python/apply:...`` are used
for constructing Python functions, classes, and objects. See the sections `Use
Python-specific tags in YAML documents`_ and `Use Python-specific tags to
construct Python objects`_ for some examples.

.. _the YAML types repository: http://yaml.org/type/index.html

Common Tasks
------------

Import the module
~~~~~~~~~~~~~~~~~

::

  >>> from syck import *

or

::

  >>> import syck

Load a document from a string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> source = "..."
  >>> object = load(source)

Load a document from a file
~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> source = file(..., 'r')
  >>> object = load(source)

Convert a Python object to a YAML document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> object = ...
  >>> document = dump(object)

Dump a Python object to a YAML stream
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> object = ...
  >>> output = file(..., 'w')
  >>> dump(object, output)

Format the output YAML stream
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> object = ...
  >>> output = file(..., 'w')
  >>> dump(object, output,
  ...     headless=False, use_header=False, use_version=False,
  ...     explicit_typing=True, style=None, best_width=80, indent=2)

Load several documents from a YAML stream
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> source = ...
  >>> objects = load_documents(source)
  >>> for object in objects:
  ...     # ...

Create several documents in a YAML stream
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> objects = [...]
  >>> output = file(..., 'w')
  >>> dump_documents(objects, output)

Construct a representation tree of a YAML document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> source = ...
  >>> root_node = parse(source)

Convert a representation tree to a YAML document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> scalar_node = Scalar('...', tag='tag:...',
  ...     style='...', indent=.., width=..)
  >>> sequence_node = Seq(list_of_nodes, tag='tag:...', inline=..)
  >>> mapping_node = Map(dictionary_of_nodes, tag='tag:...', inline=..)
  >>> root_node = ...
  >>> output = file(..., 'w')
  >>> emit(root_node, output)

Use PySyck as a pickle replacement
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> object = ...
  >>> stream = ...
  >>> dump(object, stream)
  
  >>> stream = ...
  >>> object = load(stream)

Use PySyck to display the structure of a complex object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> object = ...
  >>> print dump(object)

Use PySyck to display a YAML representation graph
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> source = ...
  >>> node = parse(source)
  >>> print dump(node)

Use Python-specific tags in YAML documents
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  --- %YAML:1.0
  - !python/none ''       # You may also use '!null'.
  - !python/bool 'False'  # You may also use '!bool'.
  - !python/int '123'     # You may also use '!int'.
  - !python/long '1234567890'
  - !python/float '123.456789'  # Also '!float'.
  - !python/str 'a string'      # Also '!str'.
  - !python/unicode 'a unicode string encoded in utf-8'
  - !python/list [1, 2, 3]      # The same as '!seq' or no tag.
  - !python/tuple [1, 2, 3]
  - !python/dict { 1: foo, 2: bar } # The same as '!map' or no tag.

Use Python-specific tags to construct functions or classes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  --- %YAML:1.0
  - !python/name:package.module.function_name ''
  - !python/name:package.module.class_name ''

Use Python-specific tags to construct Python objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  --- %YAML:1.0
  - !python/object:package.module.type
    attribute1: value1
    attribute2: value2
    # ...
  - !python/new:package.module.type
    - parameter1
    - parameter2
    # ...
  - !python/new:package.module.type
    args: [parameter1, parameter2, ...]
    kwds: {kwd1: val1, kwd2: val2, ...}
    state: {attr1: val1, attr2: val2, ...}
    # ...
  - !python/apply:package.module.function
    - parameter1
    - parameter2
    # ...
  - !python/apply:package.module.function
    args: [parameter1, parameter2, ...]
    kwds: {kwd1: val1, kwd2: val2, ...}
    state: {attr1: val1, attr2: val2, ...}
    # ...

Use application specific tags
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

  >>> class MyClass:
  ...   # ...

  >>> class MyLoader(Loader):
  ...     def construct_private_my_tag(self, node):
  ...         # ...
  ...         return MyClass(...)

  >>> class MyDumper(Dumper):
  ...     def represent_MyDumper(self, object):
  ...         # ...
  ...         return Map(...)

  >>> source = """--- !!my_tag { ... }"""
  >>> my_instance = load(source, Loader=MyLoader)

  >>> my_instance = MyClass(...)
  >>> output = dump(my_instance, Dumper=MyDumper)

Reference
---------

Functions
~~~~~~~~~

``load`` : function
  ``load(source, Loader=Loader, **parameters)``

  The function ``load()`` returns a Python object corresponding to the first
  document in the source. If the source is empty, ``load()`` returns ``None``.
  ``source`` must be a string or a file-like object that has the method
  ``read(max_length)``.

  By default, the function ``load()`` uses an instance of the class ``Loader``
  for parsing. You may use another class or pass additional parameters to the
  class constructor. See the section Parser_ for more details.

  Example::

    >>> load("""
    ... - foo
    ... - bar
    ... - baz
    ... """)
    ['foo', 'bar', 'baz']

``parse`` : function
  ``parse(source, Loader=Loader, **parameters)``

  The function ``parse()`` parses the source and returns a representation tree
  of the first document. ``source`` must be a string or a file-like object
  that has the method ``read(max_length)``.

  By default, the function ``parse()`` uses an instance of the class ``Loader``
  for parsing. You may use another class or pass additional parameters to the
  class constructor. See the section Parser_ for more details.

  Example::

    >>> parse("""
    ... - foo
    ... - bar
    ... - baz
    ... """)
    <_syck.Seq object at 0xb7a3f2fc>

``load_documents`` : function
  ``load_documents(source, Loader=Loader, **parameters)``

  The function ``load_documents()`` parses the source and an iterator.  The
  iterator produces Python objects corresponding the documents of the source
  stream. ``source`` must be a string or a file-like object that has the method
  ``read(max_length)``.

  By default, the function ``load_documents()`` uses an instance of the class
  ``Loader`` for parsing. You may use another class or pass additional
  parameters to the class constructor. See the section Parser_ for more
  details.

  Example::

    >>> source = """
    ... --- >
    ... This is the
    ... first document.
    ... --- >
    ... This is the
    ... next document.
    ... --- >
    ... This is the
    ... last document.
    ... """
    >>> for object in load_documents(source): print object
    ...
    This is the first document.

    This is the next document.

    This is the last document.

``parse_documents`` : function
  ``parse_documents(source, Loader=Loader, **parameters)``

  The function ``parse_documents()`` is similar to ``load_documents()``, but
  produces representation graphs for all documents in the source.
  
``dump`` : function
  ``dump(object, output=None, Dumper=Dumper, **parameters)``

  The function ``dump()`` converts ``object`` to a representation graph
  and write it to ``output``. ``output`` must be ``None`` or a file-like
  object that has the method ``write(data)``. If ``output`` is ``None``,
  ``dump()`` returns the generated document.

  By default, the function ``dump()`` uses an instance of the class ``Dumper``
  for emitting. You may use another class or pass additional parameters to the
  class constructor. See the section Emitter_ for more details.

  Example::

    >>> object = ['foo', 'bar', ['baz']]
    >>> dump(object, sys.stdout)
    ---
    - foo
    - bar
    - - baz
    >>> print dump(object)
    ---
    - foo
    - bar
    - - baz

    >>> print dump(object, use_version=True, indent=5)
    --- %YAML:1.0
    - foo
    - bar
    -    - baz

``emit`` : function
  ``emit(node, output=None, Dumper=Dumper, **parameters)``

  The function ``emit()`` write the representation graph to the output stream.
  ``output`` must be ``None`` or a file-like object that has the method
  ``write(data)``. If ``output`` is ``None``, ``emit()`` returns the generated
  document.

  By default, the function ``emit()`` uses an instance of the class ``Dumper``
  for emitting. You may use another class or pass additional parameters to the
  class constructor. See the section Emitter_ for more details.

  Example::

    >>> foo = Scalar('a string')
    >>> bar = Scalar('a unicode string', tag="tag:python.yaml.org,2002:unicode")
    >>> baz = Scalar('12345', tag="tag:yaml.org,2002:int")
    >>> seq = Seq([foo, bar, baz], tag="tag:python.taml.org,2002:tuple")
    >>> print emit(seq, use_version=True)
    --- %YAML:1.0 !python.taml.org,2002/tuple
    - a string
    - !python/unicode a unicode string
    - 12345

``dump_documents`` : function
  ``dump_documents(objects, output=None, Dumper=Dumper, **parameters)``

  The function ``dump_documents()`` takes a list of objects and converts
  each object to a YAML document. If ``output`` is ``None``, it returns
  the produced documents. Otherwise it writes down them to ``output``,
  which must be a file-like object with the method ``write(data)``.

  By default, the function ``dump_documents()`` uses an instance of the class
  ``Dumper`` for emitting. You may use another class or pass additional
  parameters to the class constructor. See the section Emitter_ for more
  details.

  Example::

    >>> print dump_documents(['foo', 'bar', 'baz'])
    --- foo
    --- bar
    --- baz

``emit_documents`` : function
  ``emit_documents(nodes, output=None, Dumper=Dumper, **parameters)``

  The function ``emit_documents()`` is similar to ``dump_documents()``, but
  it requires a list of representation graphs.

Exceptions
~~~~~~~~~~

``error`` : exception
  This exception is raised by the Syck parser when it detects a syntax error.

  The attribute ``args`` of the exception is a triple: *message*, *row*,
  *column*.

  Example::

    >>> load("""---
    ... - foo
    ... - '''
    ... - bar
    ... """)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "build/lib.linux-i686-2.3/syck/loaders.py", line 384, in load
      File "build/lib.linux-i686-2.3/syck/loaders.py", line 42, in load
    _syck.error: ('syntax error', 4, 2)

Nodes
~~~~~

The following four classes represents nodes in the YAML representation graph:

``Node`` : class
  ``Node`` is an abstract class; you cannot create an instance of the class
  ``Node``. ``Node`` is the base class of ``Scalar``, ``Seq``, and ``Map``.

``Scalar`` : subclass of ``Node``
  ``Scalar`` represents a scalar node. Its value is a string.

``Seq`` : subclass of ``Node``
  ``Seq`` represents a sequence node. Its value is a list of nodes.

``Map`` : subclass of ``Node``
  ``Map`` represents a mapping node. Its value is a list of pairs or a
  dictionary.

All instances of ``Scalar``, ``Seq``, and ``Map`` have the following
attributes:

``kind`` : string
  ``'scalar'``, ``'seq'``, or ``'map'``; read-only.

``anchor`` : string or ``None``
  The node anchor.

``tag`` : string or ``None``
  The node tag.

``value``
  The node value. For scalar nodes, it should be a string. For sequence nodes,
  it should be a list. For mapping nodes, it should be a list of pairs or a
  dictionary.

``Scalar`` instances have additional attributes:

``style`` : string or ``None``
  The node style. Possible values are ``None`` (means literal or plain),
  ``'1quote'``, ``'2quote'``, ``'fold'``, ``'literal'``, ``'plain'``.

``indent`` : integer
  The node indentation. ``0`` means the default value.

``width`` : integer
  The width of the node field. Longer scalars will be broken on several lines
  to fit the field. ``0`` means the default value.

``chomp`` : string or ``None``
  The chomping method. Possible values are ``None`` (clip), ``'-'`` (strip),
  ``'+'`` (keep).

``Seq`` and ``Map`` instances have an additional attribute:

``inline`` : boolean
  The block/flow flag.

For example, let us create a representation graph and transform it into a YAML
stream::

  >>> # Create three scalar nodes:
  >>> foo = Scalar('foo', tag="tag:example.com,2005:foo", style='fold',
  ...     indent=5)
  >>> bar = Scalar('bar', style='1quote')
  >>> baz = Scalar('baz')

  >>> # Create a sequence node:
  >>> seq = Seq([foo, bar, baz], tag="x-private:seq")

  >>> # Emit it into a YAML stream:
  >>> print emit(seq)
  --- !!seq
  - !example.com,2005/foo >-
       foo
  - 'bar'
  - baz

Now let us construct a representation graph from a YAML document::

  >>> # The function 'parse' generates a representation graph:
  >>> root = parse("""
  ... - foo
  ... - bar
  ... - baz
  ... """)

  >>> # The object 'root' is a sequence node:
  >>> root
  <_syck.Seq object at 0xb7e124b4>

  >>> # We can transform 'root' back into a YAML stream:
  >>> print emit(root)
  ---
  - foo
  - bar
  - baz

  >>> # We can also display the structure of the representation tree using a
  >>> # clever trick:
  >>> print dump(root)
  --- !python/object:_syck.Seq
  value:
  - !python/object:_syck.Scalar
    value: foo
    tag: tag:yaml.org,2002:str
  - !python/object:_syck.Scalar
    value: bar
    tag: tag:yaml.org,2002:str
  - !python/object:_syck.Scalar
    value: baz
    tag: tag:yaml.org,2002:str

Parser
~~~~~~

``Parser`` : class
  The class ``Parser`` is a low-level wrapper of a Syck YAML parser. It can
  generate a representation graph from a YAML stream.

  The class constructor has the following arguments:

  * ``Parser(source, implicit_typing=True, taguri_expansion=True)``.

  The parameter ``source`` is a YAML stream. It must be a string
  or a file-like object. If it is not a string, it should have a
  method named ``read(max_length)`` that returns a string.

  It is not recommended to change the default values of the parameters
  ``implicit_typing`` and ``taguri_expansion``. See the Syck documentation
  for more details about them.

  The class defines a single method:

  * ``Parser.parse()``.

  It parses the source and returns the root node of the corresponding
  representation graph. If the stream is finished, it returns ``None`` and
  set the flag ``eof`` on.

``GenericLoader`` : subclass of ``Parser``
  The subclass ``GenericLoader`` defines two additional methods:

  * ``GenericLoader.load()``,

  * ``GenericLoader.construct(node)``.

  The method ``load()`` parses the source and constructs the corresponding
  Python object. To generate an object by a node, ``load()`` uses the
  ``construct()`` method. The ``construct()`` method defined in
  ``GenericLoader`` just returns the value of the node: a string, a list,
  or a dictionary.

``Loader`` : subclass of ``GenericLoader``
  
  ``Loader`` redefines the method

  * ``Loader.construct(node)``,

  defines an additional method:

  * ``Loader.find_constructor(node)``,

  and add many other auxiliary methods for constructing Python objects.

  ``Loader.construct()`` calls ``find_constructor()`` for the given node,
  and uses the returned constructor to generate a Python object.

  ``Loader.find_constructor()`` determines the constructor of a node by
  the following rules:

  * If the node tag has the form ``tag:yaml.org,2002:type_id``, returns the
    method ``Loader.construct_type_id``.

  * If the node tag has the form ``tag:python.yaml.org,2002:type_id``, returns
    the method ``Loader.construct_python_type_id``.

  * If the node tag has the form ``x-private:type_id``, returns
    ``Loader.construct_private_type_id``.

  * If the node tag has the form ``tag:domain.tld,year:type_id``, returns
    ``Loader.construct_domain_tld_year_type_id``.

  See the source for more details.

Let us show how ``Parser``, ``GenericLoader``, and ``Loader`` parse the same
document::

  >>> # The source stream includes PySyck specific tags '!python/tuple'
  >>> # and '!python/unicode'. It also includes implicitly typed integer
  >>> # '12345'
  >>> source = """--- !python/tuple
  ... - a string
  ... - !python/unicode a unicode string
  ... - 12345
  ... """

  >>> # 'Parser.parse()' returns the root node of the representation tree:
  >>> p = Parser(source)
  >>> print p.parse()
  <_syck.Seq object at 0xb7a33f54>

  >>> # 'GenericLoader.load()' returns a Python object, but ignores the tags:
  >>> gl = GenericLoader(source)
  >>> print gl.load()
  ['a string', 'a unicode string', '12345']

  >>> # 'Loader.load()' is aware of the tags:
  >>> l = Loader(source)
  >>> print l.load()
  ('a string', u'a unicode string', 12345)

Emitter
~~~~~~~

``Emitter`` : class
  The class ``Emitter`` is a low-level wrapper of a Syck YAML emitter. It can
  generate a YAML stream from a representation graph.

  The class constructor has the following signature:

  * ``Emitter(output, headless=False, use_header=False, use_version=False,
    explicit_typing=True, style=None, best_width=80, indent=2)``.

  The parameter ``output`` must be a file-like object that provides a method
  ``write(data)``. The other parameters describe the formatting of the output
  document.

  The class defines a single method:

  * ``emit(node)``.

  The parameter ``node`` must be the root node of a YAML representation graph.
  The method ``emit()`` writes the generated YAML document to the ``output``
  stream.

``GenericDumper`` : subclass of ``Emitter``
  The subclass ``GenericDumper`` adds the following methods:

  * ``GenericDumper.dump(object)``,

  * ``GenericDumper.represent(object)``,

  * ``GenericDumper.allow_aliases(object)``.

  The method ``dump()`` converts the given object into a representation graph,
  generates a YAML document, and writes it to the ``output`` stream. It uses
  the method ``represent()`` to convert an object to a representation node.
  The method ``represent()`` defined in ``GenericDumper`` generates a sequence
  node for a list object and a mapping node for a dictionary object. Otherwise
  it generates a scalar node with the value equal to ``str(object)``.

  The Syck YAML emitter automatically detects if the same object is reffered
  from different parts of the graph and generates aliases for it. Unfortunately
  it does not work well with immutable Python objects such as strings, numbers,
  and tuples. To prevent generating unnecessary aliases, the method
  ``allow_aliases()`` is used. If ``allow_aliases()`` for a given object
  returns ``False``, the alias will never be generated.

  The ``allow_aliases()`` method defined in ``GenericDumper`` always returns
  ``True``.

``Dumper`` : subclass of ``GenericDumper``
  The subclass ``Dumpers`` redefines the methods:

  * ``Dumper.represent(object)``,

  * ``Dumper.allow_aliases(object)``,

  defines the method

  * ``Dumper.find_representer(object)``,

  and add many other auxiliary methods for representing objects as nodes.

  ``Dumper.find_representer()`` finds a method that can represent the given
  object as a node in a representation tree. ``find_representer()`` checks the
  class of the object. If the class has the form ``package.module.type``,
  ``find_representer()`` returns the method
  ``Dumper.represent_package_module_type`` if it exists. If this method does
  not exists, ``find_representer()`` consults its base class, and so on.

  ``Dumper.represent()`` calls ``Dumper.find_representer()`` for the given
  object and uses the returned method to generate a representation node.

  See the source for more details.

Let us show how ``Emitter``, ``GenericDumper``, and ``Dumper`` work::

  >>> # For our demonstration, we define a representation tree named 'seq'
  >>> # and a Python tuple named 'object':
  >>> foo = Scalar('a string')
  >>> bar = Scalar('a unicode string', tag="tag:python.yaml.org,2002:unicode")
  >>> baz = Scalar('12345', tag="tag:yaml.org,2002:int")
  >>> seq = Seq([foo, bar, baz], tag="tag:python.taml.org,2002:tuple")
  >>> object = ('a string', u'a unicode string', 12345)

  >>> # An 'Emitter' instance can dump a representation tree into a stream,
  >>> # but obviously failed to dump a Python object:
  >>> e = Emitter(sys.stdout)
  >>> e.emit(seq)
  --- !python.taml.org,2002/tuple
  - a string
  - !python/unicode a unicode string
  - 12345
  >>> e.emit(object)
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  TypeError: Node instance is required

  >>> # A 'GenericDumper' instance dumps almost everything as a scalar:
  >>> gd = GenericDumper(sys.stdout)
  >>> gd.dump(seq)
  --- <_syck.Seq object at 0xb7a3c2fc>
  >>> gd.dump(object)
  --- ('a string', u'a unicode string', 12345)

  >>> # Finally, a 'Dumper' instance dumps a representation tree as a complex
  >>> # Python object:
  >>> d = Dumper(sys.stdout)
  >>> d.dump(seq)
  --- !python/object:_syck.Seq
  value:
  - !python/object:_syck.Scalar
    value: a string
  - !python/object:_syck.Scalar
    value: a unicode string
    tag: tag:python.yaml.org,2002:unicode
  - !python/object:_syck.Scalar
    value: "12345"
    tag: tag:yaml.org,2002:int
  tag: tag:python.taml.org,2002:tuple
  >>> # It also dumps the 'object' object as expected:
  >>> d.dump(object)
  --- !python/tuple
  - a string
  - !python/unicode a unicode string
  - 12345


Development and Bug Reports
===========================

You may check out the PySyck_ source code from `PySyck SVN repository`_.

If you find a bug in PySyck_, please file a bug report to `PySyck BTS`_. You
may review open bugs on `the list of active tickets`_.

You may use `YAML-core mailing list`_ for discussions of PySyck_.

.. _PySyck SVN repository: http://svn.pyyaml.org/pysyck/
.. _PySyck BTS: http://pyyaml.org/newticket?component=pysyck
.. _the list of active tickets: http://pyyaml.org/query?action=view&component=pysyck&order=priority
.. _YAML-core mailing list: http://lists.sourceforge.net/lists/listinfo/yaml-core


Known Bugs
==========

PySyck_ does not support Unicode for real. It is a Syck_ limitation.


History
=======

* PySyck-0.61.1 (2006-03-XX):

  - ``setup.py``: check if ``syck.h`` is present, complain if it doesn't.
  - ``ext/_syckmodule.c``: release GIL_ before calling Syck_. Note that this
    change broke Python 2.2 compatibility.
  - ``lib/syck/loader.py``, ``lib/syck/dumper.py``: change treatment of the
    ``!str`` tag. Now ``!str``-tagged scalars are converted to Unicode strings
    if they are valid UTF-8, but are not valid ASCII.
  - Windows binaries are compiled against the latest Syck_ from
    `the Syck SVN repository`_ with `my Syck patches`_.
  - The site is moved to http://pyyaml.org/wiki/PySyck.

* PySyck-0.55.1 (2005-08-30): Initial release.

.. _GIL: http://docs.python.org/api/threads.html


Author and Copyright
====================

The PySyck_ module was written by `Kirill Simonov`_.

PySyck_ is released under the BSD license as Syck_ itself.

.. _Kirill Simonov: mailto:xi@resolvent.net

..
  vim: ft=rst:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.