# Path: webtools/utils/imp.py
# def load_class(path):
#     """
#     Load class from path.
#     """
# 
#     mod_name, klass_name = None, None
# 
#     try:
#         mod_name, klass_name = path.rsplit('.', 1)
#         mod = import_module(mod_name)
#     except AttributeError as e:
#         raise RuntimeError("Error importing {0}: '{1}'".format(mod_name, e))
# 
#     try:
#         klass = getattr(mod, klass_name)
#     except AttributeError:
#         raise RuntimeError('Module "{0}" does not define a "{1}" class'.format(mod_name, klass_name))
# 
#     return klass
# 
# Path: webtools/template/base.py
# class Library(object):
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             cls._instance = super(Library, self).__new__(*args, **kwargs)
#         return cls._instance
# 
#     def __init__(self):
#         self._globals = {}
#         self._tests = {}
#         self._filters = {}
# 
#     def _update_env(self, env):
#         env.filters.update(self._filters)
#         env.globals.update(self._globals)
#         env.tests.update(self._tests)
# 
#     def _new_function(self, attr, func, name=None):
#         _attr = getattr(self, attr)
#         if name is None:
#             name = func.__name__
# 
#         _attr[name] = func
#         return func
# 
#     def _function(self, attr, name=None, _function=None):
#         if name is None and _function is None:
#             def dec(func):
#                 return self._new_function(attr, func)
#             return dec
# 
#         elif name is not None and _function is None:
#             if callable(name):
#                 return self._new_function(attr, name)
# 
#             else:
#                 def dec(func):
#                     return self._function(attr, name, func)
#                 return dec
# 
#         elif name is not None and _function is not None:
#             return self._new_function(attr, _function, name)
# 
#         raise RuntimeError("Invalid parameters")
# 
#     def global_function(self, *args, **kwargs):
#         return self._function("_globals", *args, **kwargs)
# 
#     def test(self, *args, **kwargs):
#         return self._function("_tests", *args, **kwargs)
# 
#     def filter(self, *args, **kwargs):
#         return self._function("_filters", *args, **kwargs)


import tornado.web
import importlib
import copy
import os
from .utils.imp import load_class
from .template.base import Library
        from sqlalchemy import create_engine
        from sqlalchemy.orm import scoped_session, sessionmaker
        from webtools.utils import locale
        from jinja2 import PackageLoader
        from jinja2 import FileSystemLoader
        from jinja2 import Environment, ChoiceLoader
        """

        template_dirs = self.conf.JINJA2_TEMPLATE_DIRS
        if template_dirs is None:
            raise RuntimeError("Missing `JINJA2_TEMPLATE_DIRS` setting")

        self.loaders = []
        for params in template_dirs:
            if isinstance(params, str):
                self.loaders.append(FileSystemLoader(params))
                continue

            raise RuntimeError("Invalid JINJA2_TEMPLATE_DIRS variable on settings")

    def _setup_template_engine(self):

        self.jinja_env = Environment(loader=ChoiceLoader(self.loaders),
            **self.conf.JINJA2_SETTINGS)

        # load context processors
        context_processors = self.conf.JINJA2_CONTEXT_PROCESSORS or []
        self.context_processors = [load_class(x) for x in set(context_processors)]

        # load defautl builtin tags
        self.jinja_env.globals["_"] = load_class("webtools.template.default_extensions.ugettext")

    def _setup_session_engine(self):
        if not self.conf.SESSION_ENGINE:
            return


# Path: webtools/utils/imp.py
# def load_class(path):
#     """
#     Load class from path.
#     """
# 
#     mod_name, klass_name = None, None
# 
#     try:
#         mod_name, klass_name = path.rsplit('.', 1)
#         mod = import_module(mod_name)
#     except AttributeError as e:
#         raise RuntimeError("Error importing {0}: '{1}'".format(mod_name, e))
# 
#     try:
#         klass = getattr(mod, klass_name)
#     except AttributeError:
#         raise RuntimeError('Module "{0}" does not define a "{1}" class'.format(mod_name, klass_name))
# 
#     return klass
# 
# Path: webtools/template/base.py
# class Library(object):
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             cls._instance = super(Library, self).__new__(*args, **kwargs)
#         return cls._instance
# 
#     def __init__(self):
#         self._globals = {}
#         self._tests = {}
#         self._filters = {}
# 
#     def _update_env(self, env):
#         env.filters.update(self._filters)
#         env.globals.update(self._globals)
#         env.tests.update(self._tests)
# 
#     def _new_function(self, attr, func, name=None):
#         _attr = getattr(self, attr)
#         if name is None:
#             name = func.__name__
# 
#         _attr[name] = func
#         return func
# 
#     def _function(self, attr, name=None, _function=None):
#         if name is None and _function is None:
#             def dec(func):
#                 return self._new_function(attr, func)
#             return dec
# 
#         elif name is not None and _function is None:
#             if callable(name):
#                 return self._new_function(attr, name)
# 
#             else:
#                 def dec(func):
#                     return self._function(attr, name, func)
#                 return dec
# 
#         elif name is not None and _function is not None:
#             return self._new_function(attr, _function, name)
# 
#         raise RuntimeError("Invalid parameters")
# 
#     def global_function(self, *args, **kwargs):
#         return self._function("_globals", *args, **kwargs)
# 
#     def test(self, *args, **kwargs):
#         return self._function("_tests", *args, **kwargs)
# 
#     def filter(self, *args, **kwargs):
#         return self._function("_filters", *args, **kwargs)


# Path: webtools/application.py
import tornado.web
import importlib
import copy
import os
from .utils.imp import load_class
from .template.base import Library
        from sqlalchemy import create_engine
        from sqlalchemy.orm import scoped_session, sessionmaker
        from webtools.utils import locale
        from jinja2 import PackageLoader
        from jinja2 import FileSystemLoader
        from jinja2 import Environment, ChoiceLoader

        """

        template_dirs = self.conf.JINJA2_TEMPLATE_DIRS
        if template_dirs is None:
            raise RuntimeError("Missing `JINJA2_TEMPLATE_DIRS` setting")

        self.loaders = []
        for params in template_dirs:
            if isinstance(params, str):
                self.loaders.append(FileSystemLoader(params))
                continue

            raise RuntimeError("Invalid JINJA2_TEMPLATE_DIRS variable on settings")

    def _setup_template_engine(self):

        self.jinja_env = Environment(loader=ChoiceLoader(self.loaders),
            **self.conf.JINJA2_SETTINGS)

        # load context processors
        context_processors = self.conf.JINJA2_CONTEXT_PROCESSORS or []
        self.context_processors = [load_class(x) for x in set(context_processors)]

        # load defautl builtin tags
        self.jinja_env.globals["_"] = load_class("webtools.template.default_extensions.ugettext")

    def _setup_session_engine(self):
        if not self.conf.SESSION_ENGINE:
            return


        klass = load_class(self.conf.SESSION_ENGINE)