Error logging and mutables as function defaults (#930)

* Remove mutable iterables as function defaults
* Improve error reporting and standardise how it's done
This commit is contained in:
Veronica Berglyd Olsen
2021-11-10 00:32:01 +01:00
committed by GitHub
parent 67d2e266fd
commit 00eea24e3f
15 changed files with 88 additions and 65 deletions
+5 -4
View File
@@ -27,6 +27,7 @@ import os
import logging
from novelwriter.enum import nwItemLayout, nwItemClass
from novelwriter.error import formatException
from novelwriter.common import isHandle, sha256sum
logger = logging.getLogger(__name__)
@@ -101,7 +102,7 @@ class NWDoc():
theText += inFile.read()
except Exception as exc:
self._docError = str(exc)
self._docError = formatException(exc)
return None
else:
@@ -150,7 +151,7 @@ class NWDoc():
outFile.write(docMeta)
outFile.write(docText)
except Exception as exc:
self._docError = str(exc)
self._docError = formatException(exc)
return False
# If we're here, the file was successfully saved, so we can
@@ -185,7 +186,7 @@ class NWDoc():
os.unlink(chkFile)
logger.debug("Deleted: %s", chkFile)
except Exception as exc:
self._docError = str(exc)
self._docError = formatException(exc)
return False
return True
@@ -225,7 +226,7 @@ class NWDoc():
##
def _parseMeta(self, metaLine):
"""Parse a line from the document statting with the characters
"""Parse a line from the document starting with the characters
%%~ that may contain meta data.
"""
if metaLine.startswith("%%~name:"):
+4 -3
View File
@@ -32,6 +32,7 @@ import novelwriter
from time import time
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout
from novelwriter.error import logException
from novelwriter.constants import nwFiles, nwKeyWords, nwUnicode
from novelwriter.core.document import NWDoc
from novelwriter.common import (
@@ -155,7 +156,7 @@ class NWIndex():
except Exception:
logger.error("Failed to load index file")
novelwriter.logException()
logException()
self.indexBroken = True
return False
@@ -194,7 +195,7 @@ class NWIndex():
except Exception:
logger.error("Failed to save index file")
novelwriter.logException()
logException()
return False
logger.verbose("Index saved in %.3f ms", (time() - tStart)*1000)
@@ -217,7 +218,7 @@ class NWIndex():
except Exception:
logger.error("Error while checking index")
novelwriter.logException()
logException()
self.indexBroken = True
logger.verbose("Index check took %.3f ms", (time() - tStart)*1000)
+4 -4
View File
@@ -27,10 +27,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import json
import logging
import novelwriter
from novelwriter.constants import nwFiles
from novelwriter.error import logException
from novelwriter.common import checkBool, checkFloat, checkInt, checkString
from novelwriter.constants import nwFiles
logger = logging.getLogger(__name__)
@@ -86,7 +86,7 @@ class OptionState():
theState = json.load(inFile)
except Exception:
logger.error("Failed to load GUI options file")
novelwriter.logException()
logException()
return False
# Filter out unused variables
@@ -113,7 +113,7 @@ class OptionState():
json.dump(self._theState, outFile, indent=2)
except Exception:
logger.error("Failed to save GUI options file")
novelwriter.logException()
logException()
return False
return True
+17 -13
View File
@@ -37,14 +37,15 @@ from PyQt5.QtCore import QCoreApplication
from novelwriter.core.tree import NWTree
from novelwriter.core.item import NWItem
from novelwriter.core.document import NWDoc
from novelwriter.core.status import NWStatus
from novelwriter.core.options import OptionState
from novelwriter.core.document import NWDoc
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.error import logException
from novelwriter.common import (
checkString, checkBool, checkInt, isHandle, formatTimeStamp,
makeFileNameSafe, hexToInt
)
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.constants import trConst, nwFiles, nwLabels
logger = logging.getLogger(__name__)
@@ -236,10 +237,13 @@ class NWProject():
return
def newProject(self, projData={}):
def newProject(self, projData=None):
"""Create a new project by populating the project tree with a
few starter items.
"""
if projData is None:
projData = {}
popMinimal = projData.get("popMinimal", True)
popCustom = projData.get("popCustom", False)
popSample = projData.get("popSample", False)
@@ -1252,7 +1256,7 @@ class NWProject():
except Exception:
logger.error("Failed to project language file")
novelwriter.logException()
logException()
return False
return True
@@ -1277,7 +1281,7 @@ class NWProject():
except Exception:
logger.error("Failed to read project lockfile")
novelwriter.logException()
logException()
return ["ERROR"]
return theLines
@@ -1298,7 +1302,7 @@ class NWProject():
except Exception:
logger.error("Failed to write project lockfile")
novelwriter.logException()
logException()
return False
return True
@@ -1315,7 +1319,7 @@ class NWProject():
os.unlink(lockFile)
except Exception:
logger.error("Failed to remove project lockfile")
novelwriter.logException()
logException()
return False
return True
@@ -1488,7 +1492,7 @@ class NWProject():
except Exception:
logger.error("Failed to write session stats file")
novelwriter.logException()
logException()
return False
return True
@@ -1526,7 +1530,7 @@ class NWProject():
except Exception:
errList.append(self.tr("Could not move: {0}").format(theFile))
logger.error("Could not move: %s", theFile)
novelwriter.logException()
logException()
elif len(dataItem) == 21 and dataItem.endswith("_main.bak"):
try:
@@ -1535,7 +1539,7 @@ class NWProject():
except Exception:
errList.append(self.tr("Could not delete: {0}").format(theFile))
logger.error("Could not delete: %s", theFile)
novelwriter.logException()
logException()
else:
theErr = self._moveUnknownItem(theData, dataItem)
@@ -1549,7 +1553,7 @@ class NWProject():
except Exception:
errList.append(self.tr("Could not delete: {0}").format(theFolder))
logger.error("Could not delete: %s", theFolder)
novelwriter.logException()
logException()
return errList
@@ -1569,7 +1573,7 @@ class NWProject():
logger.info("Moved to junk: %s", theSrc)
except Exception:
logger.error("Could not move item %s to junk", theSrc)
novelwriter.logException()
logException()
return self.tr("Could not move item {0} to {1}.").format(theSrc, theJunk)
return ""
@@ -1604,7 +1608,7 @@ class NWProject():
os.unlink(rmFile)
except Exception:
logger.error("Could not delete: %s", rmFile)
novelwriter.logException()
logException()
return False
return True
+5 -3
View File
@@ -27,6 +27,8 @@ import os
import logging
import novelwriter
from novelwriter.error import logException
logger = logging.getLogger(__name__)
@@ -107,7 +109,7 @@ class NWSpellEnchant():
self._projDict.add(newWord)
except Exception:
logger.error("Failed to add word to project word list %s", str(self._projectDict))
novelwriter.logException()
logException()
return False
return True
@@ -135,7 +137,7 @@ class NWSpellEnchant():
spName = self._theDict.provider.name
except Exception:
logger.error("Failed to extract information about the dictionary")
novelwriter.logException()
logException()
spTag = ""
spName = ""
@@ -169,7 +171,7 @@ class NWSpellEnchant():
except Exception:
logger.error("Failed to load project word list")
novelwriter.logException()
logException()
return False
return True
+2 -2
View File
@@ -25,13 +25,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import logging
import novelwriter
from time import time
from lxml import etree
from hashlib import sha256
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout
from novelwriter.error import logException
from novelwriter.common import checkHandle
from novelwriter.constants import nwConst, nwFiles
from novelwriter.core.item import NWItem
@@ -184,7 +184,7 @@ class NWTree():
except Exception:
logger.error("Could not write ToC file")
novelwriter.logException()
logException()
return False
return True