python: Fix libvirt.py generation to include virterror info
[libvirt-python/ericb.git] / libvirt-override-virStream.py
blob92e6fb1349718709758da0fd0ee4736af61e457f
1 def __del__(self):
2 try:
3 if self.cb:
4 libvirtmod.virStreamEventRemoveCallback(self._o)
5 except AttributeError:
6 pass
8 if self._o != None:
9 libvirtmod.virStreamFree(self._o)
10 self._o = None
12 def _dispatchStreamEventCallback(self, events, cbData):
13 """
14 Dispatches events to python user's stream event callbacks
15 """
16 cb = cbData["cb"]
17 opaque = cbData["opaque"]
19 cb(self, events, opaque)
20 return 0
22 def eventAddCallback(self, events, cb, opaque):
23 self.cb = cb
24 cbData = {"stream": self, "cb" : cb, "opaque" : opaque}
25 ret = libvirtmod.virStreamEventAddCallback(self._o, events, cbData)
26 if ret == -1: raise libvirtError ('virStreamEventAddCallback() failed')
28 def recvAll(self, handler, opaque):
29 """Receive the entire data stream, sending the data to the
30 requested data sink. This is simply a convenient alternative
31 to virStreamRecv, for apps that do blocking-I/o.
33 A hypothetical handler function looks like:
35 def handler(stream, # virStream instance
36 buf, # string containing received data
37 opaque): # extra data passed to recvAll as opaque
38 fd = opaque
39 return os.write(fd, buf)
40 """
41 while True:
42 got = self.recv(1024*64)
43 if got == -2:
44 raise libvirtError("cannot use recvAll with "
45 "nonblocking stream")
46 if len(got) == 0:
47 break
49 try:
50 ret = handler(self, got, opaque)
51 if type(ret) is int and ret < 0:
52 raise RuntimeError("recvAll handler returned %d" % ret)
53 except Exception, e:
54 try:
55 self.abort()
56 except:
57 pass
58 raise e
60 def sendAll(self, handler, opaque):
61 """
62 Send the entire data stream, reading the data from the
63 requested data source. This is simply a convenient alternative
64 to virStreamSend, for apps that do blocking-I/o.
66 A hypothetical handler function looks like:
68 def handler(stream, # virStream instance
69 nbytes, # int amt of data to read
70 opaque): # extra data passed to recvAll as opaque
71 fd = opaque
72 return os.read(fd, nbytes)
73 """
74 while True:
75 try:
76 got = handler(self, 1024*64, opaque)
77 except:
78 try:
79 self.abort()
80 except:
81 pass
82 raise e
84 if got == "":
85 break
87 ret = self.send(got)
88 if ret == -2:
89 raise libvirtError("cannot use recvAll with "
90 "nonblocking stream")
92 def recv(self, nbytes):
93 """Write a series of bytes to the stream. This method may
94 block the calling application for an arbitrary amount
95 of time.
97 Errors are not guaranteed to be reported synchronously
98 with the call, but may instead be delayed until a
99 subsequent call.
101 On success, the received data is returned. On failure, an
102 exception is raised. If the stream is a NONBLOCK stream and
103 the request would block, integer -2 is returned.
105 ret = libvirtmod.virStreamRecv(self._o, nbytes)
106 if ret == None: raise libvirtError ('virStreamRecv() failed')
107 return ret
109 def send(self, data):
110 """Write a series of bytes to the stream. This method may
111 block the calling application for an arbitrary amount
112 of time. Once an application has finished sending data
113 it should call virStreamFinish to wait for successful
114 confirmation from the driver, or detect any error
116 This method may not be used if a stream source has been
117 registered
119 Errors are not guaranteed to be reported synchronously
120 with the call, but may instead be delayed until a
121 subsequent call.
123 ret = libvirtmod.virStreamSend(self._o, data, len(data))
124 if ret == -1: raise libvirtError ('virStreamSend() failed')
125 return ret