Support version connect stanza in extension languages.
[doldaconnect.git] / lib / python / dolcon / __init__.py
CommitLineData
fbe30a6d 1from dolmod import *
c5de778c 2import os
fbe30a6d 3
4def 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
9cbeb60c 28def mustconnect(host, revision = latest):
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 """
9cbeb60c 36 fd = connect(host)
fbe30a6d 37 while True:
38 resp = getresp()
39 if resp is not None and resp.getcmd() == u".connect":
40 break
41 select()
9cbeb60c 42 if resp.getcode() != 201:
fbe30a6d 43 raise RuntimeError, resp.intresp()[0][0]
9cbeb60c 44 if not checkproto(resp, revision):
45 raise RuntimeError, resp
00ea2039 46 return fd
fbe30a6d 47
9cbeb60c 48def cnl(host = None, useauthless = True, revision = latest, **kw):
ff076c0a 49 """A convenience function for connect and loginasync.
50
51 This function will connect to the given server, or the server in
17537706 52 the environment variable $DCSERVER if none is given, or, if that
53 fails, localhost, and authenticate to the server. If any of the
54 steps fail, an exception is raised.
ff076c0a 55 """
c5de778c 56 if host is None:
57 host = os.getenv("DCSERVER")
58 if host is None:
17537706 59 host = "localhost"
9cbeb60c 60 fd = mustconnect(host, revision)
fbe30a6d 61 err, reason = login(useauthless, **kw)
62 if err != "success":
63 raise RuntimeError, (err, reason)
00ea2039 64 return fd
fbe30a6d 65
66def ecmd(*args):
ff076c0a 67 """A convenience function for qcmd.
68
69 This function will queue the given command, and then wait in a
70 select loop until the command has been carried out. The return
71 value is a Response object, corresponding to the reponse from the
72 server.
73 """
fbe30a6d 74 tag = qcmd(*args)
75 while True:
76 resp = getresp(tag)
77 if resp is not None:
78 break;
79 select()
80 return resp
81
82def ecmda(code, *args):
ff076c0a 83 """A convenience function for ecmd.
84
85 This function does essentially the same as ecmd, but it will also
86 check so that the response has the given numerical code. If not,
87 an exception is raised.
88 """
fbe30a6d 89 resp = ecmd(*args)
90 if resp.getcode() != code:
91 raise ValueError, resp.getcode()
92 return resp
194d48ea 93
94def ecmds(*args):
95 """Another convenience function for ecmd.
96
97 Like ecmda, but will fail on all 5xx codes, and succeed on all
98 others.
99 """
100 resp = ecmd(*args)
101 if resp.getcode() >= 500 and resp.getcode() < 600:
f04b2c3c 102 raise ValueError, tuple(resp.extract()[0])
194d48ea 103 return resp
54b4a861 104
105def getresps():
106 """A generator function which will iterate over all responses from
107 getresp.
108 """
109 while True:
110 resp = getresp()
111 if resp is None:
112 break
113 else:
114 yield resp