Commit 5808dd4f authored by Jérome Perrin's avatar Jérome Perrin

web_service: introduce FTPConnector.listFiles sort_on operator

This allows sorting the list of filenames, by size ( sort_on="st_size" ),
by modification date ( sort_on="st_mdate" )
parent c65b92da
...@@ -79,11 +79,11 @@ class FTPConnector(XMLObject): ...@@ -79,11 +79,11 @@ class FTPConnector(XMLObject):
finally: finally:
conn.logout() conn.logout()
def listFiles(self, path="."): def listFiles(self, path=".", sort_on=None):
""" List file of a directory """ """ List file of a directory """
conn = self.getConnection() conn = self.getConnection()
try: try:
return conn.getDirectoryContent(path) return conn.getDirectoryContent(path, sort_on=sort_on)
finally: finally:
conn.logout() conn.logout()
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
############################################################################## ##############################################################################
import os, socket import os, socket
import operator
from urlparse import urlparse from urlparse import urlparse
from socket import gaierror, error, socket, getaddrinfo, AF_UNSPEC, SOCK_STREAM from socket import gaierror, error, socket, getaddrinfo, AF_UNSPEC, SOCK_STREAM
from xmlrpclib import Binary from xmlrpclib import Binary
...@@ -136,9 +137,17 @@ class SFTPConnection: ...@@ -136,9 +137,17 @@ class SFTPConnection:
# normalise CRLF/CR/LF like FTP's ASCII mode transfer. # normalise CRLF/CR/LF like FTP's ASCII mode transfer.
return os.linesep.join(self._getFile(filepath).splitlines()) return os.linesep.join(self._getFile(filepath).splitlines())
def getDirectoryContent(self, path): def getDirectoryContent(self, path, sort_on=None):
"""retrieve all entries in a givan path as a list""" """retrieve all entries in a givan path as a list.
`sort_on` parameter allows to retrieve the directory content in a sorted
order, it understands all parameters from
paramiko.sftp_attr.SFTPAttributes, the most useful being `st_mtime` to sort
by modification date.
"""
try: try:
if sort_on:
return [x.filename for x in sorted(self.conn.listdir_attr(path), key=operator.attrgetter(sort_on))]
return self.conn.listdir(path) return self.conn.listdir(path)
except (EOFError, error), msg: except (EOFError, error), msg:
raise SFTPError(str(msg) + ' while trying to list %s on %s' % (path, self.url)) raise SFTPError(str(msg) + ' while trying to list %s on %s' % (path, self.url))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment