From: Christian Herdtweck Date: Thu, 21 Jul 2016 15:37:08 +0000 (+0200) Subject: added a print_func arg to TarFile.list to allow output to log or other X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=2d7ed6ee59030599527eed3b9edba0bc9d6b9fc2;p=python-delta-tar added a print_func arg to TarFile.list to allow output to log or other --- diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 5497de6..6763e7c 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -2534,34 +2534,41 @@ class TarFile(object): tarinfo.devminor = os.minor(statres.st_rdev) return tarinfo - def list(self, verbose=True): - """Print a table of contents to sys.stdout. If `verbose' is False, only - the names of the members are printed. If it is True, an `ls -l'-like - output is produced. + def list(self, verbose=True, print_func=None): + """ Print a table of contents + + If `verbose' is False, only the names of the members are printed. If it + is True, an `ls -l'-like output is produced. + If print_func is specified, use that instead of print()ing to stdout """ self._check() + if print_func is None: + print_func = print + for tarinfo in self: + line_parts = [] if verbose: - print(stat.filemode(tarinfo.mode), end=' ') - print("%s/%s" % (tarinfo.uname or tarinfo.uid, - tarinfo.gname or tarinfo.gid), end=' ') + line_parts.append(stat.filemode(tarinfo.mode)) + line_parts.append("%s/%s" % (tarinfo.uname or tarinfo.uid, + tarinfo.gname or tarinfo.gid)) if tarinfo.ischr() or tarinfo.isblk(): - print("%10s" % ("%d,%d" \ - % (tarinfo.devmajor, tarinfo.devminor)), end=' ') + line_parts.append("%10s" % ("%d,%d" \ + % (tarinfo.devmajor, + tarinfo.devminor))) else: - print("%10d" % tarinfo.size, end=' ') - print("%d-%02d-%02d %02d:%02d:%02d" \ - % time.localtime(tarinfo.mtime)[:6], end=' ') + line_parts.append("%10d" % tarinfo.size) + line_parts.append("%d-%02d-%02d %02d:%02d:%02d" \ + % time.localtime(tarinfo.mtime)[:6]) - print(tarinfo.name + ("/" if tarinfo.isdir() else ""), end=' ') + line_parts.append(tarinfo.name + ("/" if tarinfo.isdir() else "")) if verbose: if tarinfo.issym(): - print("->", tarinfo.linkname, end=' ') + line_parts.append("->", tarinfo.linkname) if tarinfo.islnk(): - print("link to", tarinfo.linkname, end=' ') - print() + line_parts.append("link to", tarinfo.linkname) + print_func(' '.join(line_parts)) def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None): """Add the file `name' to the archive. `name' may be any type of file