From f5af3fb29343d005871d6c19025a0bbac4961e02 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 6 Nov 2019 11:45:17 +0100 Subject: [PATCH] Cleaned up initialisation and config settings for spell checking --- novelWriter.py | 23 ++++++-------------- nw/__init__.py | 7 +----- nw/config.py | 42 ++++++++++++++++++++++++++++-------- nw/gui/elements/doceditor.py | 19 ++++++++++------ 4 files changed, 53 insertions(+), 38 deletions(-) diff --git a/novelWriter.py b/novelWriter.py index 88070229..0723076f 100755 --- a/novelWriter.py +++ b/novelWriter.py @@ -5,7 +5,7 @@ import sys if sys.hexversion < 0x030500F0: print("ERROR: At least Python 3.5 is required") - exit(1) + sys.exit(1) try: import PyQt5.QtWidgets @@ -13,37 +13,26 @@ try: import PyQt5.QtCore except: print("ERROR: Failed to load dependency python3-pyqt5") - exit(1) + sys.exit(1) try: import PyQt5.QtSvg except: print("ERROR: Failed to load dependency python3-pyqt5.qtsvg") - exit(1) + sys.exit(1) try: import lxml except: print("ERROR: Failed to load dependency python3-lxml") - exit(1) + sys.exit(1) try: import appdirs except: print("ERROR: Failed to load dependency python3-appdirs") - exit(1) - -spellPack = None -try: - import enchant - spellPack = "enchant" -except: - print("WARNING: No spell check library found.") - print("Please install python3-enchant if you want to use spell checking") + sys.exit(1) if __name__ == "__main__": import nw - inArgs = sys.argv[1:] - if spellPack is not None: - inArgs.append("--spell=%s" % spellPack) - nw.main(inArgs) + nw.main(sys.argv[1:]) diff --git a/nw/__init__.py b/nw/__init__.py index 4b2b82a2..8807fa45 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -89,7 +89,6 @@ def main(sysArgs=None): "version", "config=", "testmode", - "spell=", "style=", ] @@ -106,7 +105,7 @@ def main(sysArgs=None): " -q, --quiet Disable output to command line. Does not affect log file.\n" " -t, --time Shows time stamp in logging output.\n" " -l, --logfile= Specify log file.\n" - " --style= Set Qt5 style flag. Defaults to Fusion.\n" + " --style= Set Qt5 style flag. Defaults to 'Fusion'.\n" " --config= Alternative config file.\n" " --headless Do not display GUI. Useful for testing scripts.\n" ).format( @@ -126,7 +125,6 @@ def main(sysArgs=None): showTime = False confPath = None testMode = False - spellTool = None qtStyle = "Fusion" # Parse Options @@ -163,13 +161,10 @@ def main(sysArgs=None): confPath = inArg elif inOpt in ("--testmode"): testMode = True - elif inOpt in ("--spell"): - spellTool = inArg # Set Config Options CONFIG.showGUI = not testMode CONFIG.debugInfo = debugLevel < logging.INFO - CONFIG.spellTool = spellTool # Set Logging if showTime: debugStr = timeStr+debugStr diff --git a/nw/config.py b/nw/config.py index d65519cb..43865cdc 100644 --- a/nw/config.py +++ b/nw/config.py @@ -41,7 +41,6 @@ class Config: self.appHandle = nw.__package__.lower() self.showGUI = True self.debugInfo = False - self.spellTool = None # Set Paths self.confPath = None @@ -94,7 +93,8 @@ class Config: self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO] self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO] - self.spellLanguage = "en_GB" + self.spellTool = None + self.spellLanguage = None ## Backup self.backupPath = "" @@ -137,9 +137,9 @@ class Config: self.osDarwin = False self.osUnknown = False if self.osType.startswith("linux"): - self.osLinux = True + self.osLinux = True elif self.osType.startswith("darwin"): - self.osDarwin = True + self.osDarwin = True elif self.osType.startswith("win32"): self.osWindows = True elif self.osType.startswith("cygwin"): @@ -147,6 +147,9 @@ class Config: else: self.osUnknown = True + # Packages + self.hasEnchant = False + return ## @@ -186,13 +189,16 @@ class Config: # If it exists, load it self.loadConfig() else: - # If it does not exist, save a copy of the defaults + # If it does not exist, save a copy of the default values self.saveConfig() + # Check the availability of optional packages + self._checkOptionalPackages() + if self.spellTool is None: - logger.warning("No spell check tool available") - else: - logger.debug("Using spell check tool '%s'" % self.spellTool) + self.spellTool = "simple" + if self.spellLanguage is None: + self.spellLanguage = "en" return True @@ -288,8 +294,11 @@ class Config: self.fmtDoubleQuotes = self._parseLine( cnfParse, cnfSec, "fmtdoublequote", self.CNF_LIST, self.fmtDoubleQuotes ) + self.spellTool = self._parseLine( + cnfParse, cnfSec, "spelltool", self.CNF_STR, self.spellTool + ) self.spellLanguage = self._parseLine( - cnfParse, cnfSec, "spellcheck", self.CNF_STR, self.spellLanguage + cnfParse, cnfSec, "spellcheck", self.CNF_STR, self.spellLanguage ) self.showTabsNSpaces = self._parseLine( cnfParse, cnfSec, "showtabsnspaces", self.CNF_BOOL, self.showTabsNSpaces @@ -380,6 +389,7 @@ class Config: cnfParse.set(cnfSec,"repdots", str(self.doReplaceDots)) cnfParse.set(cnfSec,"fmtsinglequote", self._packList(self.fmtSingleQuotes)) cnfParse.set(cnfSec,"fmtdoublequote", self._packList(self.fmtDoubleQuotes)) + cnfParse.set(cnfSec,"spelltool", str(self.spellTool)) cnfParse.set(cnfSec,"spellcheck", str(self.spellLanguage)) cnfParse.set(cnfSec,"showtabsnspaces", str(self.showTabsNSpaces)) cnfParse.set(cnfSec,"showlineendings", str(self.showLineEndings)) @@ -520,4 +530,18 @@ class Config: return None return checkVal + def _checkOptionalPackages(self): + """Cheks if we have the optional packages used by some features. + """ + + try: + import pyenchant + self.hasEnchant = True + logger.debug("Checking package pyenchant: Ok") + except: + self.hasEnchant = False + logger.debug("Checking package pyenchant: Missing") + + return + # End Class Config diff --git a/nw/gui/elements/doceditor.py b/nw/gui/elements/doceditor.py index 72dea74b..4decc7f5 100644 --- a/nw/gui/elements/doceditor.py +++ b/nw/gui/elements/doceditor.py @@ -26,7 +26,7 @@ from PyQt5.QtGui import ( from nw.project import NWDoc from nw.gui.tools import GuiDocHighlighter, WordCounter -from nw.tools import NWSpellCheck +from nw.tools import NWSpellCheck, NWSpellSimple from nw.constants import nwFiles, nwUnicode, nwDocAction, nwAlert logger = logging.getLogger(__name__) @@ -64,11 +64,7 @@ class GuiDocEditor(QTextEdit): self.qDocument = self.document() self.qDocument.setDocumentMargin(self.mainConf.textMargin) self.qDocument.contentsChange.connect(self._docChange) - if self.mainConf.spellTool == "enchant": - from nw.tools.spellenchant import NWSpellEnchant - self.theDict = NWSpellEnchant() - else: - self.theDict = NWSpellCheck() + self._setupSpellChecking() self.hLight = GuiDocHighlighter(self.qDocument, self.theParent) self.hLight.setDict(self.theDict) @@ -727,4 +723,15 @@ class GuiDocEditor(QTextEdit): self._findNext() return + def _setupSpellChecking(self): + """Create the spell checking object based on the spellTool + setting in config. + """ + if self.mainConf.spellTool == "enchant": + from nw.tools.spellenchant import NWSpellEnchant + self.theDict = NWSpellEnchant() + else: + self.theDict = NWSpellSimple() + return + # END Class GuiDocEditor