def register_templates(self, module_path, dir_name=DEFAULT_PATH, prefix=None): import os.path from tool.importing import import_module mod = import_module(module_path) root = mod.__path__[0] path = os.path.join(root, dir_name) self.env['templating_env'].directories.append(path)
def find_urls(self, source): """ Accepts either module or dictionary. Returns a cumulative list of rules for all members of given module or dictionary. How does this work? Any callable object can provide a list of rules as its own attribute named ``url_rules``. The ``url`` decorator adds such an attribute to the wrapped object and sets the object as endpoint for rules being added. ``find_urls``, however, does not care about endpoints, it simply gathers rules scattered all over the place. Usage:: from tool.routing import Map, Submount import foo.views # define a view exposed at given URL. Note that it is *not* a good idea # to mix views with configuration and management code in real apps. @url('/hello/') def hello(request): return 'Hello!' # gather URLs from this module (yields the "hello" one) local_urls = find_urls(locals()) # gather URLs from some bundle's views module foo_urls = find_urls(foo.views) # gather URLs from a module that is not imported yet bar_urls = find_urls('bar.views') url_map = Map( local_urls + [ Submount('/foo/', foo_urls), Submount('/bar/', bar_urls), ] ) # ...make app, etc. Such approach does not impose any further conventions (such as where to place the views) and leaves it up to you whether to store URL mappings and views in separate modules or keep them together using the ``url`` decorator. It is considered good practice, however, to not mix different things in the same module. """ if isinstance(source, dict): d = source else: if isinstance(source, basestring): source = import_module(source) d = dict((n, getattr(source, n)) for n in dir(source)) def generate(d): for name, attr in d.iteritems(): if hasattr(attr, '__call__') and hasattr(attr, 'routing_rules'): for rule_draft in attr.routing_rules: yield self.compile_rule(**rule_draft) return list(generate(d))