Skip to content

Commit 2d9b637

Browse files
committed
Overload FTPFS.getmodified to use the MDTM feature if available
1 parent 145bcea commit 2d9b637

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

fs/ftpfs.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from .path import basename
4343
from .path import normpath
4444
from .path import split
45+
from .time import epoch_to_datetime
4546
from . import _ftp_parse as ftp_parse
4647

4748
if typing.TYPE_CHECKING:
@@ -574,6 +575,12 @@ def supports_mlst(self):
574575
"""bool: whether the server supports MLST feature."""
575576
return "MLST" in self.features
576577

578+
@property
579+
def supports_mdtm(self):
580+
# type: () -> bool
581+
"""bool: whether the server supports the MDTM feature."""
582+
return "MDTM" in self.features
583+
577584
def create(self, path, wipe=False):
578585
# type: (Text, bool) -> bool
579586
_path = self.validatepath(path)
@@ -669,25 +676,6 @@ def getinfo(self, path, namespaces=None):
669676
}
670677
)
671678

672-
if "modified" in namespaces:
673-
if "details" in namespaces:
674-
warnings.warn(
675-
"FTPFS.getinfo called with both 'modified' and 'details'"
676-
" namespace. The former will be ignored.",
677-
UserWarning,
678-
)
679-
else:
680-
with self._lock:
681-
with ftp_errors(self, path=path):
682-
cmd = "MDTM " + _encode(
683-
self.validatepath(path), self.ftp.encoding
684-
)
685-
response = self.ftp.sendcmd(cmd)
686-
modified_info = {
687-
"modified": self._parse_ftp_time(response.split()[1])
688-
}
689-
return Info({"modified": modified_info})
690-
691679
if self.supports_mlst:
692680
with self._lock:
693681
with ftp_errors(self, path=path):
@@ -716,6 +704,18 @@ def getmeta(self, namespace="standard"):
716704
_meta["supports_mtime"] = "MDTM" in self.features
717705
return _meta
718706

707+
def getmodified(self, path):
708+
# type: (Text) -> Optional[datetime]
709+
if self.supports_mdtm:
710+
_path = self.validatepath(path)
711+
with self._lock:
712+
with ftp_errors(self, path=path):
713+
cmd = "MDTM " + _encode(_path, self.ftp.encoding)
714+
response = self.ftp.sendcmd(cmd)
715+
mtime = self._parse_ftp_time(response.split()[1])
716+
return epoch_to_datetime(mtime)
717+
return super().getmodified(self, path)
718+
719719
def listdir(self, path):
720720
# type: (Text) -> List[Text]
721721
_path = self.validatepath(path)

tests/test_ftpfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ def test_getinfo_modified(self):
265265
self.assertIn("MDTM", self.fs.features)
266266
self.fs.create("bar")
267267
mtime_detail = self.fs.getinfo("bar", ("basic", "details")).modified
268-
mtime_modified = self.fs.getinfo("bar", ("modified",)).modified
268+
mtime_modified = self.fs.getmodified("bar")
269+
print(mtime_detail, mtime_modified)
269270
# Microsecond and seconds might not actually be supported by all
270271
# FTP commands, so we strip them before comparing if it looks
271272
# like at least one of the two values does not contain them.

0 commit comments

Comments
 (0)