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
Line 
1import grok
2import logging
3import os
4from zope.app.authentication.authentication import PluggableAuthentication
5from zope.app.security.interfaces import IAuthentication
6from zope.component import createObject, getAllUtilitiesRegisteredFor
7
8from waeup.sirp.interfaces import (
9    IUniversity, IDataCenter, IWAeUPSIRPPluggable,
10    IDataCenterStorageMovedEvent)
11from waeup.sirp.authentication import setup_authentication
12from waeup.sirp.datacenter import DataCenter
13from waeup.sirp.users import UserContainer
14
15class University(grok.Application, grok.Container):
16    """A university.
17    """
18    grok.implements(IUniversity)
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)
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       
34   
35    def __init__(self, name=u'Sample University', **kw):
36        super(University, self).__init__(**kw)
37        self.name = name
38        self.setup()
39
40    def setup(self):
41        self['users'] = UserContainer()
42        self['datacenter'] = DataCenter()
43
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       
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
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.