From 405e333fe99b3625b0d89565de4e83984b462a3e Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 18 Mar 2010 22:58:14 -0700 Subject: [PATCH] Expose only top-level actions, not bullets or firings. --- bulletml-runner | 4 ++-- bulletml-to-bulletyaml | 3 +++ bulletml/__init__.py | 2 +- bulletml/parser.py | 45 ++++++++++++++++++++---------------------- setup.py | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/bulletml-runner b/bulletml-runner index 8bfd338..9136f21 100755 --- a/bulletml-runner +++ b/bulletml-runner @@ -37,7 +37,7 @@ def main(argv): while True: filename = argv[file_idx % len(argv)] doc = bulletml.BulletML.FromDocument(open(filename, "rU")) - actions = [act([], 0.5) for act in doc.top] + actions = [act([], 0.5) for act in doc.actions] source = bulletml.Bullet( x=150, y=150, target=target, actions=actions, rank=0.5) @@ -72,7 +72,7 @@ def main(argv): elif event.key == pygame.K_RETURN: newfile = True elif event.key == pygame.K_s: - actions = [act([], 0.5) for act in doc.top] + actions = [act([], 0.5) for act in doc.actions] source = bulletml.Bullet( x=150, y=150, target=target, actions=actions, rank=0.5) diff --git a/bulletml-to-bulletyaml b/bulletml-to-bulletyaml index 749ccde..89f861b 100755 --- a/bulletml-to-bulletyaml +++ b/bulletml-to-bulletyaml @@ -6,6 +6,9 @@ import yaml from bulletml import BulletML, bulletyaml def main(argv): + if not argv: + raise SystemExit("Usage: %s filename ..." % sys.argv[0]) + for filename in argv: print "# Loading", filename print yaml.dump(BulletML.FromDocument(open(filename, "rU"))) diff --git a/bulletml/__init__.py b/bulletml/__init__.py index addc0fe..27faaf0 100644 --- a/bulletml/__init__.py +++ b/bulletml/__init__.py @@ -19,7 +19,7 @@ Basic Usage: doc = Bulletml.BulletML.FromDocument(open("test.xml", "rU")) rank = 0.5 # Player difficulty, 0 to 1 params = [] # Initial variable settings, usually empty - actions = [a(params, rank) for a in doc.top] + actions = [a(params, rank) for a in doc.actions] bullet = Bullet(x, y, target=player, actions=actions, rank=rank) bullets = [bullet] ... diff --git a/bulletml/parser.py b/bulletml/parser.py index 51e15ad..4870093 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -394,7 +394,7 @@ class BulletDef(object): elif tag == "actionRef": actions.append(ActionRef.FromXML(doc, subelem)) dfn = cls(actions, direction, speed) - doc.bullets[element.get("label")] = dfn + doc._bullets[element.get("label")] = dfn return dfn def __call__(self, params, rank): @@ -478,7 +478,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): @@ -584,7 +584,7 @@ class FireDef(object): 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)) @@ -621,7 +621,7 @@ class FireDef(object): 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): @@ -674,8 +674,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 +690,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)] @@ -714,6 +712,9 @@ class BulletML(object): self = cls(type=root.get("type", "none")) + self._bullets = {} + self._actions = {} + self._fires = {} self._bullet_refs = [] self._action_refs = [] self._fire_refs = [] @@ -725,22 +726,24 @@ class BulletML(object): try: for ref in self._bullet_refs: - ref.bullet = self.bullets[ref.bullet] + ref.bullet = self._bullets[ref.bullet] for ref in self._fire_refs: - ref.fire = self.fires[ref.fire] + ref.fire = self._fires[ref.fire] for ref in self._action_refs: - ref.action = self.actions[ref.action] + ref.action = self._actions[ref.action] except KeyError as exc: raise ParseError("unknown reference %s" % exc) + self.actions = [act for name, act in self._actions.items() + if name and name.startswith("top")] + del(self._bullet_refs) del(self._action_refs) del(self._fire_refs) - - self.bullets.pop(None, None) - self.actions.pop(None, None) - self.fires.pop(None, None) - + del(self._bullets) + del(self._actions) + del(self._fires) + return self @classmethod @@ -776,12 +779,6 @@ 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, diff --git a/setup.py b/setup.py index 17213c2..db0e165 100755 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ if __name__ == "__main__": license="MIT-style", packages=["bulletml"], data_files=glob.glob("examples/*/*.xml") + ["examples/template.xml"], - scripts=["bulletml-runner"], + scripts=["bulletml-runner", "bulletml-to-bulletyaml"], long_description="""\ BulletML is the Bullet Markup Language. BulletML can describe the barrage of bullets in shooting games. (For example Progear, Psyvariar, -- 2.20.1