Rename and move PluginConfig to ConfigHelper
[cascardo/ipsilon.git] / ipsilon / info / common.py
1 # Copyright (C) 2014 Ipsilon Project Contributors
2 #
3 # See the file named COPYING for the project license
4
5 from ipsilon.util.log import Log
6 from ipsilon.util.plugin import PluginInstaller, PluginLoader
7 from ipsilon.util.plugin import PluginObject
8 from ipsilon.util.config import ConfigHelper
9
10
11 class InfoProviderBase(ConfigHelper, PluginObject):
12
13     def __init__(self, *pargs):
14         ConfigHelper.__init__(self)
15         PluginObject.__init__(self, *pargs)
16
17     def get_user_attrs(self, user):
18         raise NotImplementedError
19
20
21 class InfoMapping(Log):
22
23     def __init__(self):
24         self.standard_attributes = {
25             'fullname': 'Full Name',
26             'nickname': 'Nickname',
27             'surname': 'Last Name',
28             'firstname': 'First Name',
29             'title': 'Title',
30             'dob': 'Date of Birth',
31             'email': 'E-mail Address',
32             'gender': 'Gender',
33             'postcode': 'Postal Code',
34             'street': 'Street Address',
35             'state': 'State or Province',
36             'country': 'Country',
37             'phone': 'Telephone Number',
38             'language': 'Language',
39             'timezone': 'Time Zone',
40         }
41
42     def display_name(self, name):
43         if name in self.standard_attributes:
44             return self.standard_attributes[name]
45         return name
46
47
48 FACILITY = 'info_config'
49
50
51 class Info(Log):
52
53     def __init__(self, site):
54         self._site = site
55
56         plugins = PluginLoader(Info, FACILITY, 'InfoProvider')
57         plugins.get_plugin_data()
58         self._site[FACILITY] = plugins
59
60         available = plugins.available.keys()
61         self.debug('Available info providers: %s' % str(available))
62
63         for item in plugins.enabled:
64             self.debug('Info plugin in enabled list: %s' % item)
65             if item not in plugins.available:
66                 self.debug('Info Plugin %s not found' % item)
67                 continue
68             plugins.available[item].enable()
69
70     def get_user_attrs(self, user, requested=None):
71         plugins = self._site[FACILITY].available
72         result = dict()
73
74         for _, p in plugins.items():
75             if requested is None:
76                 if not p.is_enabled:
77                     continue
78             else:
79                 if requested != p.name:
80                     continue
81             result = p.get_user_attrs(user)
82             if result:
83                 break
84
85         return result
86
87
88 class InfoProviderInstaller(object):
89
90     def __init__(self):
91         self.facility = FACILITY
92         self.ptype = 'info'
93         self.name = None
94
95     def install_args(self, group):
96         raise NotImplementedError
97
98     def validate_args(self, args):
99         return
100
101     def unconfigure(self, opts):
102         return
103
104     def configure(self, opts):
105         raise NotImplementedError
106
107
108 class InfoProviderInstall(object):
109
110     def __init__(self):
111         pi = PluginInstaller(InfoProviderInstall, FACILITY)
112         self.plugins = pi.get_plugins()