Move the spell check class constants to the nwConst class

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