<s> import tornado . web <EOL> import importlib <EOL> import copy <EOL> import os <EOL> from . utils . imp import load_class <EOL> from . template . base import Library <EOL> _global_app = None <EOL> def get_app ( ) : <EOL> global _global_app <EOL> if _global_app is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return _global_app <EOL> def set_app ( _app ) : <EOL> global _global_app <EOL> _global_app = _app <EOL> def del_app ( ) : <EOL> global _global_app <EOL> _global_app = None <EOL> class Application ( tornado . web . Application ) : <EOL> engine = None <EOL> session_engine = None <EOL> jinja_env = None <EOL> auth_backends = [ ] <EOL> context_processors = [ ] <EOL> def __init__ ( self , settings ) : <EOL> self . conf = settings <EOL> handlers = self . conf . HANDLERS <EOL> if handlers is not None : <EOL> try : <EOL> handlers = load_class ( handlers + "<STR_LIT>" ) <EOL> except ImportError : <EOL> raise RuntimeError ( "<STR_LIT>" . format ( handlers ) ) <EOL> handlers = self . _setup_handlers ( handlers ) or None <EOL> tornado_settings = copy . deepcopy ( self . conf . TORNADO_SETTINGS ) <EOL> tornado_settings . update ( { "<STR_LIT>" : self . conf . SECRET_KEY } ) <EOL> super ( Application , self ) . __init__ ( handlers = handlers , default_host = self . conf . TORNADO_DEFAULT_HOST , <EOL> transforms = self . conf . TORNADO_TRANSFORMS , wsgi = self . conf . TORNADO_WSGI_MODE , ** tornado_settings ) <EOL> self . _setup_database_engine ( ) <EOL> self . _setup_session_engine ( ) <EOL> self . _setup_authentication_engine ( ) <EOL> self . _setup_template_loaders ( ) <EOL> self . _setup_template_engine ( ) <EOL> self . _setup_i18n ( ) <EOL> self . _setup_installed_modules ( ) <EOL> set_app ( self ) <EOL> def _setup_handlers ( self , handlers ) : <EOL> if handlers is None : <EOL> return None <EOL> return [ ( x , load_class ( y ) ) for x , y in handlers ] <EOL> def _setup_database_engine ( self ) : <EOL> from sqlalchemy import create_engine <EOL> from sqlalchemy . orm import scoped_session , sessionmaker <EOL> engine_url = self . conf . SQLALCHEMY_ENGINE_URL <EOL> engine_kwargs = self . conf . SQLALCHEMY_ENGINE_KWARGS <EOL> if not engine_url : <EOL> return <EOL> self . engine = create_engine ( engine_url , ** engine_kwargs ) <EOL> self . db = scoped_session ( sessionmaker ( bind = self . engine ) ) <EOL> def _setup_i18n ( self ) : <EOL> if not self . conf . I18N : <EOL> return <EOL> if self . conf . I18N_DIRECTORIES is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> pwd = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> pwd_locale = os . path . join ( pwd , "<STR_LIT>" ) <EOL> if os . path . exists ( pwd_locale ) : <EOL> self . conf . I18N_DIRECTORIES . append ( pwd_locale ) <EOL> from webtools . utils import locale <EOL> locale . set_default_locale ( self . conf . I18N_DEFAULT_LANG ) <EOL> locale . load_gettext_translations ( frozenset ( self . conf . I18N_DIRECTORIES ) , self . conf . I18N_DOMAIN ) <EOL> def _setup_installed_modules ( self ) : <EOL> from jinja2 import PackageLoader <EOL> self . models_modules = [ ] <EOL> if not self . conf . INSTALLED_MODULES : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> for module_path in self . conf . INSTALLED_MODULES : <EOL> print ( "<STR_LIT>" . format ( module_path ) ) <EOL> module = None <EOL> try : <EOL> module = importlib . import_module ( module_path ) <EOL> except ImportError : <EOL> continue <EOL> self . loaders . append ( PackageLoader ( module_path , "<STR_LIT>" ) ) <EOL> try : <EOL> importlib . import_module ( module_path + "<STR_LIT>" ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> models_mod = importlib . import_module ( module_path + "<STR_LIT>" ) <EOL> self . models_modules . append ( models_mod ) <EOL> except ImportError : <EOL> pass <EOL> if Library . _instance : <EOL> Library . _instance . _update_env ( self . jinja_env ) <EOL> def _setup_template_loaders ( self ) : <EOL> """<STR_LIT>""" <EOL> from jinja2 import FileSystemLoader <EOL> template_dirs = self . conf . JINJA2_TEMPLATE_DIRS <EOL> if template_dirs is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . loaders = [ ] <EOL> for params in template_dirs : <EOL> if isinstance ( params , str ) : <EOL> self . loaders . append ( FileSystemLoader ( params ) ) <EOL> continue <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _setup_template_engine ( self ) : <EOL> from jinja2 import Environment , ChoiceLoader <EOL> self . jinja_env = Environment ( loader = ChoiceLoader ( self . loaders ) , <EOL> ** self . conf . JINJA2_SETTINGS ) <EOL> context_processors = self . conf . JINJA2_CONTEXT_PROCESSORS or [ ] <EOL> self . context_processors = [ load_class ( x ) for x in set ( context_processors ) ] <EOL> self . jinja_env . globals [ "<STR_LIT:_>" ] = load_class ( "<STR_LIT>" ) <EOL> def _setup_session_engine ( self ) : <EOL> if not self . conf . SESSION_ENGINE : <EOL> return <EOL> klass = load_class ( self . conf . SESSION_ENGINE ) <EOL> self . session_engine = klass ( self , ** self . conf . SESSION_ENGINE_KWARGS ) <EOL> def _setup_authentication_engine ( self ) : <EOL> if not self . conf . AUTHENTICATION_BACKENDS : <EOL> return <EOL> assert isinstance ( self . conf . AUTHENTICATION_BACKENDS , ( tuple , list ) ) , "<STR_LIT>" <EOL> self . auth_backends = [ load_class ( x ) ( self ) for x in self . conf . AUTHENTICATION_BACKENDS ] <EOL> if self . session_engine is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . conf . JINJA2_CONTEXT_PROCESSORS . append ( "<STR_LIT>" ) <EOL> def _authenticate ( self , username = None , password = None ) : <EOL> for backend in self . auth_backends : <EOL> user = backend . authenticate ( username = username , password = password ) <EOL> if user : <EOL> return user <EOL> return None </s>
