From 6f48c973332ca65555b83dac487412cfba7ef0d4 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Thu, 3 Feb 2011 10:46:15 +0530 Subject: [PATCH] Grouper first pass complete --- hyde/ext/plugins/grouper.py | 52 ++++++++++++++++++++++++++-------- hyde/tests/ext/test_grouper.py | 16 ++++++++--- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/hyde/ext/plugins/grouper.py b/hyde/ext/plugins/grouper.py index 6db8148..598cd10 100644 --- a/hyde/ext/plugins/grouper.py +++ b/hyde/ext/plugins/grouper.py @@ -20,13 +20,22 @@ class Group(Expando): grouping resources. """ - def __init__(self, grouping): + def __init__(self, grouping, parent=None): + self.parent = parent + self.root = self + self.root = parent.root if parent else self super(Group, self).__init__(grouping) - name = 'group' + if hasattr(parent, 'sorter') and not hasattr(self, 'sorter'): + self.sorter = parent.sorter + name = 'groups' if hasattr(grouping, 'name'): name = grouping.name + add_method(Node, + 'walk_%s_groups' % self.name, + Group.walk_groups_in_node, + group=self) add_method(Node, 'walk_resources_grouped_by_%s' % name, Group.walk_resources, @@ -38,7 +47,7 @@ class Group(Expando): regular expando objects. """ if key == "groups": - self.groups = [Group(group) for group in value] + self.groups = [Group(group, parent=self) for group in value] else: return super(Group, self).set_expando(key, value) @@ -46,17 +55,38 @@ class Group(Expando): def walk_resources(node, group): """ The method that gets attached to the node - object. + object for walking the resources in the node + that belong to this group. """ return group.list_resources(node) + @staticmethod + def walk_groups_in_node(node, group): + """ + The method that gets attached to the node + object for walking the groups in the node. + """ + walker = group.walk_groups() + for g in walker: + lister = g.list_resources(node) + found = False + for r in lister: + found = True + yield g + break; + if not found: + walker.send(True) + found = False + + def walk_groups(self): """ Walks the groups in the current group """ for group in self.groups: - yield group - group.walk_groups() + skip = (yield group) + if not skip: + group.walk_groups() def list_resources(self, node): @@ -66,13 +96,12 @@ class Group(Expando): group. """ walker = 'walk_resources' - if hasattr(self, 'sort_with'): - walker = 'walk_resources_sorted_by_' + self.sort_with + if hasattr(self, 'sorter'): + walker = 'walk_resources_sorted_by_' + self.sorter walker = getattr(node, walker, getattr(node, 'walk_resources')) - for resource in walker(): try: - group_value = getattr(resource.meta, self.name) + group_value = getattr(resource.meta, self.root.name) except AttributeError: continue @@ -98,7 +127,7 @@ class GrouperPlugin(Plugin): # based on the groups specified here. # The node and resource should be tagged # with the categories in their metadata - sort_with: kind # A reference to the sorter + sorter: kind # A reference to the sorter description: Articles about hyde groups: - @@ -116,7 +145,6 @@ class GrouperPlugin(Plugin): def __init__(self, site): super(GrouperPlugin, self).__init__(site) - def begin_site(self): """ Initialize plugin. Add the specified groups to the diff --git a/hyde/tests/ext/test_grouper.py b/hyde/tests/ext/test_grouper.py index 09e80c0..44410f9 100644 --- a/hyde/tests/ext/test_grouper.py +++ b/hyde/tests/ext/test_grouper.py @@ -38,8 +38,7 @@ class TestGrouper(object): attr: - source_file.kind grouper: - sections: - name: section + section: description: Sections in the site sorter: kind groups: @@ -58,11 +57,20 @@ class TestGrouper(object): SorterPlugin(s).begin_site() GrouperPlugin(s).begin_site() - groups = dict([(g.name, g) for g in s.grouper['sections'].groups]) + + groups = dict([(g.name, g) for g in s.grouper['section'].groups]) + assert len(groups) == 2 assert 'start' in groups assert 'plugins' in groups - assert hasattr(s.content, 'walk_resources_grouped_by_sections') + assert hasattr(s.content, 'walk_section_groups') + groups = dict([(g.name, g) for g in s.content.walk_section_groups()]) + assert len(groups) == 2 + assert 'start' in groups + assert 'plugins' in groups + + assert hasattr(s.content, 'walk_resources_grouped_by_section') + # assert hasattr(s, 'sectional')