X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fftp.py;h=38b23a5eb22b7aaaedf6aac5ffe3fcfc022d3413;hb=a968d1a54ba12336da3f05bfee5317721c563443;hp=c0b3e25d4d54419800d2433ef4f3a028d6cc0f53;hpb=be284bd6256a7a095d62786d9b58f815eefb769a;p=qcg-portal.git diff --git a/filex/ftp.py b/filex/ftp.py index c0b3e25..38b23a5 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -1,7 +1,9 @@ from datetime import datetime from Queue import Queue, Empty +import os import re from threading import Event +from urlparse import urlparse from django.utils.timezone import localtime, UTC from gridftp import FTPClient, Buffer, HandleAttr, OperationAttr @@ -84,25 +86,25 @@ class FTPOperation: @staticmethod def _parse_mlst(listing): - data = [] - for item in listing.strip().splitlines(): # we may receive empty string when there are multiple consecutive newlines in listing if item: attrs, name = item.split(' ', 1) - attrs = dict((attr.split('=') for attr in attrs.split(';') if attr)) - - date = localtime(datetime.strptime(attrs['Modify'], "%Y%m%d%H%M%S").replace(tzinfo=UTC())) + attrs_dict = {} + for attr in attrs.split(';'): + try: + key, value = attr.split('=', 1) + attrs_dict[key] = value + except ValueError: + pass - data.append({ + yield { 'name': name, - 'type': 'file' if attrs['Type'] == 'file' else 'directory', - 'size': int(attrs['Size']), - 'date': date, - }) - - return data + 'type': 'directory' if attrs_dict['Type'] == 'dir' else 'file', + 'size': int(attrs_dict['Size']), + 'date': localtime(datetime.strptime(attrs_dict['Modify'], "%Y%m%d%H%M%S").replace(tzinfo=UTC())), + } def get(self, url): self.cli.get(url, self._done, None, self.op_attr) @@ -125,3 +127,26 @@ class FTPOperation: self.cli.move(src, dst, self._done, None, self.op_attr) self.wait() + + def info(self, url): + data = self.listing(url).next() + + if data['name'] == '.': + data['name'] = os.path.basename(urlparse(url).path.rstrip('/')) or u'/' + + return data + + def delete(self, url): + self.cli.delete(url, self._done, None, self.op_attr) + + self.wait() + + def rmdir(self, url): + self.cli.rmdir(url, self._done, None, self.op_attr) + + self.wait() + + def mkdir(self, url): + self.cli.mkdir(url, self._done, None, self.op_attr) + + self.wait()