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