
from .template import Template



import os
import uuid
import imp

from .template import Template


def discover_templates(paths=None, recursive=True):
    '''Search *paths* for mount points and load templates from them.

    *paths* should be a list of filesystem paths to search for mount points.
    If not specified will try to use value from environment variable
    :envvar:`LUCIDITY_TEMPLATE_PATH`.

    A mount point is a Python file that defines a 'register' function. The
    function should return a list of instantiated
    :py:class:`~lucidity.template.Template` objects.

    If *recursive* is True (the default) then all directories under a path
    will also be searched.

    '''
    templates = []

    if paths is None:
        paths = os.environ.get('LUCIDITY_TEMPLATE_PATH', '').split(os.pathsep)

    for path in paths:
        for base, directories, filenames in os.walk(path):
            for filename in filenames:
                _, extension = os.path.splitext(filename)
                if extension != '.py':
                    continue

                module_path = os.path.join(base, filename)
                module_name = uuid.uuid4().hex
                module = imp.load_source(module_name, module_path)
                try:
                    registered = module.register()
                except AttributeError:
                    pass
                else:
                    if registered:
                        templates.extend(registered)

            if not recursive:
                del directories[:]

    return templates


Add helper method to load templates from disk.
Add helper method to load templates from disk.

+ 
+ import os
+ import uuid
+ import imp
  
  from .template import Template
  
  
+ def discover_templates(paths=None, recursive=True):
+     '''Search *paths* for mount points and load templates from them.
+ 
+     *paths* should be a list of filesystem paths to search for mount points.
+     If not specified will try to use value from environment variable
+     :envvar:`LUCIDITY_TEMPLATE_PATH`.
+ 
+     A mount point is a Python file that defines a 'register' function. The
+     function should return a list of instantiated
+     :py:class:`~lucidity.template.Template` objects.
+ 
+     If *recursive* is True (the default) then all directories under a path
+     will also be searched.
+ 
+     '''
+     templates = []
+ 
+     if paths is None:
+         paths = os.environ.get('LUCIDITY_TEMPLATE_PATH', '').split(os.pathsep)
+ 
+     for path in paths:
+         for base, directories, filenames in os.walk(path):
+             for filename in filenames:
+                 _, extension = os.path.splitext(filename)
+                 if extension != '.py':
+                     continue
+ 
+                 module_path = os.path.join(base, filename)
+                 module_name = uuid.uuid4().hex
+                 module = imp.load_source(module_name, module_path)
+                 try:
+                     registered = module.register()
+                 except AttributeError:
+                     pass
+                 else:
+                     if registered:
+                         templates.extend(registered)
+ 
+             if not recursive:
+                 del directories[:]
+ 
+     return templates
+ 
+ 
Add helper method to load templates from disk.
## Code Before:

from .template import Template


## Instruction:
Add helper method to load templates from disk.
## Code After:

import os
import uuid
import imp

from .template import Template


def discover_templates(paths=None, recursive=True):
    '''Search *paths* for mount points and load templates from them.

    *paths* should be a list of filesystem paths to search for mount points.
    If not specified will try to use value from environment variable
    :envvar:`LUCIDITY_TEMPLATE_PATH`.

    A mount point is a Python file that defines a 'register' function. The
    function should return a list of instantiated
    :py:class:`~lucidity.template.Template` objects.

    If *recursive* is True (the default) then all directories under a path
    will also be searched.

    '''
    templates = []

    if paths is None:
        paths = os.environ.get('LUCIDITY_TEMPLATE_PATH', '').split(os.pathsep)

    for path in paths:
        for base, directories, filenames in os.walk(path):
            for filename in filenames:
                _, extension = os.path.splitext(filename)
                if extension != '.py':
                    continue

                module_path = os.path.join(base, filename)
                module_name = uuid.uuid4().hex
                module = imp.load_source(module_name, module_path)
                try:
                    registered = module.register()
                except AttributeError:
                    pass
                else:
                    if registered:
                        templates.extend(registered)

            if not recursive:
                del directories[:]

    return templates

