added a print_func arg to TarFile.list to allow output to log or other
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 21 Jul 2016 15:37:08 +0000 (17:37 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 11 Nov 2020 12:18:34 +0000 (13:18 +0100)
deltatar/tarfile.py

index 5497de6..6763e7c 100644 (file)
@@ -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