Refactor plugin configuration
[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 PluginInstaller, PluginLoader
9 from ipsilon.util.plugin import PluginObject, PluginConfig
10
11
12 class InfoProviderBase(PluginConfig, PluginObject):
13
14     def __init__(self):
15         PluginConfig.__init__(self)
16         PluginObject.__init__(self)
17         self._site = None
18         self.is_enabled = False
19
20     def get_user_attrs(self, user):
21         raise NotImplementedError
22
23     def enable(self, site):
24         if self.is_enabled:
25             return
26
27         if not self._site:
28             self._site = site
29         plugins = self._site[FACILITY]
30
31         # configure self
32         if self.name in plugins['config']:
33             self.import_config(plugins['config'][self.name])
34
35         plugins['enabled'].append(self)
36         self.is_enabled = True
37         self.debug('Info plugin enabled: %s' % self.name)
38
39     def disable(self, site):
40         if not self.is_enabled:
41             return
42
43         plugins = self._site[FACILITY]
44         plugins['enabled'].remove(self)
45         self.is_enabled = False
46         self.debug('Info plugin disabled: %s' % self.name)
47
48
49 class InfoMapping(Log):
50
51     def __init__(self):
52         self.standard_attributes = {
53             'fullname': 'Full Name',
54             'nickname': 'Nickname',
55             'surname': 'Last Name',
56             'firstname': 'First Name',
57             'title': 'Title',
58             'dob': 'Date of Birth',
59             'email': 'E-mail Address',
60             'gender': 'Gender',
61             'postcode': 'Postal Code',
62             'street': 'Street Address',
63             'state': 'State or Province',
64             'country': 'Country',
65             'phone': 'Telephone Number',
66             'language': 'Language',
67             'timezone': 'Time Zone',
68         }
69         self.mapping = dict()
70
71     def set_mapping(self, attrs_map):
72         self.mapping = attrs_map
73
74     def display_name(self, name):
75         if name in self.standard_attributes:
76             return self.standard_attributes[name]
77         return name
78
79     def map_attrs(self, attrs):
80         s = dict()
81         e = dict()
82         for a in attrs:
83             if a in self.mapping:
84                 s[self.mapping[a]] = attrs[a]
85             else:
86                 e[a] = attrs[a]
87
88         return s, e
89
90
91 FACILITY = 'info_config'
92
93
94 class Info(Log):
95
96     def __init__(self, site):
97         self._site = site
98
99         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
100         self._site[FACILITY] = loader.get_plugin_data()
101         plugins = self._site[FACILITY]
102
103         available = plugins['available'].keys()
104         self.debug('Available info providers: %s' % str(available))
105
106         plugins['root'] = self
107         for item in plugins['whitelist']:
108             self.debug('Login plugin in whitelist: %s' % item)
109             if item not in plugins['available']:
110                 self.debug('Info Plugin %s not found' % item)
111                 continue
112             plugins['available'][item].enable(self._site)
113
114     def get_user_attrs(self, user, requested=None):
115         plugins = self._site[FACILITY]['available']
116         result = dict()
117
118         for _, p in plugins.items():
119             if requested is None:
120                 if not p.is_enabled:
121                     continue
122             else:
123                 if requested != p.name:
124                     continue
125             result = p.get_user_attrs(user)
126             if result:
127                 break
128
129         return result
130
131
132 class InfoProviderInstaller(object):
133
134     def __init__(self):
135         self.facility = FACILITY
136         self.ptype = 'info'
137         self.name = None
138
139     def install_args(self, group):
140         raise NotImplementedError
141
142     def configure(self, opts):
143         raise NotImplementedError
144
145
146 class InfoProviderInstall(object):
147
148     def __init__(self):
149         pi = PluginInstaller(InfoProviderInstall)
150         self.plugins = pi.get_plugins()