From 91e2092674b935f56a38787fea2e85b856b60494 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 27 Oct 2020 20:21:39 +0100 Subject: [PATCH] Move the spell check class constants to the nwConst class --- nw/config.py | 8 ++++---- nw/constants/constants.py | 6 ++++++ nw/core/spellcheck.py | 28 ++++++++++++---------------- nw/gui/doceditor.py | 4 ++-- nw/gui/preferences.py | 11 ++++++----- tests/test_project.py | 4 ++-- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/nw/config.py b/nw/config.py index 8fc681e0..05345b45 100644 --- a/nw/config.py +++ b/nw/config.py @@ -37,7 +37,7 @@ from shutil import which from PyQt5.Qt import PYQT_VERSION_STR from PyQt5.QtCore import QT_VERSION_STR, QStandardPaths, QSysInfo -from nw.constants import nwFiles, nwUnicode +from nw.constants import nwConst, nwFiles, nwUnicode from nw.common import splitVersionNumber, formatTimeStamp logger = logging.getLogger(__name__) @@ -88,7 +88,7 @@ class Config: self.guiIcons = "typicons_colour_light" self.guiDark = False # Load icons for dark backgrounds, if available self.guiLang = "en" # Hardcoded for now since the GUI is only in English - self.guiFont = "" # Defaults to system defualt font + self.guiFont = "" # Defaults to system default font self.guiFontSize = 11 self.guiScale = 1.0 # Set automatically by Theme class @@ -284,7 +284,7 @@ class Config: logger.verbose("App path: %s" % self.appPath) logger.verbose("Last path: %s" % self.lastPath) - # If config folder does not exist, make it. + # If config folder does not exist, create it. # This assumes that the os config folder itself exists. if not os.path.isdir(self.confPath): try: @@ -327,7 +327,7 @@ class Config: self._checkOptionalPackages() if self.spellTool is None: - self.spellTool = "internal" + self.spellTool = nwConst.SP_INTERNAL if self.spellLanguage is None: self.spellLanguage = "en" diff --git a/nw/constants/constants.py b/nw/constants/constants.py index e7549bb4..f2e97cf5 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -29,14 +29,20 @@ from nw.constants.enum import nwItemClass, nwItemLayout, nwOutline class nwConst(): + # Date and Time Formats tStampFmt = "%Y-%m-%d %H:%M:%S" # Default format fStampFmt = "%Y-%m-%d %H.%M.%S" # FileName safe format dStampFmt = "%Y-%m-%d" # Date only format + # Various Hard Limits maxDepth = 30 # Maximum folder depth of a project maxDocSize = 5000000 # Maxium size of a single document maxBuildSize = 10000000 # Maxium size of a project build + # Spell Check Providers + SP_INTERNAL = "internal" + SP_ENCHANT = "enchant" + # END Class nwConst class nwRegEx(): diff --git a/nw/core/spellcheck.py b/nw/core/spellcheck.py index 7223c616..b2f92a96 100644 --- a/nw/core/spellcheck.py +++ b/nw/core/spellcheck.py @@ -31,7 +31,7 @@ import os from difflib import get_close_matches -from nw.constants import isoLanguage +from nw.constants import nwConst, isoLanguage logger = logging.getLogger(__name__) @@ -41,11 +41,8 @@ logger = logging.getLogger(__name__) class NWSpellCheck(): - SP_INTERNAL = "internal" - SP_ENCHANT = "enchant" - theDict = None - PROJW = [] + projDict = [] def __init__(self): self.mainConf = nw.CONFIG @@ -71,9 +68,9 @@ class NWSpellCheck(): def addWord(self, newWord): """Add a word to the project dictionary. """ - if self.projectDict is not None and newWord not in self.PROJW: + if self.projectDict is not None and newWord not in self.projDict: newWord = newWord.strip() - self.PROJW.append(newWord) + self.projDict.append(newWord) try: with open(self.projectDict, mode="a+", encoding="utf-8") as outFile: outFile.write("%s\n" % newWord) @@ -110,7 +107,7 @@ class NWSpellCheck(): """Read the content of the project dictionary, and add it to the lookup lists. """ - self.PROJW = [] + self.projDict = [] if projectDict is not None: self.projectDict = projectDict if not os.path.isfile(projectDict): @@ -120,9 +117,9 @@ class NWSpellCheck(): 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.PROJW: - self.PROJW.append(theLine) - logger.debug("Project word list contains %d words" % len(self.PROJW)) + if len(theLine) > 0 and theLine not in self.projDict: + self.projDict.append(theLine) + logger.debug("Project word list contains %d words" % len(self.projDict)) except Exception as e: logger.error("Failed to load project word list") logger.error(str(e)) @@ -157,7 +154,7 @@ class NWSpellEnchant(NWSpellCheck): self.spellLanguage = None self._readProjectDictionary(projectDict) - for pWord in self.PROJW: + for pWord in self.projDict: self.theDict.add_to_session(pWord) return @@ -236,7 +233,6 @@ class NWSpellSimple(NWSpellCheck): when no other is available. This method is fairly slow compared to other implementations. """ - WORDS = [] def __init__(self): @@ -266,7 +262,7 @@ class NWSpellSimple(NWSpellCheck): self.spellLanguage = None self._readProjectDictionary(projectDict) - for pWord in self.PROJW: + for pWord in self.projDict: if pWord not in self.WORDS: self.WORDS.append(pWord) @@ -324,7 +320,7 @@ class NWSpellSimple(NWSpellCheck): if theBits[1] != ".dict": continue - spName = "%s [internal]" % self.expandLanguage(theBits[0]) + spName = "%s [%s]" % (self.expandLanguage(theBits[0]), nwConst.SP_INTERNAL) retList.append((theBits[0], spName)) return retList @@ -333,6 +329,6 @@ class NWSpellSimple(NWSpellCheck): """Return the tag and provider of the currently loaded dictionary. """ - return self.theLang, "internal" + return self.theLang, nwConst.SP_INTERNAL # END Class NWSpellSimple diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 4546d717..f45e569d 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -50,7 +50,7 @@ from PyQt5.QtWidgets import ( QFrame ) -from nw.core import NWDoc, NWSpellCheck, NWSpellSimple, countWords +from nw.core import NWDoc, NWSpellSimple, countWords from nw.gui.dochighlight import GuiDocHighlighter from nw.common import transferCase from nw.constants import ( @@ -1614,7 +1614,7 @@ class GuiDocEditor(QTextEdit): """Create the spell checking object based on the spellTool setting in config. """ - if self.mainConf.spellTool == NWSpellCheck.SP_ENCHANT: + if self.mainConf.spellTool == nwConst.SP_ENCHANT: from nw.core.spellcheck import NWSpellEnchant self.theDict = NWSpellEnchant() else: diff --git a/nw/gui/preferences.py b/nw/gui/preferences.py index 945e35f6..2bb22bde 100644 --- a/nw/gui/preferences.py +++ b/nw/gui/preferences.py @@ -37,7 +37,8 @@ from PyQt5.QtWidgets import ( ) from nw.gui.custom import QSwitch, QConfigLayout, PagedDialog, QuotesDialog -from nw.core import NWSpellCheck, NWSpellSimple, NWSpellEnchant +from nw.core import NWSpellSimple, NWSpellEnchant +from nw.constants import nwConst logger = logging.getLogger(__name__) @@ -703,11 +704,11 @@ class GuiConfigEditEditingTab(QWidget): ## Spell Check Provider and Language self.spellLangList = QComboBox(self) self.spellToolList = QComboBox(self) - self.spellToolList.addItem("Internal (difflib)", NWSpellCheck.SP_INTERNAL) - self.spellToolList.addItem("Spell Enchant (pyenchant)", NWSpellCheck.SP_ENCHANT) + self.spellToolList.addItem("Internal (difflib)", nwConst.SP_INTERNAL) + self.spellToolList.addItem("Spell Enchant (pyenchant)", nwConst.SP_ENCHANT) theModel = self.spellToolList.model() - idEnchant = self.spellToolList.findData(NWSpellCheck.SP_ENCHANT) + idEnchant = self.spellToolList.findData(nwConst.SP_ENCHANT) theModel.item(idEnchant).setEnabled(self.mainConf.hasEnchant) self.spellToolList.currentIndexChanged.connect(self._doUpdateSpellTool) @@ -814,7 +815,7 @@ class GuiConfigEditEditingTab(QWidget): preserve the language choice, if the language exists in the updated list. """ - if spellTool == NWSpellCheck.SP_ENCHANT: + if spellTool == nwConst.SP_ENCHANT: theDict = NWSpellEnchant() else: theDict = NWSpellSimple() diff --git a/tests/test_project.py b/tests/test_project.py index 9be85a67..b8df3c0e 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -13,7 +13,7 @@ from nwtools import cmpFiles from nw.core.project import NWProject from nw.core.document import NWDoc from nw.core.spellcheck import NWSpellEnchant, NWSpellSimple -from nw.constants import nwItemClass, nwItemType, nwItemLayout, nwFiles +from nw.constants import nwConst, nwItemClass, nwItemType, nwItemLayout, nwFiles @pytest.mark.project def testProjectNewOpenSave(nwFuncTemp, nwTempProj, nwRef, nwTemp, nwDummy): @@ -405,7 +405,7 @@ def testSpellSimple(nwTemp, nwConf): aTag, aName = spChk.describeDict() assert aTag == "en" - assert aName == "internal" + assert aName == nwConst.SP_INTERNAL @pytest.mark.project def testProjectOptions(nwDummy, nwLipsum):