917f28bce5e9532edc9681144ad82451f5965c4a
[cascardo/ipsilon.git] / ipsilon / util / plugin.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 import os
21 import imp
22 import cherrypy
23
24
25 class Plugins(object):
26
27     def __init__(self, path=None):
28         if path is None:
29             self._path = os.getcwd()
30         else:
31             self._path = path
32         self._providers_tree = None
33
34     def _load_class(self, tree, class_type, file_name):
35         cherrypy.log.error('Check module %s for class %s' % (file_name,
36                                                              class_type))
37         name, ext = os.path.splitext(os.path.split(file_name)[-1])
38         try:
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)
43             else:
44                 return
45         except Exception, e:  # pylint: disable=broad-except
46             cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
47             return
48
49         if hasattr(mod, class_type):
50             tree[name] = getattr(mod, class_type)()
51             cherrypy.log.error('Added module %s' % (name))
52
53     def _load_classes(self, tree, path, class_type):
54         files = None
55         try:
56             files = os.listdir(path)
57         except Exception, e:  # pylint: disable=broad-except
58             cherrypy.log.error('No modules in %s: [%s]' % (path, e))
59             return
60
61         for name in files:
62             filename = os.path.join(path, name)
63             self._load_class(tree, class_type, filename)
64
65     def get_providers(self):
66         if self._providers_tree is None:
67             path = None
68             if 'providers.dir' in cherrypy.config:
69                 path = cherrypy.config['providers.dir']
70             if not path:
71                 path = os.path.join(self._path, 'providers')
72
73             self._providers_tree = []
74             self._load_classes(self._providers_tree, path, 'IdpProvider')
75
76         return self._providers_tree
77
78     def get_custom(self, path, class_type):
79         tree = []
80         self._load_classes(tree, path, class_type)
81         return tree