X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=8f253fe7a09d722ebded8d296d4c8f0a9d64def6;hp=51e15add66b82eac1e0d60671301c5b0e0b37dc2;hb=6e7423671150b704dd2accbf4b8d544744271b81;hpb=e753939f30c6c07a68363c7819acf8c0e57a6951 diff --git a/bulletml/parser.py b/bulletml/parser.py index 51e15ad..8f253fe 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -358,10 +358,11 @@ class BulletDef(object): direction = None speed = None - def __init__(self, actions=[], direction=None, speed=None): + def __init__(self, actions=[], direction=None, speed=None, tags=()): self.direction = direction self.speed = speed self.actions = list(actions) + self.tags = set(tags) def __getstate__(self): state = [] @@ -371,6 +372,8 @@ class BulletDef(object): state.append(("speed", self.speed)) if self.actions: state.append(("actions", self.actions)) + if self.tags: + state.append(("tags", list(self.tags))) return state def __setstate__(self, state): @@ -393,8 +396,10 @@ class BulletDef(object): actions.append(ActionDef.FromXML(doc, subelem)) elif tag == "actionRef": actions.append(ActionRef.FromXML(doc, subelem)) + elif tag == "tag": + self.tags.add(subelem.text) dfn = cls(actions, direction, speed) - doc.bullets[element.get("label")] = dfn + doc._bullets[element.get("label")] = dfn return dfn def __call__(self, params, rank): @@ -402,6 +407,7 @@ class BulletDef(object): return ( self.direction and self.direction(params, rank), self.speed and self.speed(params, rank), + self.tags, actions) def __repr__(self): @@ -478,7 +484,7 @@ class ActionDef(object): else: actions.append(ctr.FromXML(doc, subelem)) dfn = cls(actions) - doc.actions[element.get("label")] = dfn + doc._actions[element.get("label")] = dfn return dfn def __call__(self, params, rank): @@ -567,11 +573,13 @@ class Offset(object): class FireDef(object): """Fire definition (creates a bullet).""" - def __init__(self, bullet, direction=None, speed=None, offset=None): + def __init__( + self, bullet, direction=None, speed=None, offset=None, tags=()): self.bullet = bullet self.direction = direction self.speed = speed self.offset = offset + self.tags = set(tags) def __getstate__(self): state = [] @@ -581,10 +589,12 @@ class FireDef(object): state.append(("speed", self.speed)) if self.offset: state.append(("offset", self.offset)) + if self.tags: + state.append(("tags", list(self.tags))) try: params = self.bullet.params except AttributeError: - state = dict(bullet=self.bullet) + state.append(('bullet', self.bullet)) else: if params.params: state.append(('bullet', self.bullet)) @@ -616,21 +626,24 @@ class FireDef(object): bullet = BulletRef.FromXML(doc, subelem) elif tag == "offset": offset = Offset.FromXML(doc, subelem) + elif tag == "tag": + self.tags.add(subelem.text) try: fire = cls(bullet, direction, speed, offset) except UnboundLocalError as exc: raise ParseError(str(exc)) else: - doc.fires[element.get("label")] = fire + doc._fires[element.get("label")] = fire return fire def __call__(self, params, rank): - direction, speed, actions = self.bullet(params, rank) + direction, speed, tags, actions = self.bullet(params, rank) if self.direction: direction = self.direction(params, rank) if self.speed: speed = self.speed(params, rank) - return direction, speed, actions, self.offset + tags = tags.union(self.tags) + return direction, speed, self.offset, tags, actions def __repr__(self): return "%s(direction=%r, speed=%r, bullet=%r)" % ( @@ -674,8 +687,8 @@ class FireRef(object): class BulletML(object): """BulletML document. - A BulletML document is a collection of bullets, actions, and - firings, as well as a base game type. + A BulletML document is a collection of top-level actions and the + base game type. You can add tags to the BulletML.CONSTRUCTORS dictionary to extend its parsing. It maps tag names to classes with a FromXML @@ -690,11 +703,9 @@ class BulletML(object): fire=FireDef, ) - def __init__(self, type="none", bullets=None, fires=None, actions=None): + def __init__(self, type="none", actions=None): self.type = type - self.bullets = {} if bullets is None else bullets - self.actions = {} if actions is None else actions - self.fires = {} if fires is None else fires + self.actions = [] if actions is None else actions def __getstate__(self): return [('type', self.type), ('actions', self.actions)] @@ -712,36 +723,41 @@ class BulletML(object): tree = ElementTree() root = tree.parse(source) - self = cls(type=root.get("type", "none")) + doc = cls(type=root.get("type", "none")) - self._bullet_refs = [] - self._action_refs = [] - self._fire_refs = [] + doc._bullets = {} + doc._actions = {} + doc._fires = {} + doc._bullet_refs = [] + doc._action_refs = [] + doc._fire_refs = [] for element in root.getchildren(): tag = realtag(element) - if tag in self.CONSTRUCTORS: - self.CONSTRUCTORS[tag].FromXML(self, element) + if tag in doc.CONSTRUCTORS: + doc.CONSTRUCTORS[tag].FromXML(doc, element) try: - for ref in self._bullet_refs: - ref.bullet = self.bullets[ref.bullet] - for ref in self._fire_refs: - ref.fire = self.fires[ref.fire] - for ref in self._action_refs: - ref.action = self.actions[ref.action] + for ref in doc._bullet_refs: + ref.bullet = doc._bullets[ref.bullet] + for ref in doc._fire_refs: + ref.fire = doc._fires[ref.fire] + for ref in doc._action_refs: + ref.action = doc._actions[ref.action] except KeyError as exc: raise ParseError("unknown reference %s" % exc) - del(self._bullet_refs) - del(self._action_refs) - del(self._fire_refs) + doc.actions = [act for name, act in doc._actions.items() + if name and name.startswith("top")] - self.bullets.pop(None, None) - self.actions.pop(None, None) - self.fires.pop(None, None) - - return self + del(doc._bullet_refs) + del(doc._action_refs) + del(doc._fire_refs) + del(doc._bullets) + del(doc._actions) + del(doc._fires) + + return doc @classmethod def FromYAML(cls, source): @@ -776,16 +792,9 @@ class BulletML(object): else: raise ParseError("unknown initial character %r" % start) - @property - def top(self): - """Get a list of all top-level actions.""" - return [dfn for name, dfn in self.actions.items() - if name and name.startswith("top")] - def __repr__(self): - return "%s(type=%r, bullets=%r, actions=%r, fires=%r)" % ( - type(self).__name__, self.type, self.bullets, self.actions, - self.fires) + return "%s(type=%r, actions=%r)" % ( + type(self).__name__, self.type, self.actions) ActionDef.CONSTRUCTORS = dict( repeat=Repeat,