__init__ needs to be in the main package
[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
41     def display_name(self, name):
42         if name in self.standard_attributes:
43             return self.standard_attributes[name]
44         return name
45
46
47 FACILITY = 'info_config'
48
49
50 class Info(Log):
51
52     def __init__(self, site):
53         self._site = site
54
55         plugins = PluginLoader(Info, FACILITY, 'InfoProvider')
56         plugins.get_plugin_data()
57         self._site[FACILITY] = plugins
58
59         available = plugins.available.keys()
60         self.debug('Available info providers: %s' % str(available))
61
62         for item in plugins.enabled:
63             self.debug('Info plugin in enabled list: %s' % item)
64             if item not in plugins.available:
65                 self.debug('Info Plugin %s not found' % item)
66                 continue
67             plugins.available[item].enable()
68
69     def get_user_attrs(self, user, requested=None):
70         plugins = self._site[FACILITY].available
71         result = dict()
72
73         for _, p in plugins.items():
74             if requested is None:
75                 if not p.is_enabled:
76                     continue
77             else:
78                 if requested != p.name:
79                     continue
80             result = p.get_user_attrs(user)
81             if result:
82                 break
83
84         return result
85
86
87 class InfoProviderInstaller(object):
88
89     def __init__(self):
90         self.facility = FACILITY
91         self.ptype = 'info'
92         self.name = None
93
94     def install_args(self, group):
95         raise NotImplementedError
96
97     def configure(self, opts):
98         raise NotImplementedError
99
100
101 class InfoProviderInstall(object):
102
103     def __init__(self):
104         pi = PluginInstaller(InfoProviderInstall, FACILITY)
105         self.plugins = pi.get_plugins()