From 2de38e373a540468c69bfb1945b19f92983a0dad Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Thu, 6 Jan 2011 00:08:47 +0530 Subject: [PATCH] Added ability to update Expando's with an Expando --- hyde/ext/plugins/meta.py | 6 +++--- hyde/model.py | 7 +++++-- hyde/tests/test_model.py | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/hyde/ext/plugins/meta.py b/hyde/ext/plugins/meta.py index 1831e18..2433b1c 100644 --- a/hyde/ext/plugins/meta.py +++ b/hyde/ext/plugins/meta.py @@ -29,10 +29,10 @@ class Metadata(Expando): """ Updates the metadata with new stuff """ - if isinstance(data, dict): - super(Metadata, self).update(data) - else: + if isinstance(data, basestring): super(Metadata, self).update(yaml.load(data)) + else: + super(Metadata, self).update(data) class MetaPlugin(Plugin): diff --git a/hyde/model.py b/hyde/model.py index 9e84db4..16387cf 100644 --- a/hyde/model.py +++ b/hyde/model.py @@ -18,8 +18,11 @@ class Expando(object): Updates the expando with a new dictionary """ d = d or {} - for key, value in d.items(): - setattr(self, key, Expando.transform(value)) + if isinstance(d, dict): + for key, value in d.items(): + setattr(self, key, Expando.transform(value)) + elif isinstance(d, Expando): + self.update(d.__dict__) @staticmethod def transform(primitive): diff --git a/hyde/tests/test_model.py b/hyde/tests/test_model.py index df6c2c9..7db97f4 100644 --- a/hyde/tests/test_model.py +++ b/hyde/tests/test_model.py @@ -37,6 +37,11 @@ def test_expando_update(): assert x.b.c == d['b']['c'] assert x.b.d.e == d['b']['d']['e'] assert x.f == d["f"] + d2 = {"a": 789, "f": "opq"} + y = Expando(d2) + x.update(y) + assert x.a == 789 + assert x.f == "opq" TEST_SITE_ROOT = File(__file__).parent.child_folder('sites/test_jinja') import yaml