Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / commissioning / importing.py @ 6695edc9

History | View | Annotate | Download (2.6 kB)

1 54069d1b Stavros Sachtouris
# Copyright 2012 GRNET S.A. All rights reserved.
2 54069d1b Stavros Sachtouris
#
3 54069d1b Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 54069d1b Stavros Sachtouris
# without modification, are permitted provided that the following
5 54069d1b Stavros Sachtouris
# conditions are met:
6 54069d1b Stavros Sachtouris
#
7 54069d1b Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 54069d1b Stavros Sachtouris
#      disclaimer.
10 54069d1b Stavros Sachtouris
#
11 54069d1b Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 54069d1b Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 54069d1b Stavros Sachtouris
#      provided with the distribution.
15 54069d1b Stavros Sachtouris
#
16 54069d1b Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 54069d1b Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 54069d1b Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 54069d1b Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 54069d1b Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 54069d1b Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 54069d1b Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 54069d1b Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 54069d1b Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 54069d1b Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 54069d1b Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 54069d1b Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 54069d1b Stavros Sachtouris
#
29 54069d1b Stavros Sachtouris
# The views and conclusions contained in the software and
30 54069d1b Stavros Sachtouris
# documentation are those of the authors and should not be
31 54069d1b Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 54069d1b Stavros Sachtouris
# or implied, of GRNET S.A.
33 54069d1b Stavros Sachtouris
34 54069d1b Stavros Sachtouris
from imp import find_module, load_module
35 54069d1b Stavros Sachtouris
36 54069d1b Stavros Sachtouris
_modules = {}
37 54069d1b Stavros Sachtouris
38 6764f588 Stavros Sachtouris
39 54069d1b Stavros Sachtouris
def imp_module(fullname):
40 54069d1b Stavros Sachtouris
    if fullname in _modules:
41 54069d1b Stavros Sachtouris
        return _modules[fullname]
42 54069d1b Stavros Sachtouris
43 54069d1b Stavros Sachtouris
    components = fullname.split('.')
44 54069d1b Stavros Sachtouris
    if not components:
45 54069d1b Stavros Sachtouris
        raise ValueError('invalid module name')
46 54069d1b Stavros Sachtouris
47 54069d1b Stavros Sachtouris
    module = None
48 54069d1b Stavros Sachtouris
    modulepath = []
49 54069d1b Stavros Sachtouris
50 54069d1b Stavros Sachtouris
    for name in components:
51 54069d1b Stavros Sachtouris
        if not name:
52 54069d1b Stavros Sachtouris
            raise ValueError("Relative paths not allowed")
53 54069d1b Stavros Sachtouris
54 54069d1b Stavros Sachtouris
        modulepath.append(name)
55 54069d1b Stavros Sachtouris
        modulename = '.'.join(modulepath)
56 54069d1b Stavros Sachtouris
        if modulename in _modules:
57 54069d1b Stavros Sachtouris
            module = _modules[modulename]
58 54069d1b Stavros Sachtouris
59 54069d1b Stavros Sachtouris
        elif hasattr(module, name):
60 54069d1b Stavros Sachtouris
            module = getattr(module, name)
61 54069d1b Stavros Sachtouris
62 54069d1b Stavros Sachtouris
        elif not hasattr(module, '__path__'):
63 54069d1b Stavros Sachtouris
            m = find_module(name)
64 54069d1b Stavros Sachtouris
            module = load_module(modulename, *m)
65 54069d1b Stavros Sachtouris
66 54069d1b Stavros Sachtouris
        else:
67 54069d1b Stavros Sachtouris
            try:
68 54069d1b Stavros Sachtouris
                m = find_module(name, module.__path__)
69 54069d1b Stavros Sachtouris
                module = load_module(modulename, *m)
70 54069d1b Stavros Sachtouris
            except ImportError:
71 54069d1b Stavros Sachtouris
                m = "No module '%s' in '%s'" % (name, module.__path__)
72 54069d1b Stavros Sachtouris
                raise ImportError(m)
73 54069d1b Stavros Sachtouris
74 54069d1b Stavros Sachtouris
        _modules[modulename] = module
75 54069d1b Stavros Sachtouris
76 54069d1b Stavros Sachtouris
    return module
77 54069d1b Stavros Sachtouris
78 54069d1b Stavros Sachtouris
79 54069d1b Stavros Sachtouris
def list_modules():
80 54069d1b Stavros Sachtouris
    return sorted(_modules.keys())