3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class Plugins(object):
26 def __init__(self, path=None):
28 self._path = os.getcwd()
31 self._providers_tree = None
33 def _load_class(self, tree, class_type, file_name):
34 cherrypy.log.error('Check module %s for class %s' % (file_name,
36 name, ext = os.path.splitext(os.path.split(file_name)[-1])
38 if ext.lower() == '.py':
39 mod = imp.load_source(name, file_name)
40 elif ex.lower() == '.pyc':
41 mod = imp.load_compiled(name, file_name)
45 cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
48 if hasattr(mod, class_type):
49 tree[name] = getattr(mod, class_type)()
50 cherrypy.log.error('Added module %s' % (name))
52 def _load_classes(self, tree, path, class_type):
55 files = os.listdir(path)
57 cherrypy.log.error('No modules in %s: [%s]' % (path, e))
61 filename = od.path.join(path, name)
62 self._load_class(tree, class_type, filename)
64 def get_providers(self):
65 if self._providers_tree is None:
67 if 'providers.dir' in cherrypy.config:
68 path = cherrypy.config['providers.dir']
70 path = os.path.join(self._path, 'providers')
72 self._providers_tree = []
73 self._load_classes(self._providers_tree, path, 'IdpProvider')
75 return self._providers_tree
77 def get_custom(self, class_type):
79 self._load_classes(tree, class_type)