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

Last change on this file since 5386 was 5345, checked in by uli, 14 years ago

Tell a bit more in docstrings.

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