clearer names for actions, and infer actions better
[monitor.git] / monitor / Rpyc / Lib.py
1 """\r
2 shared types, functions and constants\r
3 """\r
4 import sys\r
5 \r
6 \r
7 DEFAULT_PORT = 18812\r
8 \r
9 def raise_exception(typ, val, tbtext):\r
10     """a helper for raising remote exceptions"""\r
11     if type(typ) == str:\r
12         raise typ\r
13     else:\r
14         val._remote_traceback = tbtext\r
15         raise val\r
16 \r
17 class ImmDict(object):\r
18     """immutable dict (passes by value)"""\r
19     def __init__(self, dict):\r
20         self.dict = dict\r
21     def items(self):\r
22         return self.dict.items()\r
23 \r
24 class AttrFrontend(object):\r
25     """a wrapper that implements the attribute protocol for a dict backend"""\r
26     def __init__(self, dict):\r
27         self.__dict__["____dict"] = dict\r
28     def __delattr__(self, name):\r
29         del self.__dict__["____dict"][name]\r
30     def __getattr__(self, name):\r
31         return self.__dict__["____dict"][name]\r
32     def __setattr__(self, name, value):\r
33         self.__dict__["____dict"][name] = value\r
34     def __repr__(self):\r
35         return "<AttrFrontend %s>" % (self.__dict__["____dict"].keys(),)\r
36 \r
37 # installs an rpyc-enabled exception hook. this happens automatically when the module\r
38 # is imported. also, make sure the current excepthook is the original one, so we dont \r
39 # install our hook twice (and thus cause infinite recursion) in case the module is reloaded \r
40 def rpyc_excepthook(exctype, value, traceback):\r
41     if hasattr(value, "_remote_traceback"):\r
42         print >> sys.stderr, "======= Remote traceback ======="\r
43         print >> sys.stderr, value._remote_traceback\r
44         print >> sys.stderr, "======= Local exception ======="\r
45         orig_excepthook(exctype, value, traceback)\r
46     else:\r
47         orig_excepthook(exctype, value, traceback)\r
48 \r
49 if sys.excepthook.__name__ != "rpyc_excepthook": \r
50     orig_excepthook = sys.excepthook\r
51     sys.excepthook = rpyc_excepthook\r
52 \r
53 \r