From 616149a9751a64294f167f3239d72629d05139fe Mon Sep 17 00:00:00 2001 From: Neil Flood Date: Sat, 3 May 2025 09:22:45 +1000 Subject: [PATCH 1/2] Cope when a parent has no history --- processinghistory/history.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/processinghistory/history.py b/processinghistory/history.py index 9f33c77..dbb07bf 100644 --- a/processinghistory/history.py +++ b/processinghistory/history.py @@ -48,6 +48,7 @@ METADATA_BY_KEY = "metadataByKey" PARENTS_BY_KEY = "parentsByKey" AUTOENVVARSLIST_NAME = "HISTORY_ENVVARS_TO_AUTOINCLUDE" +NO_TIMESTAMP = "UnknownTimestamp" # These GDAL drivers are known to have limits on the size of metadata which # can be stored, and so we need to keep below these, or we lose everything. @@ -248,20 +249,23 @@ def makeProcessingHistory(userDict, parents): for parentfile in parents: parentHist = readHistoryFromFile(filename=parentfile) - key = (os.path.basename(parentfile), - parentHist.metadataByKey[CURRENTFILE_KEY]['timestamp']) + if parentHist is not None: + key = (os.path.basename(parentfile), + parentHist.metadataByKey[CURRENTFILE_KEY]['timestamp']) - # Convert parent's "currentfile" metadata and parentage to normal key entries - procHist.metadataByKey[key] = parentHist.metadataByKey[CURRENTFILE_KEY] - procHist.parentsByKey[key] = parentHist.parentsByKey[CURRENTFILE_KEY] + # Convert parent's "currentfile" metadata and parentage to normal key entries + procHist.metadataByKey[key] = parentHist.metadataByKey[CURRENTFILE_KEY] + procHist.parentsByKey[key] = parentHist.parentsByKey[CURRENTFILE_KEY] - # Remove those from parentHist - parentHist.metadataByKey.pop(CURRENTFILE_KEY) - parentHist.parentsByKey.pop(CURRENTFILE_KEY) + # Remove those from parentHist + parentHist.metadataByKey.pop(CURRENTFILE_KEY) + parentHist.parentsByKey.pop(CURRENTFILE_KEY) - # Copy over all the other ancestor metadata and parentage - procHist.metadataByKey.update(parentHist.metadataByKey) - procHist.parentsByKey.update(parentHist.parentsByKey) + # Copy over all the other ancestor metadata and parentage + procHist.metadataByKey.update(parentHist.metadataByKey) + procHist.parentsByKey.update(parentHist.parentsByKey) + else: + key = (os.path.basename(parentfile), NO_TIMESTAMP) # Add this parent as parent of current file procHist.parentsByKey[CURRENTFILE_KEY].append(key) From bb7d87d73e527000a383c3547328b20525aa521a Mon Sep 17 00:00:00 2001 From: Neil Flood Date: Sat, 3 May 2025 09:22:59 +1000 Subject: [PATCH 2/2] Test for when parent has no history --- processinghistory/tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/processinghistory/tests.py b/processinghistory/tests.py index 7c89dd0..546d689 100755 --- a/processinghistory/tests.py +++ b/processinghistory/tests.py @@ -147,6 +147,29 @@ def test_ancestry(self): self.deleteTempFiles(filelist) + def test_parentNoHistory(self): + """ + The case of a parent which has no history + """ + childFile = 'child.kea' + parentFile = 'parent.kea' + makeRaster(childFile) + makeRaster(parentFile) + userDict = {'DESCRIPTION': "A test file", 'FIELD1': "Field value"} + history.writeHistoryToFile(userDict, filename=childFile, + parents=[parentFile]) + # Now read it back + procHist = history.readHistoryFromFile(filename=childFile) + + self.assertNotEqual(procHist, None, msg='History is None') + parentsList = procHist.parentsByKey[history.CURRENTFILE_KEY] + self.assertEqual(len(parentsList), 1, msg='Incorrect parent count') + numMetadata = len(procHist.metadataByKey) + self.assertEqual(numMetadata, 1, msg='Incorrect metadata count') + self.assertEqual(parentsList[0][0], parentFile, msg='Incorrect parent name') + + self.deleteTempFiles([parentFile, childFile]) + def test_useDataset(self): """ Test writing and reading history using an open gdal Dataset