1 from Queue import Queue, Empty
3 from threading import Event
5 from gridftp import FTPClient, Buffer, HandleAttr, OperationAttr
8 class FTPException(Exception):
13 def __init__(self, proxy=None):
15 self.attr = HandleAttr()
16 self.op_attr = OperationAttr()
19 self.op_attr.set_authorization(proxy)
21 self.buffer = Buffer(4096)
22 self.cli = FTPClient(self.attr)
29 self.op_attr.destroy()
32 def report_error(self):
33 if self.error is not None:
34 match = re.search(r'A system call failed: (.*)$', self.error.replace('\r\n', '\n'), re.MULTILINE)
36 msg = match.groups()[0] if match else "Unknown error"
38 raise FTPException(msg)
40 def _read(self, arg, handle, error, buff, length, offset, eof):
42 self.stream.put(str(buff))
45 self.cli.register_read(self.buffer, self._read, None)
47 def _done(self, arg, handle, error):
51 def listing(self, url):
52 self.cli.verbose_list(url, self._done, None, self.op_attr)
53 self.cli.register_read(self.buffer, self._read, None)
61 while not self.stream.empty():
62 result += self.stream.get()
67 self.cli.get(url, self._done, None, self.op_attr)
68 self.cli.register_read(self.buffer, self._read, None)
72 yield self.stream.get(timeout=0.1)