Browse Source

Added ability to check if a resource belongs to a group hierarchy

main
Lakshmi Vyasarajan 14 years ago
parent
commit
6cca79c348
3 changed files with 61 additions and 11 deletions
  1. +32
    -0
      hyde/ext/plugins/grouper.py
  2. +12
    -0
      hyde/tests/ext/test_grouper.py
  3. +17
    -11
      hyde/tests/ext/test_less.py

+ 32
- 0
hyde/ext/plugins/grouper.py View File

@@ -42,6 +42,10 @@ class Group(Expando):
'walk_resources_grouped_by_%s' % self.name,
Group.walk_resources,
group=self)
add_method(Resource,
'walk_%s_groups' % self.name,
Group.walk_resource_groups,
group=self)

def set_expando(self, key, value):
"""
@@ -53,6 +57,23 @@ class Group(Expando):
else:
return super(Group, self).set_expando(key, value)

@staticmethod
def walk_resource_groups(resource, group):
"""
This method gets attached to the resource object.
Returns group and its ancestors that the resource
belongs to, in that order.
"""
try:
group_name = getattr(resource.meta, group.root.name)
except AttributeError:
group_name = None
if group_name:
for g in group.walk_groups():
if g.name == group_name:
return reversed(list(g.walk_hierarchy()))
return []

@staticmethod
def walk_resources(node, group):
"""
@@ -75,6 +96,17 @@ class Group(Expando):
lister = g.walk_resources_in_node(node)
yield Grouper(group=g, resources=lister)

def walk_hierarchy(self):
"""
Walks the group hierarchy starting from
this group.
"""
g = self
yield g
while g.parent:
yield g.parent
g = g.parent

def walk_groups(self):
"""
Walks the groups in the current group


+ 12
- 0
hyde/tests/ext/test_grouper.py View File

@@ -61,6 +61,7 @@ class TestGrouperSingleLevel(object):
self.all = ['installation.html', 'overview.html', 'templating.html', 'plugins.html', 'tags.html']
self.start = ['installation.html', 'overview.html', 'templating.html']
self.plugins = ['plugins.html', 'tags.html']
self.section = self.all

def tearDown(self):
TEST_SITE.delete()
@@ -129,6 +130,17 @@ class TestGrouperSingleLevel(object):
plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()]
assert plugin_resources == self.plugins

def test_resource_belongs_to(self):

groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])

for name, group in groups.items():
pages = getattr(self, name)
for page in pages:
res = self.s.content.resource_from_relative_path('blog/' + page)
res_groups = getattr(res, 'walk_%s_groups' % name)()
assert group in res_groups

def test_prev_next(self):

resources = []


+ 17
- 11
hyde/tests/ext/test_less.py View File

@@ -29,14 +29,20 @@ class TestLess(object):
def test_can_execute_less(self):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.less.LessCSSPlugin']
s.config.less = Expando(dict(app='/usr/local/share/npm/bin/lessc'))
source = TEST_SITE.child('content/media/css/site.less')
target = File(Folder(s.config.deploy_root_path).child('media/css/site.css'))
gen = Generator(s)
gen.generate_resource_at_path(source)

assert target.exists
text = target.read_all()
expected_text = File(LESS_SOURCE.child('expected-site.css')).read_all()

assert text == expected_text
less_paths = ['/usr/local/share/npm/bin/lessc', '~/local/bin/lessc']
for path in less_paths:
if File(path).exists:
s.config.less = Expando(dict(app=path))
source = TEST_SITE.child('content/media/css/site.less')
target = File(Folder(s.config.deploy_root_path).child('media/css/site.css'))
gen = Generator(s)
gen.generate_resource_at_path(source)

assert target.exists
text = target.read_all()
expected_text = File(LESS_SOURCE.child('expected-site.css')).read_all()

assert text == expected_text
return

assert "Cannot find the less css executable"

Loading…
Cancel
Save