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