File size: 2,039 Bytes
08c964e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Hook __import__
import builtins
import os
import sys
import public
import public.PluginLoader as plugin_loader


if 'class_v2/' not in sys.path and 'class_v2' not in sys.path:
    sys.path.insert(0, 'class_v2/')


__hooked = False
old__import__ = builtins.__import__


def hook_import():
    global __hooked

    if __hooked:
        return

    def _aap__import__(name, globals = None, locals = None, fromlist = (), level = 0):
        try:
            return old__import__(name, globals, locals, fromlist, level)

        except SyntaxError:
            # searching module in project.
            if level == 0 and str(name).strip() != '':
                panel_path = public.get_panel_path()
                pyfile = '{}.py'.format(str(name).strip().replace('.', '/'))

                realpath = os.path.join(panel_path, 'class', pyfile)
                cond = os.path.exists(realpath)

                if not cond:
                    realpath = os.path.join(panel_path, 'class_v2', pyfile)
                    cond = os.path.exists(realpath)

                if not cond:
                    realpath = os.path.join(panel_path, pyfile)
                    cond = os.path.exists(realpath)

                if cond:
                    try:
                        # public.print_log('Load project module: {} {} {} {}'.format(name, level, realpath, fromlist))

                        m = plugin_loader.get_module(realpath)

                        if fromlist is None or len(fromlist) == 0:
                            return m

                        for prop_name in fromlist:
                            prop = getattr(m, prop_name)

                            if globals is not None:
                                globals[prop_name] = prop

                            if locals is not None:
                                locals[prop_name] = prop

                        return m

                    except:
                        raise

            raise

    builtins.__import__ = _aap__import__

    __hooked = True