# HG changeset patch # User Franz Glasner # Date 1743025679 -3600 # Node ID cd458e31855371e41bc9dafc7e3ab0a6810a5723 # Parent 285ee01dbb3979d67b49f6981a332e6404d56d7f treesum: break up TreesumWriter in TreesumWriter and a new base class WriterBase. An upcoming TabularTreesumWriter shall use the new base class also. diff -r 285ee01dbb39 -r cd458e318553 cutils/treesum.py --- 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):