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