Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Rpyc/ModuleNetProxy.py
blob: 6c205828036e7d1a4d09ca8aa9bcb0147dc28cad (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
from NetProxy import NetProxyWrapper, _get_conn, _get_oid


class ModuleNetProxy(NetProxyWrapper):
    """a netproxy specialzied for exposing remote modules (first tries to getattr,
    if it fails tries to import)"""
    __slots__ = ["____base__", "____cache__"]
    
    def __init__(self, proxy, base):
        NetProxyWrapper.__init__(self, proxy)
        object.__setattr__(self, "____base__", base)
        object.__setattr__(self, "____cache__", {})

    def __request__(self, handler, *args):
        return _get_conn(self).sync_request(handler, _get_oid(self), *args)

    def __getattr__(self, name):
        cache = object.__getattribute__(self, "____cache__")
        try:
            return cache[name]
        except KeyError:
            pass

        try:
            return self.__request__("handle_getattr", name)
        except AttributeError:
            pass
        
        try:
            fullname = object.__getattribute__(self, "____base__") + "." + name
            module = ModuleNetProxy(_get_conn(self).rimport(fullname), fullname)
            cache[name] = module
            return module
        except ImportError:
            raise AttributeError("'module' object has not attribute or submodule %r" % (name,))


class RootImporter(object):
    """the root of the interpreter's import hierarchy"""
    __slots__ = ["____conn__"]
    
    def __init__(self, conn):
        object.__setattr__(self, "____conn__", conn)
    
    def __getitem__(self, name):
        return _get_conn(self).rimport(name)
    
    def __getattr__(self, name):
        return ModuleNetProxy(self[name], name)
    
    def __setattr__(self, name, value):
        raise TypeError("read only type")