Skip to content

Commit 096d81f

Browse files
authored
Cope when a parent file has no processing history (#7)
* Cope when a parent has no history * Test for when parent has no history
1 parent 22388d9 commit 096d81f

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

processinghistory/history.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
METADATA_BY_KEY = "metadataByKey"
4949
PARENTS_BY_KEY = "parentsByKey"
5050
AUTOENVVARSLIST_NAME = "HISTORY_ENVVARS_TO_AUTOINCLUDE"
51+
NO_TIMESTAMP = "UnknownTimestamp"
5152

5253
# These GDAL drivers are known to have limits on the size of metadata which
5354
# can be stored, and so we need to keep below these, or we lose everything.
@@ -248,20 +249,23 @@ def makeProcessingHistory(userDict, parents):
248249
for parentfile in parents:
249250
parentHist = readHistoryFromFile(filename=parentfile)
250251

251-
key = (os.path.basename(parentfile),
252-
parentHist.metadataByKey[CURRENTFILE_KEY]['timestamp'])
252+
if parentHist is not None:
253+
key = (os.path.basename(parentfile),
254+
parentHist.metadataByKey[CURRENTFILE_KEY]['timestamp'])
253255

254-
# Convert parent's "currentfile" metadata and parentage to normal key entries
255-
procHist.metadataByKey[key] = parentHist.metadataByKey[CURRENTFILE_KEY]
256-
procHist.parentsByKey[key] = parentHist.parentsByKey[CURRENTFILE_KEY]
256+
# Convert parent's "currentfile" metadata and parentage to normal key entries
257+
procHist.metadataByKey[key] = parentHist.metadataByKey[CURRENTFILE_KEY]
258+
procHist.parentsByKey[key] = parentHist.parentsByKey[CURRENTFILE_KEY]
257259

258-
# Remove those from parentHist
259-
parentHist.metadataByKey.pop(CURRENTFILE_KEY)
260-
parentHist.parentsByKey.pop(CURRENTFILE_KEY)
260+
# Remove those from parentHist
261+
parentHist.metadataByKey.pop(CURRENTFILE_KEY)
262+
parentHist.parentsByKey.pop(CURRENTFILE_KEY)
261263

262-
# Copy over all the other ancestor metadata and parentage
263-
procHist.metadataByKey.update(parentHist.metadataByKey)
264-
procHist.parentsByKey.update(parentHist.parentsByKey)
264+
# Copy over all the other ancestor metadata and parentage
265+
procHist.metadataByKey.update(parentHist.metadataByKey)
266+
procHist.parentsByKey.update(parentHist.parentsByKey)
267+
else:
268+
key = (os.path.basename(parentfile), NO_TIMESTAMP)
265269

266270
# Add this parent as parent of current file
267271
procHist.parentsByKey[CURRENTFILE_KEY].append(key)

processinghistory/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ def test_ancestry(self):
147147

148148
self.deleteTempFiles(filelist)
149149

150+
def test_parentNoHistory(self):
151+
"""
152+
The case of a parent which has no history
153+
"""
154+
childFile = 'child.kea'
155+
parentFile = 'parent.kea'
156+
makeRaster(childFile)
157+
makeRaster(parentFile)
158+
userDict = {'DESCRIPTION': "A test file", 'FIELD1': "Field value"}
159+
history.writeHistoryToFile(userDict, filename=childFile,
160+
parents=[parentFile])
161+
# Now read it back
162+
procHist = history.readHistoryFromFile(filename=childFile)
163+
164+
self.assertNotEqual(procHist, None, msg='History is None')
165+
parentsList = procHist.parentsByKey[history.CURRENTFILE_KEY]
166+
self.assertEqual(len(parentsList), 1, msg='Incorrect parent count')
167+
numMetadata = len(procHist.metadataByKey)
168+
self.assertEqual(numMetadata, 1, msg='Incorrect metadata count')
169+
self.assertEqual(parentsList[0][0], parentFile, msg='Incorrect parent name')
170+
171+
self.deleteTempFiles([parentFile, childFile])
172+
150173
def test_useDataset(self):
151174
"""
152175
Test writing and reading history using an open gdal Dataset

0 commit comments

Comments
 (0)