X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=c4b3e04d1b880b638616168089cd65f922863672;hp=b2e078b739a5153596f91fbb0af1c59ffd4aad3b;hb=76079fba9b913a620bb4fee71504d427e0f9df10;hpb=bc6dd530488535b2edeff9afbbdde896aed73046 diff --git a/bulletml/parser.py b/bulletml/parser.py index b2e078b..c4b3e04 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -13,6 +13,12 @@ import math from xml.etree.ElementTree import ElementTree +# Python 3 moved this for no really good reason. +try: + from sys import intern +except ImportError: + pass + try: from io import StringIO except ImportError: @@ -64,7 +70,7 @@ class Direction(object): def __init__(self, type, value): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.value = value def __getstate__(self): @@ -132,7 +138,7 @@ class Speed(object): def __init__(self, type, value): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.value = value def __getstate__(self): @@ -248,6 +254,23 @@ class Untag(object): """Construct using an ElementTree-style element.""" return cls(element.text) +class Appearance(object): + """Set a bullet appearance.""" + + def __init__(self, appearance): + self.appearance = appearance + + def __getstate__(self): + return dict(appearance=self.appearance) + + def __setstate__(self, state): + self.__init__(state["appearance"]) + + @classmethod + def FromXML(cls, doc, element): + """Construct using an ElementTree-style element.""" + return cls(element.text) + class Vanish(object): """Make the owner disappear.""" @@ -355,14 +378,13 @@ class Accel(object): class BulletDef(object): """Bullet definition.""" - direction = None - speed = None - - def __init__(self, actions=(), direction=None, speed=None, tags=()): + def __init__(self, actions=(), direction=None, speed=None, tags=(), + appearance=None): self.direction = direction self.speed = speed self.actions = list(actions) self.tags = set(tags) + self.appearance = appearance def __getstate__(self): state = [] @@ -374,6 +396,8 @@ class BulletDef(object): state.append(("actions", self.actions)) if self.tags: state.append(("tags", list(self.tags))) + if self.appearance: + state.append(("appearance", self.appearance)) return state def __setstate__(self, state): @@ -409,6 +433,7 @@ class BulletDef(object): self.direction and self.direction(params, rank), self.speed and self.speed(params, rank), self.tags, + self.appearance, actions) def __repr__(self): @@ -537,7 +562,7 @@ class Offset(object): def __init__(self, type, x, y): if type not in self.VALID_TYPES: raise ValueError("invalid type %r" % type) - self.type = type + self.type = intern(type) self.x = x self.y = y @@ -551,7 +576,9 @@ class Offset(object): def __setstate__(self, state): state = dict(state) - self.__init__(state["type"], state.get("x"), state.get("y")) + x = NumberDef(state["x"]) if "x" in state else None + y = NumberDef(state["y"]) if "y" in state else None + self.__init__(state["type"], x, y) @classmethod def FromXML(cls, doc, element): @@ -574,13 +601,14 @@ class Offset(object): class FireDef(object): """Fire definition (creates a bullet).""" - def __init__( - self, bullet, direction=None, speed=None, offset=None, tags=()): + def __init__(self, bullet, direction=None, speed=None, offset=None, + tags=(), appearance=None): self.bullet = bullet self.direction = direction self.speed = speed self.offset = offset self.tags = set(tags) + self.appearance = appearance def __getstate__(self): state = [] @@ -592,6 +620,8 @@ class FireDef(object): state.append(("offset", self.offset)) if self.tags: state.append(("tags", list(self.tags))) + if self.appearance: + state.append(("appearance", self.appearance)) try: params = self.bullet.params except AttributeError: @@ -615,6 +645,7 @@ class FireDef(object): speed = None offset = None tags = set() + appearance = None for subelem in element.getchildren(): tag = realtag(subelem) @@ -630,8 +661,10 @@ class FireDef(object): offset = Offset.FromXML(doc, subelem) elif tag == "tag": tags.add(subelem.text) + elif tag == "appearance": + appearance = subelem.text try: - fire = cls(bullet, direction, speed, offset, tags) + fire = cls(bullet, direction, speed, offset, tags, appearance) except UnboundLocalError as exc: raise ParseError(str(exc)) else: @@ -639,13 +672,15 @@ class FireDef(object): return fire def __call__(self, params, rank): - direction, speed, tags, actions = self.bullet(params, rank) + direction, speed, tags, appearance, actions = self.bullet(params, rank) if self.direction: direction = self.direction(params, rank) if self.speed: speed = self.speed(params, rank) tags = tags.union(self.tags) - return direction, speed, self.offset, tags, actions + if self.appearance: + appearance = self.appearance + return direction, speed, self.offset, tags, appearance, actions def __repr__(self): return "%s(direction=%r, speed=%r, bullet=%r)" % ( @@ -706,7 +741,7 @@ class BulletML(object): ) def __init__(self, type="none", actions=None): - self.type = type + self.type = intern(type) self.actions = [] if actions is None else actions def __getstate__(self): @@ -774,7 +809,7 @@ class BulletML(object): else: try: return yaml.load(source) - except Exception, exc: + except Exception as exc: raise ParseError(str(exc)) @classmethod @@ -808,6 +843,7 @@ ActionDef.CONSTRUCTORS = dict( wait=Wait, vanish=Vanish, tag=Tag, + appearance=Appearance, untag=Untag, action=ActionDef, actionRef=ActionRef)