Use pylint check
[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 class Plugins(object):
25
26     def __init__(self, path=None):
27         if path is None:
28             self._path = os.getcwd()
29         else:
30             self._path = path
31         self._providers_tree = None
32
33     def _load_class(self, tree, class_type, file_name):
34         cherrypy.log.error('Check module %s for class %s' % (file_name,
35                                                              class_type))
36         name, ext = os.path.splitext(os.path.split(file_name)[-1])
37         try:
38             if ext.lower() == '.py':
39                 mod = imp.load_source(name, file_name)
40             elif ext.lower() == '.pyc':
41                 mod = imp.load_compiled(name, file_name)
42             else:
43                 return
44         except Exception, e:  # pylint: disable=broad-except
45             cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
46             return
47
48         if hasattr(mod, class_type):
49             tree[name] = getattr(mod, class_type)()
50             cherrypy.log.error('Added module %s' % (name))
51
52     def _load_classes(self, tree, path, class_type):
53         files = None
54         try:
55             files = os.listdir(path)
56         except Exception, e:  # pylint: disable=broad-except
57             cherrypy.log.error('No modules in %s: [%s]' % (path, e))
58             return
59
60         for name in files:
61             filename = os.path.join(path, name)
62             self._load_class(tree, class_type, filename)
63
64     def get_providers(self):
65         if self._providers_tree is None:
66             path = None
67             if 'providers.dir' in cherrypy.config:
68                 path = cherrypy.config['providers.dir']
69             if not path:
70                 path = os.path.join(self._path, 'providers')
71
72             self._providers_tree = []
73             self._load_classes(self._providers_tree, path, 'IdpProvider')
74
75         return self._providers_tree
76
77     def get_custom(self, path, class_type):
78         tree = []
79         self._load_classes(tree, path, class_type)
80         return tree