source: main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/app.py @ 10222

Last change on this file since 10222 was 5071, checked in by uli, 15 years ago

Change way to setup plugins as required by the new plugin API.

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