aboutsummaryrefslogtreecommitdiff
blob: 092418f0d1e3dde5dc565258acb8dafe760c650c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright 2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from __future__ import print_function

import os

from layman.output import Message

class InvalidModuleName(Exception):
    '''An invalid or unknown module name.'''

class Module(object):
    '''
    Class to define and hold our plugin-module

    @type name: string
    @param name: The module name
    @type namepath: string
    @param namepath: The path to the new module
    @param output: The output for any information
    '''
    def __init__(self, name, namepath, output):
        self.name = name
        self._namepath = namepath
        self.kids_names = []
        self.kids = {}
        self.output = output
        self.initialized = self._initialize()


    def _initialize(self):
        '''
        Initializes the plug-in module

        @rtype bool: reflects success or failure to initialize the module
        '''
        self.valid = False
        try:
            mod_name = '.'.join([self._namepath, self.name])
            self._module = __import__(mod_name, [], [], ['not empty'])
            self.valid = True
        except ImportError as e:
            self.output.error('Module._initialize(); failed to import %(mod) '\
                'error was: %(err)' % ({'err': e, 'mod': mod_name}))
            return False
        self.module_spec = self._module.module_spec
        for submodule in self.module_spec['provides']:
            kid = self.module_spec['provides'][submodule]
            kidname = kid['name']
            try:
                kid['module_name'] = '.'.join([mod_name, kid['sourcefile']])
            except KeyError:
                kid['module_name'] = '.'.join([mod_name, kidname])
                f = self._module.__file__
                msg = 'Module.__initialize(); %(module)s module spec is old, missing '\
                      'attribute: \'sourcefile\'.\nBackward compatibility '\
                      'may be removed in the future.\nFile: %(f)s' % {
                      'module': self.name, 'f': f}
                self.output.warn(msg)
            kid['is_imported'] = False
            self.kids[kidname] = kid
            self.kids_names.append(kidname)
        return True


    def get_class(self, name):
        '''
        Retrieves a module class desired

        @type name: string
        @param name: the plug-in module's name
        @rtype mod_class: instance of plug-in module's class
        '''
        if not name or name not in self.kids_names:
            raise InvalidModuleName('Module name "%(name)" was invalid or not'\
                    'part of the module "%(mod_name)"' ({'mod_name':self.name,
                                                         'name': name}))
        kid = self.kids[name]
        if kid['is_imported']:
            module = kid['instance']
        else:
            try:
                module = __import__(kid['module_name'], [], [], ["not empty"])
                kid['instance'] = module
                kid['is_imported'] = True
            except ImportError:
                raise
        mod_class = getattr(module, kid['class'])
        return mod_class


class Modules(object):
    '''
    Dynamic module system for loading and retrieving any of the
    installed layman modules and/or provided class'

    @param path: Optional path to the "modules" directory or defaults to
                 the directory of this file + "/modules"
    @param namepath: Optional python import path to the "modules" directory or
                     defaults to the directory name of this file + ".modules"
    @param output: Optional output, defaults to layman.output.Message object
    '''
    def __init__(self, path=None, namepath=None, output=None):
        if path:
            self._module_path = path
        else:
            self._module_path = os.path.join(
                (os.path.dirname(os.path.realpath(__file__))), 'modules')
        if namepath:
            self._namepath = namepath
        else:
            self._namepath = '.'.join(os.path.dirname(
                os.path.realpath(__file__)), 'modules')
        if output:
            self.output = output
        else:
            self.output = Message()
        self._modules = self._get_all_modules()
        self.module_names = sorted(self._modules)


    def _get_all_modules(self):
        '''
        Scans the overlay modules dir for loadable modules

        @rtype dict of module_plugins
        '''
        module_dir = self._module_path
        importables = []
        names = os.listdir(module_dir)
        for entry in names:
            if entry.startswith('__'):
                continue
            try:
                os.lstat(os.path.join(module_dir, entry, '__init__.py'))
                importables.append(entry)
            except EnvironmentError:
                pass

        kids = {}
        for entry in importables:
            new_module = Module(entry, self._namepath, self.output)
            for module_name in new_module.kids:
                kid = new_module.kids[module_name]
                kid['parent'] = new_module
                kids[kid['name']] = kid
        return kids


    def get_module_names(self):
        '''
        Retrieves all available module names

        @rtype: list of installed module names available
        '''
        return self.module_names


    def get_class(self, modname):
        '''
        Retrieves a module class desired

        @type modname: string
        @param modname: the module class name
        '''
        if modname and modname in self.module_names:
            mod = self._modules[modname]['parent'].get_class(modname)
        else:
            raise InvalidModuleName('Module name "%(name)s" was invalid or'\
                ' not found.' % ({'name': modname}))
        return mod


    def get_description(self, modname):
        '''
        Retrieves the module class decription

        @type modname: string
        @param modname: the module class name
        @rtype: string of modules class decription
        '''
        if modname and modname in self.module_names:
            mod = self._modules[modname]['description']
        else:
            raise InvalidModuleName('Module name "%(name)s" was invalid or'\
                ' not found.' % ({'name': modname}))
        return mod


    def get_functions(self, modname):
        '''
        Retrieves the module class exported function names

        @type modname: string
        @param modname: the module class name
        @rtype: list of the modules class exported function names
        '''
        if modname and modname in self.module_names:
            mod = self._modules[modname]['functions']
        else:
            raise InvalidModuleName('Module name "%(name)s" was invalid or'\
                ' not found.' % ({'name': modname}))
        return mod


    def get_func_descriptions(self, modname):
        '''
        Retrieves the module class  exported functions descriptions

        @type modname: string
        @param modname: the module class name
        @rtype: dict of the modules class exported functions descriptions
        '''
        if modname and modname in self.module_names:
            desc = self._modules[modname]['func_desc']
        else:
            raise InvalidModuleName('Module name "%(name)s" was invalid or'\
                ' not found.' % ({'name': modname}))
        return desc