fbe30a6d |
1 | from dolmod import * |
c5de778c |
2 | import os |
fbe30a6d |
3 | |
4 | def login(useauthless = True, **kw): |
ff076c0a |
5 | """A convenience function for loginasync. |
6 | |
7 | This function will initiate an asynchronous login per the |
8 | loginasync command, and then run a select loop while waiting for |
9 | it to complete. It will return a tuple (res, reason), where res is |
10 | the result code, and reason is an explanatory text for any error. |
11 | |
12 | res can be any of the following: |
13 | * success: Login completed successfully |
14 | * nologin: No authentication mechanism could be negotiated |
15 | * server: An error occurred on the server |
16 | * user: An error occurred in the library |
17 | * conv: The password conversation mechanism failed |
18 | * authfail: The server refused the login (due to e.g. bad credentials) |
19 | """ |
fbe30a6d |
20 | result = [None] |
21 | def mycb(*v): |
22 | result[0] = v |
23 | loginasync(mycb, useauthless, **kw) |
24 | while result[0] is None: |
25 | select() |
26 | return result[0] |
27 | |
28 | def mustconnect(host, port = -1): |
ff076c0a |
29 | """A convenience function for connect. |
30 | |
31 | This function will connect to the given host, perform a select |
32 | loop, and ensure that the server approves of the connection. If |
33 | any of these steps fail, an exception is raised. If successful, |
34 | the file descriptor for the server connection is returned. |
35 | """ |
00ea2039 |
36 | fd = connect(host, port) |
fbe30a6d |
37 | while True: |
38 | resp = getresp() |
39 | if resp is not None and resp.getcmd() == u".connect": |
40 | break |
41 | select() |
42 | if resp.getcode() != 200: |
43 | raise RuntimeError, resp.intresp()[0][0] |
00ea2039 |
44 | return fd |
fbe30a6d |
45 | |
c5de778c |
46 | def cnl(host = None, port = -1, useauthless = True, **kw): |
ff076c0a |
47 | """A convenience function for connect and loginasync. |
48 | |
49 | This function will connect to the given server, or the server in |
17537706 |
50 | the environment variable $DCSERVER if none is given, or, if that |
51 | fails, localhost, and authenticate to the server. If any of the |
52 | steps fail, an exception is raised. |
ff076c0a |
53 | """ |
c5de778c |
54 | if host is None: |
55 | host = os.getenv("DCSERVER") |
56 | if host is None: |
17537706 |
57 | host = "localhost" |
00ea2039 |
58 | fd = mustconnect(host, port) |
fbe30a6d |
59 | err, reason = login(useauthless, **kw) |
60 | if err != "success": |
61 | raise RuntimeError, (err, reason) |
00ea2039 |
62 | return fd |
fbe30a6d |
63 | |
64 | def ecmd(*args): |
ff076c0a |
65 | """A convenience function for qcmd. |
66 | |
67 | This function will queue the given command, and then wait in a |
68 | select loop until the command has been carried out. The return |
69 | value is a Response object, corresponding to the reponse from the |
70 | server. |
71 | """ |
fbe30a6d |
72 | tag = qcmd(*args) |
73 | while True: |
74 | resp = getresp(tag) |
75 | if resp is not None: |
76 | break; |
77 | select() |
78 | return resp |
79 | |
80 | def ecmda(code, *args): |
ff076c0a |
81 | """A convenience function for ecmd. |
82 | |
83 | This function does essentially the same as ecmd, but it will also |
84 | check so that the response has the given numerical code. If not, |
85 | an exception is raised. |
86 | """ |
fbe30a6d |
87 | resp = ecmd(*args) |
88 | if resp.getcode() != code: |
89 | raise ValueError, resp.getcode() |
90 | return resp |
194d48ea |
91 | |
92 | def ecmds(*args): |
93 | """Another convenience function for ecmd. |
94 | |
95 | Like ecmda, but will fail on all 5xx codes, and succeed on all |
96 | others. |
97 | """ |
98 | resp = ecmd(*args) |
99 | if resp.getcode() >= 500 and resp.getcode() < 600: |
f04b2c3c |
100 | raise ValueError, tuple(resp.extract()[0]) |
194d48ea |
101 | return resp |