changeset 325:cd458e318553

treesum: break up TreesumWriter in TreesumWriter and a new base class WriterBase. An upcoming TabularTreesumWriter shall use the new base class also.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 26 Mar 2025 22:47:59 +0100
parents 285ee01dbb39
children a464f36fffcb
files cutils/treesum.py
diffstat 1 files changed, 50 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Wed Mar 26 22:40:59 2025 +0100
+++ b/cutils/treesum.py	Wed Mar 26 22:47:59 2025 +0100
@@ -1259,14 +1259,14 @@
     return modestr
 
 
-class TreesumWriter(object):
+class WriterBase(object):
 
-    """Writer to write treesum digest files in a format similar to BSD
-    digest files.
+    """Base class for all treesum digest file writers.
 
     Wraps an output file pointer for a binary file.
 
-    Provides high-level methods to write data lines.
+    Provides low-level methods to write data lines. These methods must be
+    used if the CRC is to be updated also.
 
     Also holds the current CRC for a block.
 
@@ -1276,17 +1276,58 @@
 
     def __init__(self, outfp):
         self._outfp = outfp
-        self._reset_crc()
+        self.reset_crc()
+
+    @property
+    def crc(self):
+        return self._crc
+
+    def reset_crc(self):
+        self._crc = crc32()
+
+    def writeln(self, line):
+        """Write the bytes `line` into the output file and update the CRC
+        accordingly.
+
+        :param bytes line: The line to write to (without line ending)
+
+        """
+        self.write(line)
+        self.write(self.LS)
+
+    def write(self, data):
+        """Write `data` into the output file and update the CRC accordingly.
 
-    def _reset_crc(self):
-        self._crc = crc32()
+        :param bytes data: The data to write to and to update the CRC with
+
+        """
+        if data:
+            self._outfp.write(data)
+            self._crc.update(data)
+
+    def flush(self):
+        self._outfp.flush()
+
+
+class TreesumWriter(WriterBase):
+
+    """Writer to write treesum digest files in a format similar to BSD
+    digest files.
+
+    Provides high-level methods to write data lines.
+
+    """
+
+    def __init__(self, outfp, **kwds):
+        # IGNORE **kwds
+        super(TreesumWriter, self).__init__(outfp)
 
     def start(self, version):
         """Begin a new block, reset the current CRC and write the VERSION
         tag.
 
         """
-        self._reset_crc()
+        self.reset_crc()
         self.write(b"VERSION = ")
         self.writeln(util.b(version))
 
@@ -1378,34 +1419,11 @@
 
     def finish(self):
         """Finish a block and write the current CRC"""
-        crc = self._crc.hexdigest()
+        crc = self.crc.hexdigest()
         self.write(b"CRC32 = ")
         self.writeln(util.b(crc))
         self.flush()
 
-    def writeln(self, line):
-        """Write the bytes `line` into the output file and update the CRC
-        accordingly.
-
-        :param bytes line: The line to write to (without line ending)
-
-        """
-        self.write(line)
-        self.write(self.LS)
-
-    def write(self, data):
-        """Write `data` into the output file and update the CRC accordingly.
-
-        :param bytes data: The data to write to and to update the CRC with
-
-        """
-        if data:
-            self._outfp.write(data)
-            self._crc.update(data)
-
-    def flush(self):
-        self._outfp.flush()
-
 
 class TreesumReader(object):