Changeset 5695
- Timestamp:
- 11/17/08 14:00:03 (2 months ago)
- Files:
-
- branches/1.1/turbogears/startup.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/turbogears/startup.py
r5666 r5695 20 20 import signal 21 21 import time 22 23 from os.path import abspath, exists 22 24 23 25 import pkg_resources … … 114 116 # module public functions 115 117 def 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 """ 116 124 global DNS_SD_PID 117 125 if DNS_SD_PID: … … 137 145 # try registering with both or checking what service is running and use 138 146 # that. Program availability on the filesystem was never enough... 139 if os.path.exists(cmd):147 if exists(cmd): 140 148 DNS_SD_PID = os.spawnv(os.P_NOWAIT, cmd, [cmd]+args) 141 149 atexit.register(stop_bonjour) … … 143 151 144 152 def stop_bonjour(): 153 """Stop the bonjour publishing daemon if it is running.""" 145 154 if not DNS_SD_PID: 146 155 return … … 153 162 """Handles TurboGears tasks when the CherryPy server starts. 154 163 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. 155 180 This adds the "tg_js" configuration to make MochiKit accessible. 156 It also turns on stdlib logging when in development mode.157 181 158 182 """ 159 183 global webpath 184 conf = config.get 185 rfn = pkg_resources.resource_filename 186 187 # Add static filters 160 188 config.update({"/tg_static": 161 189 { 162 190 "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, 166 193 } 167 194 }) 168 195 config.update({"/tg_js" : 169 196 { 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, 174 200 } 175 201 }) 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 185 212 view.load_engines() 186 213 view.loadBaseTemplates() 187 214 188 webpath = config.get('server.webpath') or '' 215 # Add request filters 216 webpath = conf('server.webpath') or '' 189 217 190 218 if getattr(cherrypy, 'root', None): 191 219 if not hasattr(cherrypy.root, '_cp_filters'): 192 220 cherrypy.root._cp_filters = [] 193 cherrypy.root._cp_filters.extend([221 extra_filters = [ 194 222 VirtualPathFilter(webpath), 195 223 EndTransactionsFilter(), 196 224 NestedVariablesFilter(), 197 225 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) 199 234 200 235 webpath = webpath.lstrip('/') … … 202 237 webpath += '/' 203 238 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'): 207 244 setuplog = logging.getLogger() 208 245 setuplog.setLevel(logging.DEBUG) … … 214 251 setuplog.addHandler(handler) 215 252 216 logfile = conf ig.get("server.log_file")253 logfile = conf("server.log_file") 217 254 if logfile: 218 255 setuplog = logging.getLogger("turbogears.access") … … 224 261 setuplog.addHandler(handler) 225 262 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': 228 266 start_bonjour(bonjoursetting) 229 267 230 if config.get("sqlalchemy.dburi"): 268 # Bind metadata for SQLAlchemy 269 if conf("sqlalchemy.dburi"): 231 270 database.bind_metadata() 232 271 … … 247 286 entrypoint, e) 248 287 288 # Call oall registered startup functions 249 289 for item in call_on_startup: 250 290 item() 251 291 252 if config.get("tg.scheduler", False): 292 # Start the scheduler 293 if conf("tg.scheduler", False): 253 294 scheduler._start_scheduler() 254 295 log.info("Scheduler started")