From 31162de72532847fd0989329a408afb6a4c5b1f6 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 3 Dec 2013 16:49:24 -0500 Subject: Add plugins loader Signed-off-by: Simo Sorce --- src/ipsilon.py | 6 ++++ src/util/__init__.py | 0 src/util/plugin.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/util/__init__.py create mode 100755 src/util/plugin.py (limited to 'src') diff --git a/src/ipsilon.py b/src/ipsilon.py index 3ab3d18..affab32 100755 --- a/src/ipsilon.py +++ b/src/ipsilon.py @@ -23,6 +23,7 @@ sys.stdout = sys.stderr import atexit import threading import cherrypy +from util import plugin class Root(object): @@ -30,6 +31,11 @@ class Root(object): def index(self): return '/' + +plugins = plugin.Plugins() +idp_providers = plugins.get_providers() +cherrypy.config.update({'idp_providers': idp_providers}) + if __name__ == "__main__": cherrypy.quickstart(Root()) diff --git a/src/util/__init__.py b/src/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/util/plugin.py b/src/util/plugin.py new file mode 100755 index 0000000..be9ed02 --- /dev/null +++ b/src/util/plugin.py @@ -0,0 +1,80 @@ +#!/usr/bin/python +# +# Copyright (C) 2013 Simo Sorce +# +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import imp +import cherrypy + +class Plugins(object): + + def __init__(self, path=None): + if path is None: + self._path = os.getcwd() + else: + self._path = path + self._providers_tree = None + + def _load_class(self, tree, class_type, file_name): + cherrypy.log.error('Check module %s for class %s' % (file_name, + class_type)) + name, ext = os.path.splitext(os.path.split(file_name)[-1]) + try: + if ext.lower() == '.py': + mod = imp.load_source(name, file_name) + elif ex.lower() == '.pyc': + mod = imp.load_compiled(name, file_name) + else: + return + except Exception, e: + cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e)) + return + + if hasattr(mod, class_type): + tree[name] = getattr(mod, class_type)() + cherrypy.log.error('Added module %s' % (name)) + + def _load_classes(self, tree, path, class_type): + files = None + try: + files = os.listdir(path) + except Exception, e: + cherrypy.log.error('No modules in %s: [%s]' % (path, e)) + return + + for name in files: + filename = od.path.join(path, name) + self._load_class(tree, class_type, filename) + + def get_providers(self): + if self._providers_tree is None: + path = None + if 'providers.dir' in cherrypy.config: + path = cherrypy.config['providers.dir'] + if not path: + path = os.path.join(self._path, 'providers') + + self._providers_tree = [] + self._load_classes(self._providers_tree, path, 'IdpProvider') + + return self._providers_tree + + def get_custom(self, class_type): + tree = [] + self._load_classes(tree, class_type) + return tree -- cgit