From c917fc9c9c104959a175ffcdd55379a4a2d11be8 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 17 Aug 2022 21:49:41 +0200 Subject: [PATCH] Fix issue #1096 and remove a couple of error ourputs --- novelwriter/config.py | 2 +- novelwriter/core/spellcheck.py | 44 +++++++++++++++++++++++----------- novelwriter/gui/doceditor.py | 2 +- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/novelwriter/config.py b/novelwriter/config.py index c5159493..e52a7bd7 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -360,7 +360,7 @@ class Config: # Check the availability of optional packages self._checkOptionalPackages() - if self.spellLanguage is None: + if not self.spellLanguage: self.spellLanguage = "en" # Look for a PDF version of the manual diff --git a/novelwriter/core/spellcheck.py b/novelwriter/core/spellcheck.py index 8f2fcea9..d9c506cd 100644 --- a/novelwriter/core/spellcheck.py +++ b/novelwriter/core/spellcheck.py @@ -26,6 +26,8 @@ along with this program. If not, see . import os import logging +from collections import namedtuple + from novelwriter.error import logException logger = logging.getLogger(__name__) @@ -46,36 +48,47 @@ class NWSpellEnchant(): return ## - # Getters and Setters + # Properties ## + @property def spellLanguage(self): return self._spellLanguage + ## + # Setters + ## + 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 - crash. + crash. Note that enchant will allow loading an empty string as + a tag, but this will fail later on. See issue #1096. """ + self._theBroker = None + self._theDict = None + self._spellLanguage = None + try: import enchant - if self._theBroker is not None: - logger.debug("Deleting old pyenchant broker") - del self._theBroker - self._theBroker = enchant.Broker() - self._theDict = self._theBroker.request_dict(theLang) - self._spellLanguage = theLang - logger.debug("Enchant spell checking for language '%s' loaded", theLang) + if theLang and enchant.dict_exists(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) + else: + logger.warning("Enchant found no dictionary for language '%s'", theLang) except Exception: logger.error("Failed to load enchant spell checking for language '%s'", theLang) - self._theDict = FakeEnchant() - self._spellLanguage = None - self._readProjectDictionary(projectDict) - for pWord in self._projDict: - self._theDict.add_to_session(pWord) + if self._theDict is None: + self._theDict = FakeEnchant() + else: + self._readProjectDictionary(projectDict) + for pWord in self._projDict: + self._theDict.add_to_session(pWord) return @@ -189,6 +202,9 @@ class FakeEnchant: """Fallback for when Enchant is selected, but not installed. """ def __init__(self): + self.tag = "" + self.provider = namedtuple("provider", "name") + self.provider.name = "" return def check(self, theWord): diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 4d35c978..59275295 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -718,7 +718,7 @@ class GuiDocEditor(QTextEdit): ), nwAlert.INFO) theMode = False - if self.spEnchant.spellLanguage() is None: + if self.spEnchant.spellLanguage is None: theMode = False self._spellCheck = theMode