From 85dcef7e093386a11b90369dfc6a2a2d10280c6c Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Mon, 20 Jun 2011 12:08:39 +0530 Subject: [PATCH] Added metadata support for tags (Issue: #53) --- hyde/ext/plugins/tagger.py | 18 ++++++++++--- hyde/model.py | 17 ++++++++---- hyde/tests/ext/test_tagger.py | 50 ++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/hyde/ext/plugins/tagger.py b/hyde/ext/plugins/tagger.py index 2f6d4b8..790344f 100644 --- a/hyde/ext/plugins/tagger.py +++ b/hyde/ext/plugins/tagger.py @@ -17,7 +17,7 @@ from itertools import ifilter, izip, tee, product from operator import attrgetter -class Tag(object): +class Tag(Expando): """ A simple object that represents a tag. """ @@ -113,7 +113,7 @@ class TaggerPlugin(Plugin): tags[tagname] = tag tag.resources.append(resource) add_method(Node, - 'walk_resources_tagged_with_%s' % tag, + 'walk_resources_tagged_with_%s' % tagname, walk_resources_tagged_with, tag=tag) else: @@ -121,7 +121,19 @@ class TaggerPlugin(Plugin): if not hasattr(resource, 'tags'): setattr(resource, 'tags', []) resource.tags.append(tags[tagname]) - + try: + tag_meta = self.site.config.tagger.tags.to_dict() + except AttributeError: + tag_meta = {} + + for tagname, meta in tag_meta.iteritems(): + # Don't allow name and resources in meta + if 'resources' in meta: + del(meta['resources']) + if 'name' in meta: + del(meta['name']) + if tagname in tags: + tags[tagname].update(meta) self.site.tagger = Expando(dict(tags=tags)) def site_complete(self): diff --git a/hyde/model.py b/hyde/model.py index 7a1e671..3f03374 100644 --- a/hyde/model.py +++ b/hyde/model.py @@ -38,7 +38,7 @@ class Expando(object): for key, value in d.items(): self.set_expando(key, value) elif isinstance(d, Expando): - self.update(d.__dict__) + self.update(d.to_dict()) def set_expando(self, key, value): """ @@ -47,6 +47,7 @@ class Expando(object): """ setattr(self, str(key).encode('utf-8'), self.transform(value)) + def transform(self, primitive): """ Creates an expando object, a sequence of expando objects or just @@ -60,19 +61,25 @@ class Expando(object): else: return primitive + def __repr__(self): + return str(self.to_dict()) + def to_dict(self): """ Reverse transform an expando to dict """ + result = {} d = self.__dict__ - for k, v in d.iteritems(): + for k, v in d.items(): if isinstance(v, Expando): - d[k] = v.to_dict() + result[k] = v.to_dict() elif isinstance(v, (tuple, list, set, frozenset)): seq = type(v) - d[k] = seq(item.to_dict() if isinstance(item, Expando) + result[k] = seq(item.to_dict() if isinstance(item, Expando) else item for item in v) - return d + else: + result[k] = v + return result class Context(object): diff --git a/hyde/tests/ext/test_tagger.py b/hyde/tests/ext/test_tagger.py index ee58a14..75e199c 100644 --- a/hyde/tests/ext/test_tagger.py +++ b/hyde/tests/ext/test_tagger.py @@ -32,7 +32,7 @@ class TestTagger(object): gen = Generator(self.s) gen.load_site_if_needed() gen.generate_all() - + assert hasattr(self.s, 'tagger') assert hasattr(self.s.tagger, 'tags') assert self.s.tagger.tags @@ -43,7 +43,7 @@ class TestTagger(object): for tag in ['sad', 'happy', 'angry', 'thoughts', 'events']: assert tag in tags - sad_posts = [post.name for post in tags['sad'].resources] + sad_posts = [post.name for post in tags['sad']['resources']] assert len(sad_posts) == 2 assert "sad-post.html" in sad_posts assert "another-sad-post.html" in sad_posts @@ -119,33 +119,39 @@ class TestTagger(object): def test_tagger_metadata(self): conf = { - "sad" : { - "emotions": ["Dissappointed", "Lost"] - }, - "Angry": { - "emotions": ["Irritated", "Annoyed", "Disgusted"] + "tagger":{ + "tags": { + "sad" : { + "emotions": ["Dissappointed", "Lost"] + }, + "angry": { + "emotions": ["Irritated", "Annoyed", "Disgusted"] + } + } } } - self.s.config.tagger.meta = Expando(conf) - gen = Generator(self.s) + s = Site(TEST_SITE) + s.config.update(conf) + gen = Generator(s) gen.load_site_if_needed() gen.generate_all() - assert hasattr(self.s, 'tagger') - assert hasattr(self.s.tagger, 'tags') - assert self.s.tagger.tags - tags = self.s.tagger.tags.to_dict() - sad_tag = tags["sad"] - assert sad_tag + assert hasattr(s, 'tagger') + assert hasattr(s.tagger, 'tags') + assert s.tagger.tags + tags = s.tagger.tags + sad_tag = tags.sad assert hasattr(sad_tag, "emotions") - assert sad_tag.emotions == self.s.config.tagger.meta.sad.emotions - happy_tag = tags["happy"] - assert happy_tag - assert hasattr(happy_tag, "emotions") - assert happy_tag.emotions == self.s.config.tagger.meta.happy.emotions + assert sad_tag.emotions == s.config.tagger.tags.sad.emotions + + assert hasattr(tags, "angry") + angry_tag = tags.angry + assert angry_tag + assert hasattr(angry_tag, "emotions") + assert angry_tag.emotions == s.config.tagger.tags.angry.emotions - for tagname in ['angry', 'thoughts', 'events']: - tag = tags[tagname] + for tagname in ['happy', 'thoughts', 'events']: + tag = getattr(tags, tagname) assert tag assert not hasattr(tag, "emotions")