From 5808dd4ffabdf97b251ea15a3429124481919ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Thu, 17 May 2018 06:52:15 +0000 Subject: [PATCH] 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" ) --- .../portal_components/document.erp5.FTPConnector.py | 4 ++-- product/ERP5Type/ConnectionPlugin/SFTPConnection.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bt5/erp5_web_service/DocumentTemplateItem/portal_components/document.erp5.FTPConnector.py b/bt5/erp5_web_service/DocumentTemplateItem/portal_components/document.erp5.FTPConnector.py index e8a71cc87f..adbf8c327d 100644 --- a/bt5/erp5_web_service/DocumentTemplateItem/portal_components/document.erp5.FTPConnector.py +++ b/bt5/erp5_web_service/DocumentTemplateItem/portal_components/document.erp5.FTPConnector.py @@ -79,11 +79,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() diff --git a/product/ERP5Type/ConnectionPlugin/SFTPConnection.py b/product/ERP5Type/ConnectionPlugin/SFTPConnection.py index 60c703e796..39435d2576 100644 --- a/product/ERP5Type/ConnectionPlugin/SFTPConnection.py +++ b/product/ERP5Type/ConnectionPlugin/SFTPConnection.py @@ -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)) -- 2.30.9