# -*- coding: utf-8 -*-"""Implements :class:`SimpleCheckpoint`, a JSON-file-based checkpoint for localdevelopment and testing."""importjsonimportdataclassesfromcollections.abcimportIterablefrompathlibimportPathfromfunc_args.apiimportREQfrom..abstractionimportAbcRecordfrom..checkpointimport(T_POINTER,Tracker,BaseCheckPoint,)
[docs]@dataclasses.dataclassclassSimpleCheckpoint(BaseCheckPoint):""" A simple checkpoint using local json file for persistence. :param path_checkpoint: the path to the checkpoint file. :param path_records: the path to the records data file. """checkpoint_file:str=dataclasses.field(default=REQ)records_file:str=dataclasses.field(default=REQ)@propertydefpath_checkpoint(self)->Path:returnPath(self.checkpoint_file)@propertydefpath_records(self)->Path:returnPath(self.records_file)
[docs]defdump_records(self,records:Iterable[AbcRecord],):""" Dump the records in a batch to the persistence layer. """self.path_records.write_text("\n".join([record.serialize()forrecordinrecords]))
[docs]defload_records(self,record_class:type[AbcRecord],**kwargs,)->list[AbcRecord]:""" Load the batch records from the persistence layer. """ifself.path_records.exists():withself.path_records.open("r")asf:records=[record_class.deserialize(line)forlineinf.readlines()]returnrecordselse:return[]
[docs]defdump_as_in_progress(self,record:AbcRecord,):""" Dump the tracker to the persistence layer after calling :class:`BaseCheckpoint.mark_as_in_progress`. .. note:: It is up to the developer to implement the persistence layer to persist the data. :param record: the record to check. """self.dump()
[docs]defdump_as_failed_or_exhausted(self,record:AbcRecord,):""" Dump the tracker to the persistence layer after calling :class:`BaseCheckpoint.mark_as_failed_or_exhausted`. .. note:: It is up to the developer to implement the persistence layer to persist the data. :param record: the record to check. """self.dump()
[docs]defdump_as_succeeded(self,record:AbcRecord,):""" Dump the tracker to the persistence layer after calling :class:`BaseCheckpoint.mark_as_in_progress`. .. note:: It is up to the developer to implement the persistence layer to persist the data. :param record: the record to check. """self.dump()