Fix issue with document hash comparison

This commit is contained in:
Veronica Berglyd Olsen
2023-08-25 17:13:41 +02:00
parent 0d262c0ae8
commit 125d501aaf
+13 -10
View File
@@ -59,8 +59,7 @@ class NWDocument:
self._fileLoc = None # The file location of the currently open item self._fileLoc = None # The file location of the currently open item
self._docMeta = {} # The meta data of the currently open item self._docMeta = {} # The meta data of the currently open item
self._docError = "" # The latest encountered IO error self._docError = "" # The latest encountered IO error
self._readHash = "" # The SHA hash on last read self._lastHash = "" # The last known SHA hash
self._writeHash = "" # The SHA hash on last write
self._hashError = False # Hash mismatch on last write attempt self._hashError = False # Hash mismatch on last write attempt
if isHandle(tHandle): if isHandle(tHandle):
@@ -130,6 +129,7 @@ class NWDocument:
text = "" text = ""
self._docMeta = {} self._docMeta = {}
self._lastHash = ""
if docPath.exists(): if docPath.exists():
try: try:
@@ -152,11 +152,10 @@ class NWDocument:
else: else:
# The document file does not exist, so we assume it's a new # The document file does not exist, so we assume it's a new
# document and initialise an empty text string. # document and return an empty text string.
logger.debug("The requested document does not exist") logger.debug("The requested document does not exist")
return ""
self._readHash = hashlib.sha1(text.encode()).hexdigest() self._lastHash = hashlib.sha1(text.encode()).hexdigest()
return text return text
@@ -182,19 +181,23 @@ class NWDocument:
docTemp = docPath.with_suffix(".tmp") docTemp = docPath.with_suffix(".tmp")
# Re-read the document on disk to check if it has changed # Re-read the document on disk to check if it has changed
prevHash = self._lastHash
self.readDocument() self.readDocument()
if self._writeHash and self._writeHash != self._readHash and not forceWrite: print(f"'{self._lastHash}', '{prevHash}'")
if self._lastHash != prevHash and not forceWrite:
logger.error("File has been altered on disk since opened") logger.error("File has been altered on disk since opened")
self._hashError = True self._hashError = True
return False return False
currTime = formatTimeStamp(time())
writeHash = hashlib.sha1(text.encode()).hexdigest() writeHash = hashlib.sha1(text.encode()).hexdigest()
createdDate = self._docMeta.get("created", "Unknown") createdDate = self._docMeta.get("created", "Unknown")
updatedDate = self._docMeta.get("updated", "Unknown") updatedDate = self._docMeta.get("updated", "Unknown")
if writeHash != self._writeHash: if writeHash != self._lastHash:
updatedDate = formatTimeStamp(time()) updatedDate = currTime
if not docPath.is_file(): if not docPath.is_file():
createdDate = updatedDate createdDate = currTime
updatedDate = currTime
# DocMeta Line # DocMeta Line
if self._item is None: if self._item is None:
@@ -224,7 +227,7 @@ class NWDocument:
self._docError = formatException(exc) self._docError = formatException(exc)
return False return False
self._writeHash = writeHash self._lastHash = writeHash
self._hashError = False self._hashError = False
return True return True