Add Info providers Admin pages
[cascardo/ipsilon.git] / ipsilon / info / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon Project Contributors
4 #
5 # See the file named COPYING for the project license
6
7 from ipsilon.util.log import Log
8 from ipsilon.util.plugin import PluginLoader, PluginObject
9 from ipsilon.util.plugin import PluginInstaller
10
11
12 class InfoProviderBase(PluginObject, Log):
13
14     def __init__(self):
15         super(InfoProviderBase, self).__init__()
16
17     def get_user_attrs(self, user):
18         raise NotImplementedError
19
20     def enable(self, site):
21         plugins = site[FACILITY]
22         if self in plugins['enabled']:
23             return
24
25         # configure self
26         if self.name in plugins['config']:
27             self.set_config(plugins['config'][self.name])
28
29         plugins['enabled'].append(self)
30         self.debug('Info plugin enabled: %s' % self.name)
31
32     def disable(self, site):
33         plugins = site[FACILITY]
34         if self not in plugins['enabled']:
35             return
36
37         plugins['enabled'].remove(self)
38         self.debug('Info plugin disabled: %s' % self.name)
39
40
41 FACILITY = 'info_config'
42
43
44 class Info(Log):
45
46     def __init__(self, site):
47         self._site = site
48         self.providers = []
49
50         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
51         self._site[FACILITY] = loader.get_plugin_data()
52         plugins = self._site[FACILITY]
53
54         available = plugins['available'].keys()
55         self.debug('Available info providers: %s' % str(available))
56
57         for item in plugins['whitelist']:
58             self.debug('Login plugin in whitelist: %s' % item)
59             if item not in plugins['available']:
60                 self.debug('Info Plugin %s not found' % item)
61                 continue
62             self.providers.append((item, plugins['available'][item]))
63             self.debug('Added Info plugin: %s' % item)
64
65     def get_user_attrs(self, user, provider=None):
66         if provider:
67             for p in self.providers:
68                 if p[0] == provider:
69                     return p[1].get_user_attrs(user)
70         else:
71             for p in self.providers:
72                 ret = p[1].get_user_attrs(user)
73                 if ret:
74                     return ret
75
76         return None
77
78
79 class InfoProviderInstaller(object):
80
81     def __init__(self):
82         self.facility = FACILITY
83         self.ptype = 'info'
84         self.name = None
85
86     def install_args(self, group):
87         raise NotImplementedError
88
89     def configure(self, opts):
90         raise NotImplementedError
91
92
93 class InfoProviderInstall(object):
94
95     def __init__(self):
96         pi = PluginInstaller(InfoProviderInstall)
97         self.plugins = pi.get_plugins()