Permalink
Browse files

Initial tagger tests added

  • Loading branch information...
1 parent 5a2fd30 commit 1cf3a46b3353570c9d15d9cfe83287b67b32f42c @lakshmivyas lakshmivyas committed Mar 11, 2011
View
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+"""
+Contains classes and utilities related to tagging
+resources in hyde.
+"""
+import re
+from hyde.model import Expando
+from hyde.plugin import Plugin
+from hyde.site import Node, Resource
+from hyde.util import add_method, add_property, pairwalk
+
+from collections import namedtuple
+from functools import partial
+from itertools import ifilter, izip, tee, product
+from operator import attrgetter
+
+
+class TaggerPlugin(Plugin):
+ """
+ Tagger plugin for hyde. Adds the ability to do
+ tag resources and search based on the tags.
+
+ Configuration example
+ ---------------------
+ #yaml
+ sorter:
+ kind:
+ atts: source.kind
+ tagger:
+ blog:
+ sorter: kind # How to sort the resources in a tag
+ source: blog # The source folder to look for resources
+ target: blog/tags # The target folder to deploy the archives
+ """
+ def __init__(self, site):
+ super(GrouperPlugin, self).__init__(site)
+
+ def begin_site(self):
+ """
+ Initialize plugin. Add the specified groups to the
+ site context variable.
+ """
+ config = self.site.config
+ if not hasattr(config, 'grouper'):
+ return
+ if not hasattr(self.site, 'grouper'):
+ self.site.grouper = {}
+
+ for name, grouping in self.site.config.grouper.__dict__.items():
+ grouping.name = name
+ prev_att = 'prev_in_%s' % name
+ next_att = 'next_in_%s' % name
+ setattr(Resource, prev_att, None)
+ setattr(Resource, next_att, None)
+ self.site.grouper[name] = Group(grouping)
+ walker = Group.walk_resources(
+ self.site.content, self.site.grouper[name])
+
+ for prev, next in pairwalk(walker):
+ setattr(next, prev_att, prev)
+ setattr(prev, next_att, next)
@@ -67,7 +67,7 @@
I strained the old bean to meet this emergency.
"You want to work it so that he makes Miss Singer's acquaintance without
-knowing that you know her. Then you come along——"
+knowing that you know her. Then you come along"
"But how can I work it that way?"
View
@@ -48,26 +48,22 @@ def __init__(self, sitepath):
@abc.abstractmethod
def configure(self, site, engine):
+
"""
- The site object should contain a config attribute. The config object is
- a simple YAML object with required settings. The template implementations
- are responsible for transforming this object to match the `settings`
- required for the template engines.
+ The site object should contain a config attribute. The config object
+ is a simple YAML object with required settings. The template
+ implementations are responsible for transforming this object to match
+ the `settings` required for the template engines.
The engine is an informal protocol to provide access to some
hyde internals.
- The preprocessor and postprocessor attributes must contain the
- functions that trigger the hyde plugins to preprocess the template
- after load and postprocess it after it is processed and code is generated.
-
- Note that the processors must only be used when referencing templates,
- for example, using the include tag. The regular preprocessing and
- post processing logic is handled by hyde.
+ The preprocessor attribute must contain the function that trigger the
+ hyde plugins to preprocess the template after load.
- A context_for_path attribute must contain the function that returns the
- context object that is populated with the appropriate variables for the given
- path.
+ A context_for_path attribute must contain the function that returns
+ the context object that is populated with the appropriate variables
+ for the given path.
"""
return
@@ -79,6 +75,14 @@ def get_dependencies(self, text):
return None
@abc.abstractmethod
+ def render_resource(self, resource, context):
+ """
+ This function must load the file represented by the resource
+ object and return the rendered text.
+ """
+ return ''
+
+ @abc.abstractmethod
def render(self, text, context):
"""
Given the text, and the context, this function must return the
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+"""
+Use nose
+`$ pip install nose`
+`$ nosetests`
+"""
+from hyde.fs import File, Folder
+from hyde.generator import Generator
+from hyde.site import Site
+
+from hyde.tests.util import assert_html_equals
+import yaml
+
+TEST_SITE = File(__file__).parent.parent.child_folder('_test')
+
+class TestTagger(object):
+
+ def setUp(self):
+ TEST_SITE.make()
+ TEST_SITE.parent.child_folder(
+ 'sites/test_tagger').copy_contents_to(TEST_SITE)
+ self.s = Site(TEST_SITE)
+ self.deploy = TEST_SITE.child_folder('deploy')
+
+
+ def tearDown(self):
+ TEST_SITE.delete()
+
+
+ def test_tagger_walker(self):
+ gen = Generator(self.s)
+ gen.load_site_if_needed()
+
+ tags = self.s.taggger['blog'].tags
+
+ assert tags.length == 5
+
+ for tag in ['sad', 'happy', 'angry', 'thoughts', 'events']:
+ assert tag in tags
+
+ # sad_posts = [post.name for post in
+ # self.s.content.walk_resources_tagged_with('sad')]
+ # assert sad_posts.length == 2
+ # assert "sad-post.html" in sad_posts
+ # assert "another-sad-post.html" in sad_posts
+ #
+ # happy_posts = [post.name for post in
+ # self.s.content.walk_resources_tagged_with('happy')]
+ # assert happy_posts.length == 1
+ # assert "happy-post.html" in happy_posts
+ #
+ # angry_posts = [post.name for post in
+ # self.s.content.walk_resources_tagged_with('angry')]
+ # assert angry_posts.length == 1
+ # assert "angry-post.html" in angry_posts
+
+ # sad_thought_posts = [post.name for post in
+ # self.s.content.walk_resources_tagged_with('sad')]
+ # assert sad_posts.length == 2
+ # assert "sad-post.html" in sad_posts
+ # assert "another-sad-post.html" in sad_posts
+
+
+
+
+ # def test_tagger_archives_generated():
+ # gen = Generator(self.s)
+ # gen.load_site_if_needed()
+ # gen.load_template_if_needed()
+ # gen.generate_all()
+ # tags_folder = self.deploy.child_folder('blog/tags')
+ #
+ # blog_node = self.s.node_from_relative_path('blog')
+ # res_by_tag =
+ # for resource in blog_node.walk_resources():
+ #
+ #
+ # assert tags_folder.exists
+ # tags = ['sad', 'happy', 'angry', 'thoughts']
+ #
+ # archives = (File(tags_folder.child("%s.html" % tag)) for tag in tags)
+ # for archive in archives:
+ # assert archive.exists
+ # assert
@@ -0,0 +1,133 @@
+---
+title: An Angry Post
+description: >
+ Temper. Temper. Temper.
+created: !!timestamp '2011-01-01 10:00:00'
+tags:
+ - angry
+ - thoughts
+---
+
+--- mark excerpt
+
+To complete the character-study of Mr. Worple, he was a man of extremely
+uncertain temper, and his general tendency was to think that Corky was a poor
+chump and that whatever step he took in any direction on his own account, was
+just another proof of his innate idiocy. I should imagine Jeeves feels very
+much the same about me.
+
+--- endmark
+
+So when Corky trickled into my apartment one afternoon, shooing a girl in
+front of him, and said, "Bertie, I want you to meet my fiancée, Miss Singer,"
+the aspect of the matter which hit me first was precisely the one which he had
+come to consult me about. The very first words I spoke were, "Corky, how about
+your uncle?"
+
+The poor chap gave one of those mirthless laughs. He was looking anxious and
+worried, like a man who has done the murder all right but can't think what the
+deuce to do with the body.
+
+"We're so scared, Mr. Wooster," said the girl. "We were hoping that you might
+suggest a way of breaking it to him."
+
+Muriel Singer was one of those very quiet, appealing girls who have a way of
+looking at you with their big eyes as if they thought you were the greatest
+thing on earth and wondered that you hadn't got on to it yet yourself. She sat
+there in a sort of shrinking way, looking at me as if she were saying to
+herself, "Oh, I do hope this great strong man isn't going to hurt me." She
+gave a fellow a protective kind of feeling, made him want to stroke her hand
+and say, "There, there, little one!" or words to that effect. She made me feel
+that there was nothing I wouldn't do for her. She was rather like one of those
+innocent-tasting American drinks which creep imperceptibly into your system so
+that, before you know what you're doing, you're starting out to reform the
+world by force if necessary and pausing on your way to tell the large man in
+the corner that, if he looks at you like that, you will knock his head off.
+What I mean is, she made me feel alert and dashing, like a jolly old
+knight-errant or something of that kind. I felt that I was with her in this
+thing to the limit.
+
+"I don't see why your uncle shouldn't be most awfully bucked," I said to
+Corky. "He will think Miss Singer the ideal wife for you."
+
+Corky declined to cheer up.
+
+"You don't know him. Even if he did like Muriel he wouldn't admit it. That's
+the sort of pig-headed guy he is. It would be a matter of principle with him
+to kick. All he would consider would be that I had gone and taken an important
+step without asking his advice, and he would raise Cain automatically. He's
+always done it."
+
+I strained the old bean to meet this emergency.
+
+"You want to work it so that he makes Miss Singer's acquaintance without
+knowing that you know her. Then you come along"
+
+"But how can I work it that way?"
+
+I saw his point. That was the catch.
+
+"There's only one thing to do," I said.
+
+"What's that?"
+
+"Leave it to Jeeves."
+
+And I rang the bell.
+
+"Sir?" said Jeeves, kind of manifesting himself. One of the rummy things about
+Jeeves is that, unless you watch like a hawk, you very seldom see him come
+into a room. He's like one of those weird chappies in India who dissolve
+themselves into thin air and nip through space in a sort of disembodied way
+and assemble the parts again just where they want them. I've got a cousin
+who's what they call a Theosophist, and he says he's often nearly worked the
+thing himself, but couldn't quite bring it off, probably owing to having fed
+in his boyhood on the flesh of animals slain in anger and pie.
+
+The moment I saw the man standing there, registering respectful attention, a
+weight seemed to roll off my mind. I felt like a lost child who spots his
+father in the offing. There was something about him that gave me confidence.
+
+Jeeves is a tallish man, with one of those dark, shrewd faces. His eye gleams
+with the light of pure intelligence.
+
+"Jeeves, we want your advice."
+
+"Very good, sir."
+
+I boiled down Corky's painful case into a few well-chosen words.
+
+"So you see what it amount to, Jeeves. We want you to suggest some way by
+which Mr. Worple can make Miss Singer's acquaintance without getting on to the
+fact that Mr. Corcoran already knows her. Understand?"
+
+"Perfectly, sir."
+
+"Well, try to think of something."
+
+"I have thought of something already, sir."
+
+"You have!"
+
+"The scheme I would suggest cannot fail of success, but it has what may seem
+to you a drawback, sir, in that it requires a certain financial outlay."
+
+"He means," I translated to Corky, "that he has got a pippin of an idea, but
+it's going to cost a bit."
+
+Naturally the poor chap's face dropped, for this seemed to dish the whole
+thing. But I was still under the influence of the girl's melting gaze, and I
+saw that this was where I started in as a knight-errant.
+
+"You can count on me for all that sort of thing, Corky," I said. "Only too
+glad. Carry on, Jeeves."
+
+"I would suggest, sir, that Mr. Corcoran take advantage of Mr. Worple's
+attachment to ornithology."
+
+"How on earth did you know that he was fond of birds?"
+
+
+[My Man Jeeves by PG Wodehouse][MMJ]
+
+[MMJ]: http://www.gutenberg.org/cache/epub/8164/pg8164.html
Oops, something went wrong.

0 comments on commit 1cf3a46

Please sign in to comment.