[ftputil] statdir patches
Dan Milstein
danmil at comcast.net
Thu Jul 20 23:33:18 CEST 2006
All,
Here, as both a single attachment and also inline in the email, are a
pair of patches for ftputil.py and ftp_stat.py. They implement a new
method for ftphost:
> ftphost.statdir(path)
statdir returns a list of stat_result objects for the files on the
remote server in the directory 'path'. This allows for an efficient,
if slightly low-level, means of examining a potentially long list of
files on a remote server, and taking action based on modification
dates, directory/file nature, etc.
Stefan: let me know if you need any official thing to say "Yes, I
submit this code to the project and release it under a BSD license".
-Dan
--- ../ftputil-2.1/ftputil.py 2006-03-01 19:55:46.000000000 -0500
+++ ftputil.py 2006-07-20 17:25:48.000000000 -0400
@@ -697,6 +697,20 @@
"""
return self._stat.stat(path, _exception_for_missing_path)
+ def statdir(self, path, _exception_for_missing_path=True):
+ """
+ Return a list of "stat" info for each file in the directory
`path`.
+
+ If the directory `path` can't be parsed, raise a
`ParserError`. If
+ the directory containing `path` can be parsed but the `path`
can't
+ be found, raise a `PermanentError`.
+
+ (`_exception_for_missing_path` is an implementation aid and
+ _not_ intended for use by ftputil clients.)
+
+ """
+ return self._stat.statdir(path,
_exception_for_missing_path=True)
+
def walk(self, top, topdown=True, onerror=None):
"""
Iterate over directory tree and return a tuple (dirpath,
--- ../ftputil-2.1/ftp_stat.py 2006-02-19 12:01:38.000000000 -0500
+++ ftp_stat.py 2006-07-20 17:26:07.000000000 -0400
@@ -449,6 +449,36 @@
# remember the path we have encountered
visited_paths[path] = True
+ def _real_statdir(self, path, _exception_for_missing_path=True):
+ """
+ Return a list of "stat" info for each file in the directory
`path`.
+ Do not follow symbolic links (i.e. like "lstat")
+
+ If the directory `path` can't be parsed, raise a
`ParserError`. If
+ the directory containing `path` can be parsed but the `path`
can't
+ be found, raise a `PermanentError`.
+
+ (`_exception_for_missing_path` is an implementation aid and
+ _not_ intended for use by ftputil clients.)
+
+ """
+ # get output from FTP's `DIR` command
+ lines = []
+ path = self._path.abspath(path)
+ lines = self._host_dir(path)
+ result = []
+ for line in lines:
+ try:
+ stat_result = self._parser.parse_line(line,
+
self._host.time_shift())
+ result.append(stat_result)
+ except ftp_error.ParserError:
+ # ignore things like "total 17", as found in some
+ # server listings
+ if not line.lower().startswith("total"):
+ raise
+ return result
+
def __call_with_parser_retry(self, method, *args, **kwargs):
"""
Call `method` with the `args` and `kwargs` once. If that
@@ -483,3 +513,6 @@
return self.__call_with_parser_retry(self._real_stat, path,
_exception_for_missing_path)
+ def statdir(self, path, _exception_for_missing_path=True):
+ return self.__call_with_parser_retry(self._real_statdir, path,
+
_exception_for_missing_path)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ftputil.patch
Type: application/octet-stream
Size: 2906 bytes
Desc: not available
Url : http://codespeak.net/pipermail/ftputil/attachments/20060720/a2cefabe/attachment.obj
More information about the ftputil
mailing list