Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all Element with Template pickable natively #144

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions branca/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __init__(self, template=None, template_name=None):
self._env = ENV
self._children = OrderedDict()
self._parent = None
self._template_str = template
self._template_name = template_name

if template is not None:
self._template = Template(template)
Expand All @@ -71,11 +73,18 @@ def __getstate__(self):
"""
state: dict = self.__dict__.copy()
state["_env"] = None
state.pop("_template", None)
return state

def __setstate__(self, state: dict):
"""Re-add ._env attribute when unpickling"""
state["_env"] = ENV

if state["_template_str"] is not None:
state["_template"] = Template(state["_template_str"])
elif state["_template_name"] is not None:
state["_template"] = ENV.get_template(state["_template_name"])

self.__dict__.update(state)

def get_name(self):
Expand Down