Added support for module-level __init__ and __del__ methods.

Useful to open and close persistent databases.
This commit is contained in:
sebd 2005-05-09 14:34:54 +00:00
parent 17614641a6
commit 3e3e524358
1 changed files with 22 additions and 2 deletions

View File

@ -63,6 +63,7 @@ import stations
import strings
import stylesheets
import submissions
import types
import things
import webdav
import xmlschemas
@ -659,12 +660,21 @@ def main():
['/usr/local/lib/expression/modules',
'/usr/lib/expression/modules'])
# Import all modules that are listed in the configuration file.
# When an imported module provides an __init__() method, call it.
for moduleName in configuration.getPythonModuleNames():
try:
module = __import__(moduleName)
__import__(moduleName)
except ImportError:
logs.error("Failed to load module %s; skipping" % moduleName)
logs.error("Failed to load module %s" % moduleName)
raise
else:
module = sys.modules[moduleName]
if hasattr(module, '__init__'):
method = getattr(module, '__init__')
if type(method) == types.FunctionType:
logs.debug("Calling %s.__init__()" % moduleName)
method()
for namespace in configuration.evaluateXpath("yep:namespace"):
name, uri = namespace.prop("name"), namespace.prop("uri")
@ -792,6 +802,16 @@ def main():
httpServer.serve_forever()
except KeyboardInterrupt:
pass
# For each module that was imported at start-up (because listed in the configuration file),
# call its __del__() method, if present.
for moduleName in configuration.getPythonModuleNames():
if moduleName in sys.modules:
module = sys.modules[moduleName]
if hasattr(module, '__del__'):
method = getattr(module, '__del__')
if type(method) == types.FunctionType:
logs.debug("Calling %s.__del__()" % moduleName)
method()
application.stop()
del HttpRequestHandler.baseEnviron
finally: