Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/treesum.py @ 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 | 48430941c18c |
| children | a464f36fffcb |
comparison
equal
deleted
inserted
replaced
| 324:285ee01dbb39 | 325:cd458e318553 |
|---|---|
| 1257 if not modestr.startswith("0"): | 1257 if not modestr.startswith("0"): |
| 1258 modestr = "0" + modestr | 1258 modestr = "0" + modestr |
| 1259 return modestr | 1259 return modestr |
| 1260 | 1260 |
| 1261 | 1261 |
| 1262 class TreesumWriter(object): | 1262 class WriterBase(object): |
| 1263 | |
| 1264 """Base class for all treesum digest file writers. | |
| 1265 | |
| 1266 Wraps an output file pointer for a binary file. | |
| 1267 | |
| 1268 Provides low-level methods to write data lines. These methods must be | |
| 1269 used if the CRC is to be updated also. | |
| 1270 | |
| 1271 Also holds the current CRC for a block. | |
| 1272 | |
| 1273 """ | |
| 1274 | |
| 1275 LS = util.b(os.linesep) | |
| 1276 | |
| 1277 def __init__(self, outfp): | |
| 1278 self._outfp = outfp | |
| 1279 self.reset_crc() | |
| 1280 | |
| 1281 @property | |
| 1282 def crc(self): | |
| 1283 return self._crc | |
| 1284 | |
| 1285 def reset_crc(self): | |
| 1286 self._crc = crc32() | |
| 1287 | |
| 1288 def writeln(self, line): | |
| 1289 """Write the bytes `line` into the output file and update the CRC | |
| 1290 accordingly. | |
| 1291 | |
| 1292 :param bytes line: The line to write to (without line ending) | |
| 1293 | |
| 1294 """ | |
| 1295 self.write(line) | |
| 1296 self.write(self.LS) | |
| 1297 | |
| 1298 def write(self, data): | |
| 1299 """Write `data` into the output file and update the CRC accordingly. | |
| 1300 | |
| 1301 :param bytes data: The data to write to and to update the CRC with | |
| 1302 | |
| 1303 """ | |
| 1304 if data: | |
| 1305 self._outfp.write(data) | |
| 1306 self._crc.update(data) | |
| 1307 | |
| 1308 def flush(self): | |
| 1309 self._outfp.flush() | |
| 1310 | |
| 1311 | |
| 1312 class TreesumWriter(WriterBase): | |
| 1263 | 1313 |
| 1264 """Writer to write treesum digest files in a format similar to BSD | 1314 """Writer to write treesum digest files in a format similar to BSD |
| 1265 digest files. | 1315 digest files. |
| 1266 | 1316 |
| 1267 Wraps an output file pointer for a binary file. | |
| 1268 | |
| 1269 Provides high-level methods to write data lines. | 1317 Provides high-level methods to write data lines. |
| 1270 | 1318 |
| 1271 Also holds the current CRC for a block. | |
| 1272 | |
| 1273 """ | 1319 """ |
| 1274 | 1320 |
| 1275 LS = util.b(os.linesep) | 1321 def __init__(self, outfp, **kwds): |
| 1276 | 1322 # IGNORE **kwds |
| 1277 def __init__(self, outfp): | 1323 super(TreesumWriter, self).__init__(outfp) |
| 1278 self._outfp = outfp | |
| 1279 self._reset_crc() | |
| 1280 | |
| 1281 def _reset_crc(self): | |
| 1282 self._crc = crc32() | |
| 1283 | 1324 |
| 1284 def start(self, version): | 1325 def start(self, version): |
| 1285 """Begin a new block, reset the current CRC and write the VERSION | 1326 """Begin a new block, reset the current CRC and write the VERSION |
| 1286 tag. | 1327 tag. |
| 1287 | 1328 |
| 1288 """ | 1329 """ |
| 1289 self._reset_crc() | 1330 self.reset_crc() |
| 1290 self.write(b"VERSION = ") | 1331 self.write(b"VERSION = ") |
| 1291 self.writeln(util.b(version)) | 1332 self.writeln(util.b(version)) |
| 1292 | 1333 |
| 1293 def write_comment(self, comment): | 1334 def write_comment(self, comment): |
| 1294 self.write(b"COMMENT (") | 1335 self.write(b"COMMENT (") |
| 1376 self.write(util.b(str(size))) | 1417 self.write(util.b(str(size))) |
| 1377 self.writeln(b"") | 1418 self.writeln(b"") |
| 1378 | 1419 |
| 1379 def finish(self): | 1420 def finish(self): |
| 1380 """Finish a block and write the current CRC""" | 1421 """Finish a block and write the current CRC""" |
| 1381 crc = self._crc.hexdigest() | 1422 crc = self.crc.hexdigest() |
| 1382 self.write(b"CRC32 = ") | 1423 self.write(b"CRC32 = ") |
| 1383 self.writeln(util.b(crc)) | 1424 self.writeln(util.b(crc)) |
| 1384 self.flush() | 1425 self.flush() |
| 1385 | |
| 1386 def writeln(self, line): | |
| 1387 """Write the bytes `line` into the output file and update the CRC | |
| 1388 accordingly. | |
| 1389 | |
| 1390 :param bytes line: The line to write to (without line ending) | |
| 1391 | |
| 1392 """ | |
| 1393 self.write(line) | |
| 1394 self.write(self.LS) | |
| 1395 | |
| 1396 def write(self, data): | |
| 1397 """Write `data` into the output file and update the CRC accordingly. | |
| 1398 | |
| 1399 :param bytes data: The data to write to and to update the CRC with | |
| 1400 | |
| 1401 """ | |
| 1402 if data: | |
| 1403 self._outfp.write(data) | |
| 1404 self._crc.update(data) | |
| 1405 | |
| 1406 def flush(self): | |
| 1407 self._outfp.flush() | |
| 1408 | 1426 |
| 1409 | 1427 |
| 1410 class TreesumReader(object): | 1428 class TreesumReader(object): |
| 1411 | 1429 |
| 1412 """Reader to read and/or verify treesum digest files. | 1430 """Reader to read and/or verify treesum digest files. |
