From 29b24c8fc17a3bdcccd1ac853eee5b90c6fd0856 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 18 Mar 2010 21:49:06 -0700 Subject: [PATCH] YAML wrapper. --- bulletml-to-bulletyaml | 13 +++++++++++ bulletml/__init__.py | 3 +++ bulletml/bulletyaml.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100755 bulletml-to-bulletyaml create mode 100644 bulletml/bulletyaml.py diff --git a/bulletml-to-bulletyaml b/bulletml-to-bulletyaml new file mode 100755 index 0000000..c5f8c56 --- /dev/null +++ b/bulletml-to-bulletyaml @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +import sys +import yaml + +from bulletml import BulletML, bulletyaml + +def main(argv): + for filename in argv: + print yaml.dump(BulletML.FromDocument(open(filename, "rU"))) + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/bulletml/__init__.py b/bulletml/__init__.py index 1e1dbcd..c87a442 100644 --- a/bulletml/__init__.py +++ b/bulletml/__init__.py @@ -10,6 +10,9 @@ More information is available at the BulletML homepage, http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html, or the python-bullet homepage, http://code.google.com/p/python-bulletml/. +If you want to use a YAML-based implementation, check out the +bulletml.bulletyaml module in this package. + Basic Usage: from bulletml import Bullet, BulletML diff --git a/bulletml/bulletyaml.py b/bulletml/bulletyaml.py new file mode 100644 index 0000000..ea45436 --- /dev/null +++ b/bulletml/bulletyaml.py @@ -0,0 +1,53 @@ +"""BulletYAML implementation. + +BulletYAML is a translation of BulletML into YAML. The structure is +mostly the same as the XML version, except BulletRef/FireRef/ActionRef +elements are only used if they contain parameters, as YAML has its own +intra-document references. Parameterless references are turned into +direct YAML references. + +If PyYAML is installed, importing this module automatically registers +BulletYAML tags with the default loader and dumper. + +Example BulletYAML document: + !BulletML + type: vertical + actions: + top: !ActionDef + actions: + - !FireDef + bullet: !BulletDef {} + +""" + +from bulletml import parser + +def register(Loader=None, Dumper=None): + """Register BulletYAML types for a Loader and Dumper.""" + for cls in [parser.Direction, parser.ChangeDirection, + parser.Speed, parser.ChangeSpeed, parser.Wait, + parser.Tag, parser.Untag, parser.Vanish, + parser.Repeat, parser.Accel, parser.BulletDef, + parser.BulletRef, parser.ActionDef, parser.ActionRef, + parser.FireDef, parser.FireRef, parser.Offset, + parser.BulletML]: + + def add(cls, loader, dumper): + tag = "!" + cls.__name__ + if loader: + def construct(loader, node): + return loader.construct_yaml_object(node, cls) + loader.add_constructor(tag, construct) + if dumper: + def represent(dumper, obj): + return dumper.represent_yaml_object(tag, obj, cls) + dumper.add_representer(cls, represent) + + add(cls, Loader, Dumper) + +try: + import yaml +except ImportError: + pass +else: + register(yaml, yaml) -- 2.30.2