X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=2ad648c20c97eb16e53a08b2c17c7a453b19799a;hp=a07cc2dab61f7cb74ba3bbeec0f91d83df08f744;hb=4bb0077fd274237fb81db63460620470b1f6d520;hpb=16023be19df43f8b8acc4671e0fabdeffb0e720a diff --git a/bulletml/parser.py b/bulletml/parser.py index a07cc2d..2ad648c 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -14,9 +14,12 @@ import math from xml.etree.ElementTree import ElementTree try: - from cStringIO import StringIO + from io import StringIO except ImportError: - from StringIO import StringIO + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO from bulletml.errors import Error from bulletml.expr import NumberDef, INumberDef @@ -170,6 +173,28 @@ class Wait(object): def __repr__(self): return "%s(%r)" % (type(self).__name__, self.frames) +class Tag(object): + """Set a bullet tag.""" + + def __init__(self, tag): + self.tag = tag + + @classmethod + def FromElement(cls, doc, element): + """Construct using an ElementTree-style element.""" + return cls(element.text) + +class Untag(object): + """Unset a bullet tag.""" + + def __init__(self, tag): + self.tag = tag + + @classmethod + def FromElement(cls, doc, element): + """Construct using an ElementTree-style element.""" + return cls(element.text) + class Vanish(object): """Make the owner disappear.""" @@ -502,7 +527,7 @@ class BulletML(object): @classmethod def FromDocument(cls, source): """Return a BulletML instance based on a string or file-like.""" - if isinstance(source, (str, unicode)): + if not hasattr(source, 'read'): source = StringIO(source) tree = ElementTree() @@ -542,7 +567,7 @@ class BulletML(object): @property def top(self): """Get a list of all top-level actions.""" - return [dfn for name, dfn in self.actions.iteritems() + return [dfn for name, dfn in self.actions.items() if name and name.startswith("top")] def __repr__(self): @@ -551,13 +576,15 @@ class BulletML(object): self.fires) ActionDef.CONSTRUCTORS = dict( - repeat=Repeat, - fire=FireDef, - fireRef=FireRef, - changeSpeed=ChangeSpeed, - changeDirection=ChangeDirection, - accel=Accel, - wait=Wait, - vanish=Vanish, - action=ActionDef, - actionRef=ActionRef) + repeat=Repeat, + fire=FireDef, + fireRef=FireRef, + changeSpeed=ChangeSpeed, + changeDirection=ChangeDirection, + accel=Accel, + wait=Wait, + vanish=Vanish, + tag=Tag, + untag=Untag, + action=ActionDef, + actionRef=ActionRef)