From 7c56ce1a57976559341f22fe38be7f3f756ec4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 7 Nov 2006 14:38:19 +0000 Subject: [PATCH] support for loading extra modules --- authentic/__init__.py | 29 +++++++++++++++++++++++++++++ authentic/ctl/start.py | 3 +-- authenticctl.py | 12 ++++++------ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/authentic/__init__.py b/authentic/__init__.py index ee2ec96..0eefc6d 100644 --- a/authentic/__init__.py +++ b/authentic/__init__.py @@ -103,6 +103,34 @@ class AuthenticPublisher(Publisher): get_response().breadcrumb = [] self.install_lang(lang) return Publisher.try_publish(self, request) + + + extra_dirs = None + def register_extra_dir(cls, dir): + if not cls.extra_dirs: + cls.extra_dirs = [] + cls.extra_dirs.append(dir) + register_extra_dir = classmethod(register_extra_dir) + + def load_extra_dirs(cls): + for extra_dir in cls.extra_dirs or []: + if not os.path.exists(extra_dir): + continue + sys.path.append(extra_dir) + for filename in os.listdir(extra_dir): + if not filename.endswith('.py'): + continue + modulename = filename[:-3] + fp, pathname, description = imp.find_module(modulename, [extra_dir]) + try: + imp.load_module(modulename, fp, pathname, description) + except: + raise + logger.warn('failed to load extra module: %s' % modulename) + if fp: + fp.close() + load_extra_dirs = classmethod(load_extra_dirs) + class AuthenticVHostPublisher(AuthenticPublisher): def try_publish(self, request): @@ -115,6 +143,7 @@ class AuthenticVHostPublisher(AuthenticPublisher): def create_publisher(publisher_class = AuthenticPublisher): + publisher_class.load_extra_dirs() publisher = publisher_class(RootDirectory(), session_cookie_name = 'authentic', session_cookie_path = '/', diff --git a/authentic/ctl/start.py b/authentic/ctl/start.py index 7043f7e..b0f0425 100644 --- a/authentic/ctl/start.py +++ b/authentic/ctl/start.py @@ -15,14 +15,13 @@ def start(args): port = int(args[i+1]) i += 1 elif args[i] == '--extra': + authentic.AuthenticPublisher.register_extra_dir(args[i+1]) extra_dirs.append(args[i+1]) i += 1 elif args[i] == '--single-host': single_host = True i += 1 - authentic.extra_dirs = extra_dirs - try: if single_host: run(authentic.create_publisher, port=port, script_name = '') diff --git a/authenticctl.py b/authenticctl.py index a32896a..de0abad 100755 --- a/authenticctl.py +++ b/authenticctl.py @@ -2,9 +2,6 @@ import sys -import authentic.ctl - - def print_usage(): print 'Usage: authenticctl.py command [...]' print '' @@ -21,11 +18,14 @@ else: command = sys.argv[1] if command == 'start': - authentic.ctl.start(sys.argv[2:]) + from authentic.ctl.start import start + start(sys.argv[2:]) elif command == 'clean_sessions': - authentic.ctl.clean_sessions(sys.argv[2:]) + from authentic.ctl.clean_sessions import clean_sessions + clean_sessions(sys.argv[2:]) elif command == 'lock': - authentic.ctl.lock(sys.argv[2:]) + from authentic.ctl.lock import lock + lock(sys.argv[2:]) else: print_usage()