PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/ext/plugins/depends.py

http://github.com/hyde/hyde
Python | 61 lines | 30 code | 7 blank | 24 comment | 2 complexity | af4222aefa61e27b4c1aea666a33551c MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 3Depends plugin
 4
 5/// Experimental: Not working yet.
 6"""
 7
 8from hyde._compat import basestring
 9from hyde.plugin import Plugin
10
11
12class DependsPlugin(Plugin):
13
14    """
15    The plugin class setting explicit dependencies.
16    """
17
18    def __init__(self, site):
19        super(DependsPlugin, self).__init__(site)
20
21    def begin_site(self):
22        """
23        Initialize dependencies.
24
25        Go through all the nodes and resources to initialize
26        dependencies at each level.
27        """
28        for resource in self.site.content.walk_resources():
29            self._update_resource(resource)
30
31    def _update_resource(self, resource):
32        """
33        If the meta data for the resource contains a depends attribute,
34        this plugin adds an entry to the depends property of the
35        resource.
36
37        The dependency can contain the following template variables:
38        node, resource, site, context.
39
40        The following strings are valid:
41        '{node.module}/dependencies/{resource.source.name_without_extension}.inc'
42        '{context.dependency_folder}/{resource.source.name_without_extension}.{site.meta.depext}'
43        """
44        depends = []
45        try:
46            depends = resource.meta.depends
47        except AttributeError:
48            return
49
50        if not hasattr(resource, 'depends') or not resource.depends:
51            resource.depends = []
52
53        if isinstance(depends, basestring):
54            depends = [depends]
55
56        for dep in depends:
57            resource.depends.append(dep.format(node=resource.node,
58                                               resource=resource,
59                                               site=self.site,
60                                               context=self.site.context))
61        resource.depends = list(set(resource.depends))