X-Git-Url: https://git.yukkurigames.com/?p=python-bulletml.git;a=blobdiff_plain;f=bulletml%2Fparser.py;h=edb9d29e4c544bae3506e1681cbe25141a78c1db;hp=bf850a7aae753d5f7b52592b113965043ed0a402;hb=ba81e7d74da58dc8dfa47949502d2a2759c84309;hpb=ef50d69288ee60ac2a8fae2ffe5860e80299fd72 diff --git a/bulletml/parser.py b/bulletml/parser.py index bf850a7..edb9d29 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -5,6 +5,8 @@ http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html from __future__ import division +import math + from xml.etree.ElementTree import ElementTree try: @@ -61,7 +63,7 @@ class Direction(object): return cls(element.get("type", default), NumberDef(element.text)) def __call__(self, params, rank): - return (self.value(params, rank), self.type) + return (math.radians(self.value(params, rank)), self.type) def __repr__(self): return "%s(%r, type=%r)" % ( @@ -185,6 +187,7 @@ class Repeat(object): @classmethod def FromElement(cls, doc, element): + """Construct using an ElementTree-style element.""" for subelem in element.getchildren(): tag = realtag(subelem) if tag == "times": @@ -440,22 +443,25 @@ class BulletML(object): fire=FireDef, ) - def __init__(self, source): + def __init__(self, type="none", bullets={}, fires={}, actions={}): + self.type = type self.bullets = {} self.actions = {} self.fires = {} - self._bullet_refs = [] - self._action_refs = [] - self._fire_refs = [] - + @classmethod + def FromDocument(cls, source): if isinstance(source, (str, unicode)): source = StringIO(source) tree = ElementTree() root = tree.parse(source) - self.type = root.get("type", "none") + self = cls(type=root.get("type", "none")) + + self._bullet_refs = [] + self._action_refs = [] + self._fire_refs = [] for element in root.getchildren(): tag = realtag(element) @@ -480,6 +486,8 @@ class BulletML(object): self.actions.pop(None, None) self.fires.pop(None, None) + return self + @property def top(self): """Get a list of all top-level actions."""