source: main/waeup.sirp/trunk/src/waeup/sirp/app.py @ 5053

Last change on this file since 5053 was 5016, checked in by uli, 15 years ago

Stop hardcoding of creation of faculties. Use plugin mechanism
instead.

  • Property svn:eol-style set to native
File size: 3.2 KB
RevLine 
[3521]1import grok
[4874]2import logging
3import os
[4789]4from zope.app.authentication.authentication import PluggableAuthentication
5from zope.app.security.interfaces import IAuthentication
[5016]6from zope.component import createObject, getAllUtilitiesRegisteredFor
[3521]7
[5016]8from waeup.sirp.interfaces import (
9    IUniversity, IDataCenter, IWAeUPSIRPPluggable,
10    IDataCenterStorageMovedEvent)
[4920]11from waeup.sirp.authentication import setup_authentication
12from waeup.sirp.datacenter import DataCenter
13from waeup.sirp.users import UserContainer
[4789]14
[3521]15class University(grok.Application, grok.Container):
[4789]16    """A university.
17    """
[4968]18    grok.implements(IUniversity)
[4789]19    # Setup authentication for this app. Note: this is only
20    # initialized, when a new instance of this app is created.
21    grok.local_utility(
22        PluggableAuthentication, provides = IAuthentication,
23        setup = setup_authentication)
[4874]24
25    @property
26    def logger(self):
27        sitename = self.__name__
28        loggername = 'waeup.sirp.%s' % sitename
29        logger = logging.getLogger(loggername)
30        if not logger.handlers:
31            logger = self._setupLogger(logger)
32        return logger
33       
[4789]34   
35    def __init__(self, name=u'Sample University', **kw):
[3521]36        super(University, self).__init__(**kw)
37        self.name = name
[4789]38        self.setup()
[3521]39
[4789]40    def setup(self):
41        self['users'] = UserContainer()
42        self['datacenter'] = DataCenter()
[4874]43
[5016]44        self['students'] = createObject(u'waeup.StudentContainer')
45        self['hostels'] = createObject(u'waeup.HostelContainer')
46        self._createPlugins()
47
48    def _createPlugins(self):
49        """Create instances of all plugins defined somewhere.
50        """
51        plugins = getAllUtilitiesRegisteredFor(IWAeUPSIRPPluggable)
52        for plugin in plugins:
53            self[plugin.name] = createObject(plugin.factory_name)
54       
[4874]55    def _setupLogger(self, logger):
56        """Setup general application logger.
57
58        The logfile will be stored in the datacenter logs/ dir.
59        """
60        logdir = os.path.join(self['datacenter'].storage, 'logs')
61        if not os.path.exists(logdir):
62            os.mkdir(logdir)
63        filename = os.path.join(logdir, 'application.log')
64
65        # Create a rotating file handler logger for application.
66        handler = logging.handlers.RotatingFileHandler(
67            filename, maxBytes=5*1024**1, backupCount=7)
68        formatter = logging.Formatter(
69            '%(asctime)s - %(levelname)s - %(message)s')
70        handler.setFormatter(formatter)
71       
72        # Don't send log msgs to ancestors. This stops displaying
73        # logmessages on the commandline.
74        logger.propagate = False
75        logger.addHandler(handler)
76        return logger
[4884]77
78@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
79def handle_storage_move(obj, event):
80    """Event handler, in case datacenter storage moves.
81
82    We initialize the application log again, then.
83    """
84    app = grok.getSite()
85    if app is None:
86        return
87    if obj is not app['datacenter']:
88        return
89    logger = app.logger
90    logger.warn('Log Dir moved. Closing.')
91    handlers = logger.handlers
92    for handler in handlers:
93        logger.removeHandler(handler)
94    app._setupLogger(logger)
95    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.