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

Last change on this file since 5004 was 4968, checked in by uli, 15 years ago

Remove usage of datareceivers.

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