Commit 48d66e8e authored by Jérome Perrin's avatar Jérome Perrin

sftp: introduce 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 6a8a762c
......@@ -80,11 +80,11 @@ class FTPConnector(XMLObject):
finally:
conn.logout()
def listFiles(self, path="."):
def listFiles(self, path=".", sort_on=None):
""" List file of a directory """
conn = self.getConnection()
try:
return conn.getDirectoryContent(path)
return conn.getDirectoryContent(path, sort_on=sort_on)
finally:
conn.logout()
......
......@@ -28,6 +28,7 @@
##############################################################################
import os, socket
import operator
from urlparse import urlparse
from socket import gaierror, error, socket, getaddrinfo, AF_UNSPEC, SOCK_STREAM
from xmlrpclib import Binary
......@@ -136,9 +137,17 @@ class SFTPConnection:
# normalise CRLF/CR/LF like FTP's ASCII mode transfer.
return os.linesep.join(self._getFile(filepath).splitlines())
def getDirectoryContent(self, path):
"""retrieve all entries in a givan path as a list"""
def getDirectoryContent(self, path, sort_on=None):
"""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:
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)
except (EOFError, error), msg:
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