# 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
        if not self.conf.INSTALLED_MODULES:
            raise RuntimeError("INSTALLED_MODULES is mandatory setting.")

        # setup module template loader
        for module_path in self.conf.INSTALLED_MODULES:
            print("Setup {0} module...".format(module_path))

            module = None
            try:
                module = importlib.import_module(module_path)
            except ImportError:
                continue

            # setup module template loader
            self.loaders.append(PackageLoader(module_path, "templates"))

            # setup jinja template additions
            try:
                importlib.import_module(module_path + ".jinja2_addons")
            except ImportError:
                pass

            # try import models
            try:
                models_mod = importlib.import_module(module_path + ".models")
                self.models_modules.append(models_mod)
            except ImportError:
                pass

        if Library._instance:

# 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

        if not self.conf.INSTALLED_MODULES:
            raise RuntimeError("INSTALLED_MODULES is mandatory setting.")

        # setup module template loader
        for module_path in self.conf.INSTALLED_MODULES:
            print("Setup {0} module...".format(module_path))

            module = None
            try:
                module = importlib.import_module(module_path)
            except ImportError:
                continue

            # setup module template loader
            self.loaders.append(PackageLoader(module_path, "templates"))

            # setup jinja template additions
            try:
                importlib.import_module(module_path + ".jinja2_addons")
            except ImportError:
                pass

            # try import models
            try:
                models_mod = importlib.import_module(module_path + ".models")
                self.models_modules.append(models_mod)
            except ImportError:
                pass

        if Library._instance:

            Library._instance._update_env(self.jinja_env)