Add getresps.
[doldaconnect.git] / lib / python / dolcon / __init__.py
... / ...
CommitLineData
1from dolmod import *
2import os
3
4def login(useauthless = True, **kw):
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 """
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
28def mustconnect(host, port = -1):
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 """
36 fd = connect(host, port)
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]
44 return fd
45
46def cnl(host = None, port = -1, useauthless = True, **kw):
47 """A convenience function for connect and loginasync.
48
49 This function will connect to the given server, or the server in
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.
53 """
54 if host is None:
55 host = os.getenv("DCSERVER")
56 if host is None:
57 host = "localhost"
58 fd = mustconnect(host, port)
59 err, reason = login(useauthless, **kw)
60 if err != "success":
61 raise RuntimeError, (err, reason)
62 return fd
63
64def ecmd(*args):
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 """
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
80def ecmda(code, *args):
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 """
87 resp = ecmd(*args)
88 if resp.getcode() != code:
89 raise ValueError, resp.getcode()
90 return resp
91
92def 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:
100 raise ValueError, tuple(resp.extract()[0])
101 return resp
102
103def getresps():
104 """A generator function which will iterate over all responses from
105 getresp.
106 """
107 while True:
108 resp = getresp()
109 if resp is None:
110 break
111 else:
112 yield resp