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/>.
25 class Plugins(object):
27 def __init__(self, path=None):
29 self._path = os.getcwd()
32 self._providers_tree = None
34 def _load_class(self, tree, class_type, file_name):
35 cherrypy.log.error('Check module %s for class %s' % (file_name,
37 name, ext = os.path.splitext(os.path.split(file_name)[-1])
39 if ext.lower() == '.py':
40 mod = imp.load_source(name, file_name)
41 elif ext.lower() == '.pyc':
42 mod = imp.load_compiled(name, file_name)
45 except Exception, e: # pylint: disable=broad-except
46 cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
49 if hasattr(mod, class_type):
50 tree[name] = getattr(mod, class_type)()
51 cherrypy.log.error('Added module %s' % (name))
53 def _load_classes(self, tree, path, class_type):
56 files = os.listdir(path)
57 except Exception, e: # pylint: disable=broad-except
58 cherrypy.log.error('No modules in %s: [%s]' % (path, e))
62 filename = os.path.join(path, name)
63 self._load_class(tree, class_type, filename)
65 def get_providers(self):
66 if self._providers_tree is None:
68 if 'providers.dir' in cherrypy.config:
69 path = cherrypy.config['providers.dir']
71 path = os.path.join(self._path, 'providers')
73 self._providers_tree = []
74 self._load_classes(self._providers_tree, path, 'IdpProvider')
76 return self._providers_tree
78 def get_custom(self, path, class_type):
80 self._load_classes(tree, path, class_type)