Changeset 5695

Show
Ignore:
Timestamp:
11/17/08 14:00:03 (2 months ago)
Author:
carndt
Message:

turbogears.startup cleanup and adding some docstrings & source code comments. Make sure that request filters do not get added more than once

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/startup.py

    r5666 r5695  
    2020import signal 
    2121import time 
     22 
     23from os.path import abspath, exists 
    2224 
    2325import pkg_resources 
     
    114116# module public functions 
    115117def start_bonjour(package=None): 
     118    """Register the TurboGears server with the Bonjour framework. 
     119 
     120    Currently only Unix-like systems are supported where either the 'avahi' 
     121    daemon (Linux etc.) is available or the 'dns-sd' program (Mac OS X). 
     122 
     123    """ 
    116124    global DNS_SD_PID 
    117125    if DNS_SD_PID: 
     
    137145        # try registering with both or checking what service is running and use 
    138146        # that.  Program availability on the filesystem was never enough... 
    139         if os.path.exists(cmd): 
     147        if exists(cmd): 
    140148            DNS_SD_PID = os.spawnv(os.P_NOWAIT, cmd, [cmd]+args) 
    141149            atexit.register(stop_bonjour) 
     
    143151 
    144152def stop_bonjour(): 
     153    """Stop the bonjour publishing daemon if it is running.""" 
    145154    if not DNS_SD_PID: 
    146155        return 
     
    153162    """Handles TurboGears tasks when the CherryPy server starts. 
    154163 
     164    This performs the following initialization tasks (in given order): 
     165 
     166    * Adds a static filter for TurboGears's static files (URL '/tg_static'). 
     167    * Adds a static filter for TurboGears's JavaScript files (URL '/tg_js'). 
     168    * Adds the decoding filter to the root URL ('/') if enabled in the 
     169      configuration. 
     170    * Loads the template engines and the base templates. 
     171    * Adds the CherryPy request filters to the root controller. 
     172    * Turns off CherryPy's logging filter when in development mode and turns on 
     173      stdlib logging. 
     174    * Registers the server with the Bonjour framework, if available. 
     175    * Calls 'turbogears.database.bind_metadata' when using SQLAlchemy. 
     176    * Loads all turbogears.extensions entry points and calls their 
     177      'start_extension' method. 
     178    * Calls the callables registered in 'turbogears.call_on_startup'. 
     179    * Starts the TurboGears scheduler. 
    155180    This adds the "tg_js" configuration to make MochiKit accessible. 
    156     It also turns on stdlib logging when in development mode. 
    157181 
    158182    """ 
    159183    global webpath 
     184    conf = config.get 
     185    rfn = pkg_resources.resource_filename 
     186 
     187    # Add static filters 
    160188    config.update({"/tg_static": 
    161189            { 
    162190            "static_filter.on": True, 
    163             "static_filter.dir": 
    164                 os.path.abspath(pkg_resources.resource_filename(__name__, "static")), 
    165             'log_debug_info_filter.on' : False, 
     191            "static_filter.dir": abspath(rfn(__name__, "static")), 
     192            'log_debug_info_filter.on': False, 
    166193            } 
    167194        }) 
    168195    config.update({"/tg_js" : 
    169196            { 
    170             "static_filter.on" : True, 
    171             "static_filter.dir" : 
    172                 os.path.abspath(pkg_resources.resource_filename(__name__, "static/js")), 
    173             'log_debug_info_filter.on' : False, 
     197            "static_filter.on": True, 
     198            "static_filter.dir": abspath(rfn(__name__, "static/js")), 
     199            'log_debug_info_filter.on': False, 
    174200            } 
    175201        }) 
    176     cherrypy.config.environments['development']['log_debug_info_filter.on'] = False 
    177  
    178     if config.get("decoding_filter.on", path="/") is None: 
    179         config.update({"/": { 
    180             "decoding_filter.on" : True, 
    181             "decoding_filter.encoding" : config.get( 
    182                                         "kid.encoding", "utf8") 
    183         }}) 
    184  
     202    # Add decoding filter 
     203    if conf("decoding_filter.on", path="/") is None: 
     204        config.update({"/": 
     205            { 
     206            "decoding_filter.on": True, 
     207            "decoding_filter.encoding": conf("kid.encoding", "utf8") 
     208            } 
     209        }) 
     210 
     211    # Initialize template engines & load base templates 
    185212    view.load_engines() 
    186213    view.loadBaseTemplates() 
    187214 
    188     webpath = config.get('server.webpath') or '' 
     215    # Add request filters 
     216    webpath = conf('server.webpath') or '' 
    189217 
    190218    if getattr(cherrypy, 'root', None): 
    191219        if not hasattr(cherrypy.root, '_cp_filters'): 
    192220            cherrypy.root._cp_filters = [] 
    193         cherrypy.root._cp_filters.extend(
     221        extra_filters =
    194222            VirtualPathFilter(webpath), 
    195223            EndTransactionsFilter(), 
    196224            NestedVariablesFilter(), 
    197225            SafeMultipartFilter() 
    198         ]) 
     226        ] 
     227        # Do not add filters twice which are already present 
     228        for cp_filter in cherrypy.root._cp_filters[:]: 
     229            for candidate in extra_filters: 
     230                if candidate.__class__ == cp_filter.__class__: 
     231                    extra_filters.remove(candidate) 
     232                    break 
     233        cherrypy.root._cp_filters.extend(extra_filters) 
    199234 
    200235    webpath = webpath.lstrip('/') 
     
    202237        webpath += '/' 
    203238 
    204     isdev = config.get('server.environment') == 'development' 
    205     if not config.get("tg.new_style_logging"): 
    206         if config.get('server.log_to_screen'): 
     239    # Set up logging 
     240    cherrypy.config.environments['development']['log_debug_info_filter.on' 
     241        ] = False 
     242    if not conf("tg.new_style_logging"): 
     243        if conf('server.log_to_screen'): 
    207244            setuplog = logging.getLogger() 
    208245            setuplog.setLevel(logging.DEBUG) 
     
    214251            setuplog.addHandler(handler) 
    215252 
    216         logfile = config.get("server.log_file") 
     253        logfile = conf("server.log_file") 
    217254        if logfile: 
    218255            setuplog = logging.getLogger("turbogears.access") 
     
    224261            setuplog.addHandler(handler) 
    225262 
    226     bonjoursetting = config.get("tg.bonjour", None) 
    227     if bonjoursetting or isdev: 
     263    # Register server with Bonjour framework 
     264    bonjoursetting = conf("tg.bonjour", None) 
     265    if bonjoursetting or conf('server.environment') == 'development': 
    228266        start_bonjour(bonjoursetting) 
    229267 
    230     if config.get("sqlalchemy.dburi"): 
     268    # Bind metadata for SQLAlchemy 
     269    if conf("sqlalchemy.dburi"): 
    231270        database.bind_metadata() 
    232271 
     
    247286                    entrypoint, e) 
    248287 
     288    # Call oall registered startup functions 
    249289    for item in call_on_startup: 
    250290        item() 
    251291 
    252     if config.get("tg.scheduler", False): 
     292    # Start the scheduler 
     293    if conf("tg.scheduler", False): 
    253294        scheduler._start_scheduler() 
    254295        log.info("Scheduler started")