diff cutils/treesum.py @ 336:0049f486e1cd

treesum: Also provide the WriterBase with flags when printing sizes
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 31 Mar 2025 12:57:39 +0200
parents 7620142aacd1
children e89eb63fc319
line wrap: on
line diff
--- a/cutils/treesum.py	Mon Mar 31 09:02:43 2025 +0200
+++ b/cutils/treesum.py	Mon Mar 31 12:57:39 2025 +0200
@@ -560,6 +560,8 @@
 
     with out_cm as outfp:
         writer = writerstyle(outfp,
+                             size_only=opts.size_only,
+                             print_size=opts.print_size,
                              use_base64=opts.base64,
                              grouping_separator=opts.grouping_separator
                              )
@@ -1298,7 +1300,7 @@
 
 class WriterBase(object):
 
-    """Base class for all treesum digest file writers.
+    """(Abstract) base class for all treesum digest file writers.
 
     Wraps an output file pointer for a binary file.
 
@@ -1319,9 +1321,15 @@
     DEFAULT_GROUPING_SEPARATOR = ""
     """Disable the thousands separator in case no subclass redefines it"""
 
-    def __init__(self, outfp, use_base64=False, grouping_separator=None,
+    def __init__(self, outfp, size_only=False, print_size=False,
+                 use_base64=False, grouping_separator=None,
                  size_column_width=None):
+        # Poor man's abstract abstract class implemenetation
+        assert self.__class__ is not WriterBase
+
         self._outfp = outfp
+        self.size_only = size_only
+        self.print_size = print_size
         self.use_base64 = use_base64
         self.grouping_separator = (grouping_separator
                                    if grouping_separator is not None
@@ -1329,6 +1337,24 @@
         self.size_column_width = size_column_width or 0
         self.reset_crc()
 
+    def write_size(self, filename, sz):
+        """
+
+        If `sz` is `None` then this is signals an error because a size is
+        required.
+
+        """
+        raise NotImplementedError("write_size")
+
+    def write_file_digest(self, algorithm, filename, digest, size=None):
+        """
+
+        If `size` is `None` and the output of a size is required then this
+        is an error signal.
+
+        """
+        raise NotImplementedError("write_file_digest")
+
     @property
     def crc(self):
         return self._crc