source: main/waeup.sirp/branches/ulif-paster/src/waeup/sirp/app.py @ 11075

Last change on this file since 11075 was 5421, checked in by uli, 14 years ago

Add a poral method to update all plugins. This could ease some
upgrading tasks in future tremendously.

  • Property svn:eol-style set to native
File size: 4.2 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   
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,
32        setup = setup_authentication,)
33
34    # The name of the university.
35    name=u'Sample University'
36   
37    # The default layout.
38    skin=u'waeuptheme-gray1.css'
39   
40    @property
41    def logger(self):
42        """The application logger.
43
44        Returns a standard logger object as provided by :mod:`logging`
45        module from the standard library.
46       
47        Other components can use this logger to perform log entries
48        into the 'main' logfile.
49
50        The logger is initialized the first time, it is called.
51        """
52        sitename = self.__name__
53        loggername = 'waeup.sirp.%s' % sitename
54        logger = logging.getLogger(loggername)
55        if not logger.handlers:
56            logger = self._setupLogger(logger)
57        return logger
58       
59   
60    def __init__(self, name=name, skin=skin, **kw):
61        super(University, self).__init__(**kw)
62        self.name = name
63        self.skin = skin
64        self.setup()
65
66    def setup(self):
67        """Setup some hard-wired components.
68
69        Create local datacenter, containers for users, students and
70        the like.
71        """
72        self['users'] = UserContainer()
73        self['datacenter'] = DataCenter()
74
75        self['students'] = createObject(u'waeup.StudentContainer')
76        self['hostels'] = createObject(u'waeup.HostelContainer')
77        self._createPlugins()
78
79    def _createPlugins(self):
80        """Create instances of all plugins defined somewhere.
81        """
82        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
83            plugin.setup(self, name, self.logger)
84        return
85
86    def updatePlugins(self):
87        """Lookup all plugins and call their `update()` method.
88        """
89        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
90            plugin.update(self, name, self.logger)
91        return
92
93    def _setupLogger(self, logger):
94        """Setup general application logger.
95
96        The logfile will be stored in the datacenter logs/ dir.
97        """
98        logdir = os.path.join(self['datacenter'].storage, 'logs')
99        if not os.path.exists(logdir):
100            os.mkdir(logdir)
101        filename = os.path.join(logdir, 'application.log')
102
103        # Create a rotating file handler logger for application.
104        handler = logging.handlers.RotatingFileHandler(
105            filename, maxBytes=5*1024**1, backupCount=7)
106        formatter = logging.Formatter(
107            '%(asctime)s - %(levelname)s - %(message)s')
108        handler.setFormatter(formatter)
109       
110        # Don't send log msgs to ancestors. This stops displaying
111        # logmessages on the commandline.
112        logger.propagate = False
113        logger.addHandler(handler)
114        return logger
115
116@grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent)
117def handle_storage_move(obj, event):
118    """Event handler, in case datacenter storage moves.
119
120    We initialize the application log again, then.
121    """
122    app = grok.getSite()
123    if app is None:
124        return
125    if obj is not app['datacenter']:
126        return
127    logger = app.logger
128    logger.warn('Log Dir moved. Closing.')
129    handlers = logger.handlers
130    for handler in handlers:
131        logger.removeHandler(handler)
132    app._setupLogger(logger)
133    logger.warn('Log file moved. Opening.')
Note: See TracBrowser for help on using the repository browser.