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