pull in additional changes from 2.0 branch.
[monitor.git] / monitor / Rpyc / ModuleNetProxy.py
diff --git a/monitor/Rpyc/ModuleNetProxy.py b/monitor/Rpyc/ModuleNetProxy.py
new file mode 100644 (file)
index 0000000..3445088
--- /dev/null
@@ -0,0 +1,46 @@
+from NetProxy import NetProxyWrapper\r
+\r
+\r
+class ModuleNetProxy(NetProxyWrapper):\r
+    """a netproxy specialzied for exposing remote modules (first tries to getattr,\r
+    if it fails tries to import)"""\r
+    __doc__ = NetProxyWrapper.__doc__\r
+    \r
+    def __init__(self, proxy, base):\r
+        NetProxyWrapper.__init__(self, proxy)\r
+        self.__dict__["____base"] = base\r
+        self.__dict__["____cache"] = {}\r
+\r
+    def __request__(self, handler, *args):\r
+        return self.__dict__["____conn"].sync_request(handler, self.__dict__["____oid"], *args)\r
+\r
+    def __getattr__(self, name):\r
+        if name in self.__dict__["____cache"]:\r
+            return self.__dict__["____cache"][name]\r
+\r
+        try:\r
+            return self.__request__("handle_getattr", name)\r
+        except AttributeError:\r
+            pass\r
+        \r
+        try:\r
+            fullname = self.__dict__["____base"] + "." + name\r
+            obj = self.__dict__["____conn"].rimport(fullname)\r
+            module = ModuleNetProxy(obj, fullname)\r
+            self.__dict__["____cache"][name] = module\r
+            return module\r
+        except ImportError:\r
+            raise AttributeError("'module' object has not attribute or submodule %r" % (name,))\r
+\r
+class RootImporter(object):\r
+    """the root of the interpreter's import hierarchy"""\r
+    \r
+    def __init__(self, conn):\r
+        self.__dict__["____conn"] = conn\r
+    \r
+    def __getitem__(self, name):\r
+        return self.__dict__["____conn"].rimport(name)\r
+\r
+    def __getattr__(self, name):\r
+        return ModuleNetProxy(self[name], name)\r
+\r