Fix and optimise code (#904)

* Fix bug in early error reporting in main init
* Update docstrings and optimise code in GuiMain
* Update docstrings and optimise code in Config
* Update docstrings and optimise code in common module
* Update docstrings and optimise code in main project classes
* Update the OptionState class
* Update the spell checker class
* Update the file converter classes and extend tests
* Update the about, merge, split and item editor classes and extend tests
* Update item editor test
* Update about dialog tests
* Some minor test cleanup
* Fix typo and add clarification in contributing guide
This commit is contained in:
Veronica Berglyd Olsen
2021-10-14 20:35:42 +01:00
committed by GitHub
parent 2a06db0a2b
commit 1c45331b0a
31 changed files with 1042 additions and 666 deletions
+40 -28
View File
@@ -35,16 +35,24 @@ class NWSpellEnchant():
def __init__(self):
self.mainConf = novelwriter.CONFIG
self.theDict = None
self.projDict = set()
self.projectDict = None
self.spellLanguage = None
self.theBroker = None
self._theDict = None
self._projDict = set()
self._projectDict = None
self._spellLanguage = None
self._theBroker = None
logger.debug("Enchant spell checking activated")
return
##
# Getters and Setters
##
def spellLanguage(self):
return self._spellLanguage
def setLanguage(self, theLang, projectDict=None):
"""Load a dictionary for the language specified in the config.
If that fails, we load a mock dictionary so that lookups don't
@@ -52,49 +60,53 @@ class NWSpellEnchant():
"""
try:
import enchant
if self.theBroker is not None:
if self._theBroker is not None:
logger.debug("Deleting old pyenchant broker")
del self.theBroker
del self._theBroker
self.theBroker = enchant.Broker()
self.theDict = self.theBroker.request_dict(theLang)
self.spellLanguage = theLang
self._theBroker = enchant.Broker()
self._theDict = self._theBroker.request_dict(theLang)
self._spellLanguage = theLang
logger.debug("Enchant spell checking for language '%s' loaded", theLang)
except Exception:
logger.error("Failed to load enchant spell checking for language '%s'", theLang)
self.theDict = FakeEnchant()
self.spellLanguage = None
self._theDict = FakeEnchant()
self._spellLanguage = None
self._readProjectDictionary(projectDict)
for pWord in self.projDict:
self.theDict.add_to_session(pWord)
for pWord in self._projDict:
self._theDict.add_to_session(pWord)
return
##
# Methods
##
def checkWord(self, theWord):
"""Wrapper function for pyenchant.
"""
return self.theDict.check(theWord)
return self._theDict.check(theWord)
def suggestWords(self, theWord):
"""Wrapper function for pyenchant.
"""
return self.theDict.suggest(theWord)
return self._theDict.suggest(theWord)
def addWord(self, newWord):
"""Add a word to the project dictionary.
"""
self.theDict.add_to_session(newWord)
self._theDict.add_to_session(newWord)
if self.projectDict is not None and newWord not in self.projDict:
if self._projectDict is not None and newWord not in self._projDict:
newWord = newWord.strip()
try:
with open(self.projectDict, mode="a+", encoding="utf-8") as outFile:
with open(self._projectDict, mode="a+", encoding="utf-8") as outFile:
outFile.write("%s\n" % newWord)
self.projDict.add(newWord)
self._projDict.add(newWord)
except Exception:
logger.error("Failed to add word to project word list %s", str(self.projectDict))
logger.error("Failed to add word to project word list %s", str(self._projectDict))
novelwriter.logException()
return False
return True
@@ -119,8 +131,8 @@ class NWSpellEnchant():
dictionary.
"""
try:
spTag = self.theDict.tag
spName = self.theDict.provider.name
spTag = self._theDict.tag
spName = self._theDict.provider.name
except Exception:
logger.error("Failed to extract information about the dictionary")
novelwriter.logException()
@@ -137,8 +149,8 @@ class NWSpellEnchant():
"""Read the content of the project dictionary, and add it to the
lookup lists.
"""
self.projDict = set()
self.projectDict = projectDict
self._projDict = set()
self._projectDict = projectDict
if projectDict is None:
return False
@@ -151,9 +163,9 @@ class NWSpellEnchant():
with open(projectDict, mode="r", encoding="utf-8") as wordsFile:
for theLine in wordsFile:
theLine = theLine.strip()
if len(theLine) > 0 and theLine not in self.projDict:
self.projDict.add(theLine)
logger.debug("Project word list contains %d words", len(self.projDict))
if len(theLine) > 0 and theLine not in self._projDict:
self._projDict.add(theLine)
logger.debug("Project word list contains %d words", len(self._projDict))
except Exception:
logger.error("Failed to load project word list")