From 4f0e7df685d8d25cd0f1bbce7d6330f87c8cf178 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 18 Mar 2010 21:48:47 -0700 Subject: [PATCH] parser: Support __getstate__/__setstate__ protocol. --- bulletml/parser.py | 164 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 1 deletion(-) diff --git a/bulletml/parser.py b/bulletml/parser.py index 2ad648c..a89d1c5 100644 --- a/bulletml/parser.py +++ b/bulletml/parser.py @@ -49,6 +49,15 @@ class ParamList(object): return cls([NumberDef(subelem.text) for subelem in element if realtag(subelem) == "param"]) + def __getstate__(self): + if self.params: + return dict(params=[param.expr for param in self.params]) + else: + return {} + + def __setstate__(self, state): + self.__init__(NumberDef(param) for param in state.get("params", [])) + def __call__(self, params, rank): return [param(params, rank) for param in self.params] @@ -66,6 +75,12 @@ class Direction(object): self.type = type self.value = value + def __getstate__(self): + return dict(type=self.type, value=self.value.expr) + + def __setstate__(self, state): + self.__init__(state["type"], NumberDef(state["value"])) + @classmethod def FromElement(cls, doc, element, default="absolute"): """Construct using an ElementTree-style element.""" @@ -85,6 +100,15 @@ class ChangeDirection(object): self.term = term self.direction = direction + def __getstate__(self): + return dict(frames=self.term.expr, + type=self.direction.type, + value=self.direction.value.expr) + + def __setstate__(self, state): + self.__init__(INumberDef(state["frames"]), + Direction(state["type"], NumberDef(state["value"]))) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -117,6 +141,12 @@ class Speed(object): self.type = type self.value = value + def __getstate__(self): + return dict(type=self.type, value=self.value.expr) + + def __setstate__(self, state): + self.__init__(state["type"], NumberDef(state["value"])) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -135,6 +165,15 @@ class ChangeSpeed(object): self.term = term self.speed = speed + def __getstate__(self): + return dict(frames=self.term.expr, + type=self.speed.type, + value=self.speed.value.expr) + + def __setstate__(self, state): + self.__init__(INumberDef(state["frames"]), + Speed(state["type"], NumberDef(state["value"]))) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -162,6 +201,12 @@ class Wait(object): def __init__(self, frames): self.frames = frames + def __getstate__(self): + return dict(frames=self.frames.expr) + + def __setstate__(self, state): + self.__init__(INumberDef(state["frames"])) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -179,6 +224,12 @@ class Tag(object): def __init__(self, tag): self.tag = tag + def __getstate__(self): + return dict(tag=self.tag) + + def __setstate__(self, state): + self.__init__(state["tag"]) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -189,6 +240,12 @@ class Untag(object): def __init__(self, tag): self.tag = tag + + def __getstate__(self): + return dict(tag=self.tag) + + def __setstate__(self, state): + self.__init__(state["tag"]) @classmethod def FromElement(cls, doc, element): @@ -215,6 +272,12 @@ class Repeat(object): def __init__(self, times, action): self.times = times self.action = action + + def __getstate__(self): + return dict(times=self.times.expr, action=self.action) + + def __setstate__(self, state): + self.__init__(INumberDef(state["times"]), state["action"]) @classmethod def FromElement(cls, doc, element): @@ -249,6 +312,18 @@ class Accel(object): self.horizontal = horizontal self.vertical = vertical + def __getstate__(self): + state = dict(frames=self.term.expr) + if self.horizontal: + state["horizontal"] = self.horizontal + if self.vertical: + state["vertical"] = self.vertical + return state + + def __setstate__(self, state): + self.__init__(INumberDef(state["term"]), state.get("horizontal"), + state.get("vertical")) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -290,6 +365,19 @@ class BulletDef(object): self.speed = speed self.actions = list(actions) + def __getstate__(self): + state = dict() + if self.direction: + state["direction"] = self.direction + if self.speed: + state["speed"] = self.speed + if self.actions: + state["actions"] = self.actions + return state + + def __setstate__(self, state): + self.__init__(**state) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -326,7 +414,18 @@ class BulletRef(object): def __init__(self, bullet, params=None): self.bullet = bullet - self.params = params or ParamList() + self.params = ParamList() if params is None else params + + def __getstate__(self): + state = dict(bullet=self.bullet) + if self.params.params: + state["params"] = self.params.__getstate__() + return state + + def __setstate__(self, state): + bullet = state["bullet"] + params = [NumberDef(param) for param in state.get("params", [])] + self.__init__(bullet, params) @classmethod def FromElement(cls, doc, element): @@ -357,6 +456,12 @@ class ActionDef(object): def __init__(self, actions): self.actions = list(actions) + def __getstate__(self): + return dict(actions=self.actions) + + def __setstate__(self, state): + self.__init__(state) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -386,6 +491,17 @@ class ActionRef(object): self.action = action self.params = params or ParamList() + def __getstate__(self): + state = dict(action=self.action) + if self.params.params: + state["params"] = self.params.__getstate__() + return state + + def __setstate__(self, state): + action = state["action"] + params = [NumberDef(param) for param in state.get("params", [])] + self.__init__(action, params) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -412,6 +528,17 @@ class Offset(object): self.x = x self.y = y + def __getstate__(self): + state = dict(type=self.type) + if self.x: + state["x"] = self.x.expr + if self.y: + state["y"] = self.y.expr + return state + + def __setstate__(self, state): + self.__init__(state["type"], state.get("x"), state.get("y")) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -439,6 +566,24 @@ class FireDef(object): self.speed = speed self.offset = offset + def __getstate__(self): + try: + params = self.bullet.params + except AttributeError: + state = dict(bullet=self.bullet) + else: + if params.params: + state = dict(bullet=self.bullet) + else: + state = dict(bullet=self.bullet.bullet) + if self.direction: + state["direction"] = self.direction + if self.speed: + state["speed"] = self.speed + if self.offset: + state["offset"] = self.offset + return state + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -485,6 +630,17 @@ class FireRef(object): self.fire = fire self.params = params or ParamList() + def __getstate__(self): + state = dict(fire=self.fire) + if self.params.params: + state["params"] = self.params.__getstate__() + return state + + def __setstate__(self, state): + fire = state["fire"] + params = [NumberDef(param) for param in state.get("params", [])] + self.__init__(fire, params) + @classmethod def FromElement(cls, doc, element): """Construct using an ElementTree-style element.""" @@ -524,6 +680,12 @@ class BulletML(object): self.actions = {} if actions is None else actions self.fires = {} if fires is None else fires + def __getstate__(self): + return dict(type=self.type, actions=self.actions) + + def __setstate__(self, state): + self.__init__(state["type"], state.get("actions")) + @classmethod def FromDocument(cls, source): """Return a BulletML instance based on a string or file-like.""" -- 2.30.2