Browse Source

Added failing test for adding metadata to tags. (Issue #53)

main
Lakshmi Vyasarajan 13 years ago
parent
commit
73302caafc
2 changed files with 70 additions and 11 deletions
  1. +34
    -9
      hyde/ext/plugins/tagger.py
  2. +36
    -2
      hyde/tests/ext/test_tagger.py

+ 34
- 9
hyde/ext/plugins/tagger.py View File

@@ -17,6 +17,26 @@ from itertools import ifilter, izip, tee, product
from operator import attrgetter


class Tag(object):
"""
A simple object that represents a tag.
"""

def __init__(self, name):
"""
Initialize the tag with a name.
"""
self.name = name
self.resources = []


def __repr__(self):
return self.name

def __str__(self):
return self.name


def get_tagger_sort_method(site):
config = site.config
content = site.content
@@ -36,7 +56,7 @@ def get_tagger_sort_method(site):
return walker

def walk_resources_tagged_with(node, tag):
tags = set(tag.split('+'))
tags = set(str(tag).split('+'))
walker = get_tagger_sort_method(node.site)
for resource in walker():
try:
@@ -87,15 +107,20 @@ class TaggerPlugin(Plugin):
taglist = attrgetter("meta.tags")(resource)
except AttributeError:
continue
for tag in taglist:
if not tag in tags:
tags[tag] = [resource]
for tagname in taglist:
if not tagname in tags:
tag = Tag(tagname)
tags[tagname] = tag
tag.resources.append(resource)
add_method(Node,
'walk_resources_tagged_with_%s' % tag,
walk_resources_tagged_with,
tag=tag)
else:
tags[tag].append(resource)
tags[tagname].resources.append(resource)
if not hasattr(resource, 'tags'):
setattr(resource, 'tags', [])
resource.tags.append(tags[tagname])

self.site.tagger = Expando(dict(tags=tags))

@@ -130,7 +155,7 @@ class TaggerPlugin(Plugin):
template = config['template']
text = "{%% extends \"%s\" %%}" % template

for tag, resources in self.site.tagger.tags.to_dict().iteritems():
for tagname, tag in self.site.tagger.tags.to_dict().iteritems():
context = {}
context.update(self.site.context)
context.update(dict(
@@ -139,9 +164,9 @@ class TaggerPlugin(Plugin):
node=source,
tag=tag,
walker=getattr(source,
"walk_resources_tagged_with_%s" % tag)
"walk_resources_tagged_with_%s" % tagname)
))
archive_text = self.template.render(text, context)
archive_file = File(target.child("%s.%s" % (tag, extension)
if extension is not None else tag))
archive_file = File(target.child("%s.%s" % (tagname, extension)
if extension is not None else tagname))
archive_file.write(archive_text)

+ 36
- 2
hyde/tests/ext/test_tagger.py View File

@@ -6,6 +6,7 @@ Use nose
"""
from hyde.fs import File, Folder
from hyde.generator import Generator
from hyde.model import Expando
from hyde.site import Site

from hyde.tests.util import assert_html_equals
@@ -42,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']]
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
@@ -84,7 +85,7 @@ class TestTagger(object):

q = PyQuery(File(tags_folder.child('sad.html')).read_all())
assert q

assert q('li').length == 2
assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html'
@@ -115,3 +116,36 @@ class TestTagger(object):

assert q('li').length == 1
assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html'

def test_tagger_metadata(self):
conf = {
"sad" : {
"emotions": ["Dissappointed", "Lost"]
},
"Angry": {
"emotions": ["Irritated", "Annoyed", "Disgusted"]
}
}
self.s.config.tagger.meta = Expando(conf)
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
tags = self.s.tagger.tags.to_dict()
sad_tag = tags["sad"]
assert sad_tag
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

for tagname in ['angry', 'thoughts', 'events']:
tag = tags[tagname]
assert tag
assert not hasattr(tag, "emotions")

Loading…
Cancel
Save