Browse Source

Can walk resources in a group now

main
Lakshmi Vyasarajan 14 years ago
parent
commit
bb4e5d7677
2 changed files with 28 additions and 24 deletions
  1. +13
    -20
      hyde/ext/plugins/grouper.py
  2. +15
    -4
      hyde/tests/ext/test_grouper.py

+ 13
- 20
hyde/ext/plugins/grouper.py View File

@@ -21,23 +21,22 @@ class Group(Expando):
""" """


def __init__(self, grouping, parent=None): def __init__(self, grouping, parent=None):
self.name = 'groups'
self.parent = parent self.parent = parent
self.root = self self.root = self
self.root = parent.root if parent else self self.root = parent.root if parent else self
super(Group, self).__init__(grouping)
if hasattr(parent, 'sorter') and not hasattr(self, 'sorter'):
self.groups = []
self.sorter = getattr(grouping, 'sorter', None)
if hasattr(parent, 'sorter'):
self.sorter = parent.sorter self.sorter = parent.sorter

name = 'groups'
if hasattr(grouping, 'name'):
name = grouping.name
super(Group, self).__init__(grouping)


add_method(Node, add_method(Node,
'walk_%s_groups' % self.name, 'walk_%s_groups' % self.name,
Group.walk_groups_in_node, Group.walk_groups_in_node,
group=self) group=self)
add_method(Node, add_method(Node,
'walk_resources_grouped_by_%s' % name,
'walk_resources_grouped_by_%s' % self.name,
Group.walk_resources, Group.walk_resources,
group=self) group=self)


@@ -58,7 +57,9 @@ class Group(Expando):
object for walking the resources in the node object for walking the resources in the node
that belong to this group. that belong to this group.
""" """
return group.list_resources(node)
for group in group.walk_groups():
for resource in group.list_resources(node):
yield resource


@staticmethod @staticmethod
def walk_groups_in_node(node, group): def walk_groups_in_node(node, group):
@@ -69,25 +70,19 @@ class Group(Expando):
walker = group.walk_groups() walker = group.walk_groups()
for g in walker: for g in walker:
lister = g.list_resources(node) lister = g.list_resources(node)
found = False
for r in lister: for r in lister:
found = True
yield g yield g
break; break;
if not found:
walker.send(True)
found = False found = False



def walk_groups(self): def walk_groups(self):
""" """
Walks the groups in the current group Walks the groups in the current group
""" """
yield self
for group in self.groups: for group in self.groups:
skip = (yield group)
if not skip:
group.walk_groups()

for child in group.walk_groups():
yield child


def list_resources(self, node): def list_resources(self, node):
""" """
@@ -96,7 +91,7 @@ class Group(Expando):
group. group.
""" """
walker = 'walk_resources' walker = 'walk_resources'
if hasattr(self, 'sorter'):
if hasattr(self, 'sorter') and self.sorter:
walker = 'walk_resources_sorted_by_' + self.sorter walker = 'walk_resources_sorted_by_' + self.sorter
walker = getattr(node, walker, getattr(node, 'walk_resources')) walker = getattr(node, walker, getattr(node, 'walk_resources'))
for resource in walker(): for resource in walker():
@@ -104,11 +99,9 @@ class Group(Expando):
group_value = getattr(resource.meta, self.root.name) group_value = getattr(resource.meta, self.root.name)
except AttributeError: except AttributeError:
continue continue

if group_value == self.name: if group_value == self.name:
yield resource yield resource



class GrouperPlugin(Plugin): class GrouperPlugin(Plugin):
""" """
Grouper plugin for hyde. Adds the ability to do Grouper plugin for hyde. Adds the ability to do


+ 15
- 4
hyde/tests/ext/test_grouper.py View File

@@ -29,6 +29,7 @@ class TestGrouper(object):
def test_walk_resources_sorted_with_grouping_one_level(self): def test_walk_resources_sorted_with_grouping_one_level(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
cfg = """ cfg = """
nodemeta: meta.yaml
plugins: plugins:
- hyde.ext.meta.MetaPlugin - hyde.ext.meta.MetaPlugin
- hyde.ext.sorter.SorterPlugin - hyde.ext.sorter.SorterPlugin
@@ -37,6 +38,8 @@ class TestGrouper(object):
kind: kind:
attr: attr:
- source_file.kind - source_file.kind
filters:
is_processable: True
grouper: grouper:
section: section:
description: Sections in the site description: Sections in the site
@@ -57,12 +60,18 @@ class TestGrouper(object):
SorterPlugin(s).begin_site() SorterPlugin(s).begin_site()
GrouperPlugin(s).begin_site() GrouperPlugin(s).begin_site()


print [resource.name for resource in s.content.walk_resources_sorted_by_kind()]
groups = dict([(g.name, g) for g in s.grouper['section'].groups]) groups = dict([(g.name, g) for g in s.grouper['section'].groups])
assert len(groups) == 2 assert len(groups) == 2
assert 'start' in groups assert 'start' in groups
assert 'plugins' in groups assert 'plugins' in groups

groups = dict([(g.name, g) for g in s.grouper['section'].walk_groups()])
assert len(groups) == 3
assert 'section' in groups
assert 'start' in groups
assert 'plugins' in groups
assert hasattr(s.content, 'walk_section_groups') assert hasattr(s.content, 'walk_section_groups')
groups = dict([(g.name, g) for g in s.content.walk_section_groups()]) groups = dict([(g.name, g) for g in s.content.walk_section_groups()])
assert len(groups) == 2 assert len(groups) == 2
@@ -70,8 +79,10 @@ class TestGrouper(object):
assert 'plugins' in groups assert 'plugins' in groups


assert hasattr(s.content, 'walk_resources_grouped_by_section') assert hasattr(s.content, 'walk_resources_grouped_by_section')


resources = [resource.name for resource in s.content.walk_resources_grouped_by_section()]
assert len(resources) == 5


# assert hasattr(s, 'sectional') # assert hasattr(s, 'sectional')
# assert hasattr(s.sectional, 'groups') # assert hasattr(s.sectional, 'groups')


Loading…
Cancel
Save