clearer names for actions, and infer actions better
[monitor.git] / monitor / Rpyc / Factories.py
1 """\r
2 the factory: \r
3 exposes a nice and easy interface to the internals of rpyc. \r
4 this module, along with Utils, are the only modules most clients will need.\r
5 """\r
6 \r
7 from Stream import SocketStream, PipeStream\r
8 from Channel import Channel\r
9 from Connection import Connection\r
10 from AsyncNetProxy import AsyncNetProxy\r
11 from weakref import WeakValueDictionary\r
12 from Lib import DEFAULT_PORT\r
13 \r
14 \r
15 __all__ = ["SocketConnection", "AuthSocketConnection", "PipeConnection", "Async"]\r
16 _async_proxy_cache = WeakValueDictionary()\r
17 \r
18 class LoginError(Exception):\r
19     pass\r
20 \r
21 def SocketConnection(host, port = DEFAULT_PORT, **kw):\r
22     """shorthand for creating a conneciton over a socket to a server"""\r
23     return Connection(Channel(SocketStream.from_new_socket(host, port, **kw)))\r
24 \r
25 def _create_auth_connection(chan, username, password):\r
26     from Authentication import login\r
27     if not login(chan, username, password):\r
28         raise LoginError("the server did not accept the login")\r
29     return Connection(chan)\r
30     \r
31 def AuthSocketConnection(host, username, password, port = DEFAULT_PORT, **kw):\r
32     """shorthand for creating a conneciton over a socket to a server, with authentication"""\r
33     chan = Channel(SocketStream.from_new_socket(host, port, **kw))\r
34     return _create_auth_connection(chan, username, password)\r
35 \r
36 def PipeConnection(incoming, outgoing):\r
37     """shorthand for creating a conneciton over a pipe"""\r
38     return Connection(Channel(PipeStream(incoming, outgoing)))\r
39 \r
40 def AuthPipeConnection(incoming, outgoing, username, password):\r
41     """shorthand for creating a conneciton over a pipe"""\r
42     chan = Channel(PipeStream(incoming, outgoing))\r
43     return _create_auth_connection(chan, username, password)\r
44 \r
45 def Async(proxy):\r
46     """a factory for creating asynchronous proxies for existing synchronous ones"""\r
47     key = id(proxy)\r
48     if key in _async_proxy_cache:\r
49         return _async_proxy_cache[key]\r
50     else:\r
51         new_proxy = AsyncNetProxy(proxy)\r
52         _async_proxy_cache[key] = new_proxy\r
53         return new_proxy\r
54 \r
55 \r
56 \r
57 \r