c4be8fe8b3e012b23d1afe3474caa060ec4c3db5
[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         self.enabled = False
17
18     def get_user_attrs(self, user):
19         raise NotImplementedError
20
21     @property
22     def is_enabled(self):
23         return self.enabled
24
25     def enable(self, site):
26         self.enabled = True
27
28         plugins = site[FACILITY]
29         if self in plugins['enabled']:
30             return
31
32         # configure self
33         if self.name in plugins['config']:
34             self.set_config(plugins['config'][self.name])
35
36         plugins['enabled'].append(self)
37         self.debug('Info plugin enabled: %s' % self.name)
38
39     def disable(self, site):
40         self.enabled = False
41
42         plugins = site[FACILITY]
43         if self not in plugins['enabled']:
44             return
45
46         plugins['enabled'].remove(self)
47         self.debug('Info plugin disabled: %s' % self.name)
48
49
50 FACILITY = 'info_config'
51
52
53 class Info(Log):
54
55     def __init__(self, site):
56         self._site = site
57
58         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
59         self._site[FACILITY] = loader.get_plugin_data()
60         plugins = self._site[FACILITY]
61
62         available = plugins['available'].keys()
63         self.debug('Available info providers: %s' % str(available))
64
65         plugins['root'] = self
66         for item in plugins['whitelist']:
67             self.debug('Login plugin in whitelist: %s' % item)
68             if item not in plugins['available']:
69                 self.debug('Info Plugin %s not found' % item)
70                 continue
71             plugins['available'][item].enable(self._site)
72
73     def get_user_attrs(self, user, requested=None):
74         plugins = self._site[FACILITY]['available']
75         result = dict()
76
77         for _, p in plugins.items():
78             if requested is None:
79                 if not p.is_enabled:
80                     continue
81             else:
82                 if requested != p.name:
83                     continue
84             result = p.get_user_attrs(user)
85             if result:
86                 break
87
88         return result
89
90
91 class InfoProviderInstaller(object):
92
93     def __init__(self):
94         self.facility = FACILITY
95         self.ptype = 'info'
96         self.name = None
97
98     def install_args(self, group):
99         raise NotImplementedError
100
101     def configure(self, opts):
102         raise NotImplementedError
103
104
105 class InfoProviderInstall(object):
106
107     def __init__(self):
108         pi = PluginInstaller(InfoProviderInstall)
109         self.plugins = pi.get_plugins()