Changeset 4764

Show
Ignore:
Timestamp:
06/18/08 02:05:45 (5 months ago)
Author:
mramm
Message:

Environment now setup from base_config object in the tg framework, rather than in template code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/config/environment.py_tmpl

    r4693 r4764  
    1 """Pylons environment configuration""" 
    2 import os 
    3  
    4 {{if template_engine == 'mako'}} 
    5 from mako.lookup import TemplateLookup 
    6 {{elif template_engine == 'genshi'}} 
    7 from genshi.template import TemplateLoader 
    8 {{elif template_engine == 'jinja'}} 
    9 from jinja import ChoiceLoader, Environment, FileSystemLoader 
    10 {{endif}} 
    11 from pylons import config 
    12  
    13 from pylons.i18n import ugettext 
    14 from genshi.filters import Translator 
    15 from tg import defaults 
    16  
    17 {{if sqlalchemy}} 
    18 from sqlalchemy import engine_from_config 
    19 {{endif}} 
    20  
    21 import {{package}}.lib.app_globals as app_globals 
    22 #import {{package}}.lib.helpers 
    23 #from {{package}}.config.routing import make_map 
    24 {{if sqlalchemy}} 
    25 from {{package}}.model import init_model, DBSession, metadata 
    26 {{endif}} 
    27 {{if identity=='sqlalchemy'}} 
    28 from {{package}}.model import User, Group, Permission 
    29 {{endif}} 
    30  
    31 {{if template_engine == 'genshi'}} 
    32 def template_loaded(template): 
    33     "Plug-in our i18n function to Genshi." 
    34     template.filters.insert(0, Translator(ugettext)) 
    35 {{endif}} 
    36  
    37 def load_environment(global_conf, app_conf): 
    38     """Configure the Pylons environment via the ``pylons.config`` 
    39     object 
    40     """ 
    41     # Pylons paths 
    42     root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
    43     paths = dict(root=root, 
    44                  controllers=os.path.join(root, 'controllers'), 
    45                  static_files=os.path.join(root, 'public'), 
    46                  templates=[os.path.join(root, 'templates')]) 
    47  
    48     # Initialize config with the basic options 
    49     config.init_app(global_conf, app_conf, package='{{package}}', paths=paths) 
    50  
    51     # This setups up a set of default route that enables a standard 
    52     # TG2 style object dispatch.   Fell free to overide it with 
    53     # custom routes.  TODO: Link to TG2+routes doc. 
    54     make_map = defaults.make_default_route_map 
    55  
    56     config['routes.map'] = make_map() 
    57     config['pylons.app_globals'] = app_globals.Globals() 
    58     #config['pylons.h'] = {{package}}.lib.helpers 
    59      
    60     {{if identity == "sqlalchemy"}} 
    61  
    62     config['identity'] = {'user_class':User,  
    63                       'group_class':Group,  
    64                       'permission_class':Permission, 
    65                       'users_table':'tg_user', 
    66                       'groups_table':'tg_group', 
    67                       'permissions_table':'tg_permission', 
    68                       'password_encryption_method':'sha', 
    69                       } 
    70      
    71  
    72     {{endif}} 
    73      
    74     {{if template_engine == 'mako'}} 
    75     # Create the Mako TemplateLookup, with the default auto-escaping 
    76     config['pylons.app_globals'].mako_lookup = TemplateLookup( 
    77         directories=paths['templates'], 
    78         module_directory=os.path.join(app_conf['cache_dir'], 'templates'), 
    79         input_encoding='utf-8', output_encoding='utf-8', 
    80         imports=['from webhelpers.html import escape'], 
    81         default_filters=['escape']) 
    82     {{elif template_engine == 'genshi'}} 
    83      
    84     # Create the Genshi TemplateLoader 
    85     config['pylons.app_globals'].genshi_loader = TemplateLoader( 
    86         paths['templates'], auto_reload=True) 
    87     {{elif template_engine == 'jinja'}} 
    88  
    89     # Create the Jinja Environment 
    90     config['pylons.app_globals'].jinja_env = Environment(loader=ChoiceLoader( 
    91             [FileSystemLoader(path) for path in paths['templates']])) 
    92     # Jinja's unable to request c's attributes without strict_c 
    93     config['pylons.strict_c'] = True 
    94     {{endif}} 
    95  
    96     # If you'd like to change the default template engine used to render 
    97     # text/html content, edit these options. 
    98     default_template_engine = 'genshi' 
    99     default_template_engine_options = {} 
    100     config['buffet.template_engines'].pop() 
    101     config.add_template_engine(default_template_engine, '${package}.templates', 
    102                                default_template_engine_options) 
    103  
    104     {{if sqlalchemy}}     
    105     # Setup SQLAlchemy database engine 
    106     engine = engine_from_config(config, 'sqlalchemy.') 
    107     config['pylons.app_globals'].sa_engine = engine 
    108     # Pass the engine to initmodel, to be able to introspect tables 
    109     init_model(engine) 
    110     DBSession.configure(bind=engine) 
    111     metadata.bind = engine 
    112     {{endif}} 
    113      
    114     # CONFIGURATION OPTIONS HERE (note: all config options will override 
    115     # any Pylons config options) 
  • projects/tg.devtools/trunk/devtools/templates/turbogears/+package+/config/middleware.py_tmpl

    r4762 r4764  
    33from tg.middleware import setup_tg_wsgi_app 
    44from tg.config import AppConfig, Bunch 
    5 from {{package}}.config.environment import load_environment 
     5from tg.environment import make_load_environment 
     6import {{package}} 
    67from {{package}} import model 
     8from {{package}}.lib import app_globals, helpers 
    79 
    810base_config = AppConfig() 
     11base_config.renderers = [] 
     12 
     13 
     14base_config.package = {{package}} 
    915 
    1016#Set the default renderer 
    1117{{if template_engine == 'mako'}} 
    12 base_config.default_renderer = 'mako'  
     18base_config.default_renderer = 'mako' 
     19base_config.renderers.append('mako')   
    1320{{elif template_engine == 'jinja'}} 
    14 base_config.default_renderer = 'jinja'  
     21base_config.default_renderer = 'jinja' 
     22base_config.renderers.append('jinja')   
    1523{{elif template_engine == 'genshi'}} 
    16 base_config.default_renderer = 'genshi'  
     24base_config.default_renderer = 'genshi' 
     25base_config.renderers.append('genshi')  
    1726{{endif}} 
    1827 
     28#Configure the base SQLALchemy Setup 
     29base_config.use_sqlalchemy = True 
     30base_config.model = model 
     31 
    1932{{if identity == "sqlalchemy"}} 
     33#Configure the authentication backend 
    2034base_config.auth_backend = 'sqlalchemy' 
    2135base_config.sa_auth = Bunch() 
     
    2640{{endif}} 
    2741 
     42#Use base_config to setup the environment loader function 
     43load_environment = make_load_environment(base_config) 
     44 
     45#Use base_config to setup the nessisary WSGI App factory.  
     46#make_base_app will wrap the TG2 app with all the middleware it needs.  
    2847make_base_app = setup_tg_wsgi_app(load_environment, base_config) 
    29 #Don't ever use base_config directly 
    30 base_config = {} 
    3148 
    3249def make_app(global_conf, full_stack=True, **app_conf):