2ec2f0849b6d12c8cdc80dc222ee6c875875e089
1 """BulletYAML implementation.
3 BulletYAML is a translation of BulletML into YAML. The structure is
4 mostly the same as the XML version, except BulletRef/FireRef/ActionRef
5 elements are only used if they contain parameters, as YAML has its own
6 intra-document references. Parameterless references are turned into
7 direct YAML references.
9 If PyYAML is installed, importing this module automatically registers
10 BulletYAML tags with the default loader and dumper.
12 Example BulletYAML document:
23 from bulletml
import parser
25 def register(Loader
=None, Dumper
=None):
26 """Register BulletYAML types for a Loader and Dumper."""
27 for cls
in [parser
.Direction
, parser
.ChangeDirection
,
28 parser
.Speed
, parser
.ChangeSpeed
, parser
.Wait
,
29 parser
.Tag
, parser
.Untag
, parser
.Vanish
,
30 parser
.Repeat
, parser
.Accel
, parser
.BulletDef
,
31 parser
.BulletRef
, parser
.ActionDef
, parser
.ActionRef
,
32 parser
.FireDef
, parser
.FireRef
, parser
.Offset
,
33 parser
.Appearance
, parser
.If
, parser
.BulletML
]:
35 def add(cls
, loader
, dumper
):
36 """Register a class in a new variable scope."""
37 tag
= "!" + cls
.__name
__
39 def construct(loader
, node
):
40 """Construct an object."""
41 return loader
.construct_yaml_object(node
, cls
)
42 loader
.add_constructor(tag
, construct
)
44 def represent(dumper
, obj
):
45 """Represent an object."""
46 return dumper
.represent_yaml_object(tag
, obj
, cls
)
47 dumper
.add_representer(cls
, represent
)
49 add(cls
, Loader
, Dumper
)