1 | import grok |
---|
2 | import logging |
---|
3 | import os |
---|
4 | try: |
---|
5 | from zope.authentication.interfaces import IAuthentication |
---|
6 | except: |
---|
7 | # BBB |
---|
8 | from zope.app.security.interfaces import IAuthentication |
---|
9 | from zope.component import createObject, getUtilitiesFor |
---|
10 | try: |
---|
11 | from zope.pluggableauth import PluggableAuthentication |
---|
12 | except ImportError: |
---|
13 | # BBB |
---|
14 | from zope.app.authentication.authentication import PluggableAuthentication |
---|
15 | |
---|
16 | from waeup.sirp.authentication import setup_authentication |
---|
17 | from waeup.sirp.datacenter import DataCenter |
---|
18 | from waeup.sirp.interfaces import ( |
---|
19 | IUniversity, IDataCenter, IWAeUPSIRPPluggable, |
---|
20 | IDataCenterStorageMovedEvent) |
---|
21 | from waeup.sirp.users import UserContainer |
---|
22 | |
---|
23 | class 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 | @property |
---|
38 | def logger(self): |
---|
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 | """ |
---|
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 | |
---|
56 | |
---|
57 | def __init__(self, name=u'Sample University', **kw): |
---|
58 | super(University, self).__init__(**kw) |
---|
59 | self.name = name |
---|
60 | self.setup() |
---|
61 | |
---|
62 | def setup(self): |
---|
63 | """Setup some hard-wired components. |
---|
64 | |
---|
65 | Create local datacenter, containers for users, students and |
---|
66 | the like. |
---|
67 | """ |
---|
68 | self['users'] = UserContainer() |
---|
69 | self['datacenter'] = DataCenter() |
---|
70 | |
---|
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 | """ |
---|
78 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
79 | plugin.setup(self, name, self.logger) |
---|
80 | return |
---|
81 | |
---|
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 |
---|
104 | |
---|
105 | @grok.subscribe(IDataCenter, IDataCenterStorageMovedEvent) |
---|
106 | def 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.') |
---|