Cleaned up initialisation and config settings for spell checking

This commit is contained in:
Veronica K. B. Olsen
2019-11-06 11:45:17 +01:00
parent 9a8b97abd6
commit f5af3fb293
4 changed files with 53 additions and 38 deletions
+6 -17
View File
@@ -5,7 +5,7 @@ import sys
if sys.hexversion < 0x030500F0: if sys.hexversion < 0x030500F0:
print("ERROR: At least Python 3.5 is required") print("ERROR: At least Python 3.5 is required")
exit(1) sys.exit(1)
try: try:
import PyQt5.QtWidgets import PyQt5.QtWidgets
@@ -13,37 +13,26 @@ try:
import PyQt5.QtCore import PyQt5.QtCore
except: except:
print("ERROR: Failed to load dependency python3-pyqt5") print("ERROR: Failed to load dependency python3-pyqt5")
exit(1) sys.exit(1)
try: try:
import PyQt5.QtSvg import PyQt5.QtSvg
except: except:
print("ERROR: Failed to load dependency python3-pyqt5.qtsvg") print("ERROR: Failed to load dependency python3-pyqt5.qtsvg")
exit(1) sys.exit(1)
try: try:
import lxml import lxml
except: except:
print("ERROR: Failed to load dependency python3-lxml") print("ERROR: Failed to load dependency python3-lxml")
exit(1) sys.exit(1)
try: try:
import appdirs import appdirs
except: except:
print("ERROR: Failed to load dependency python3-appdirs") print("ERROR: Failed to load dependency python3-appdirs")
exit(1) sys.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")
if __name__ == "__main__": if __name__ == "__main__":
import nw import nw
inArgs = sys.argv[1:] nw.main(sys.argv[1:])
if spellPack is not None:
inArgs.append("--spell=%s" % spellPack)
nw.main(inArgs)
+1 -6
View File
@@ -89,7 +89,6 @@ def main(sysArgs=None):
"version", "version",
"config=", "config=",
"testmode", "testmode",
"spell=",
"style=", "style=",
] ]
@@ -106,7 +105,7 @@ def main(sysArgs=None):
" -q, --quiet Disable output to command line. Does not affect log file.\n" " -q, --quiet Disable output to command line. Does not affect log file.\n"
" -t, --time Shows time stamp in logging output.\n" " -t, --time Shows time stamp in logging output.\n"
" -l, --logfile= Specify log file.\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" " --config= Alternative config file.\n"
" --headless Do not display GUI. Useful for testing scripts.\n" " --headless Do not display GUI. Useful for testing scripts.\n"
).format( ).format(
@@ -126,7 +125,6 @@ def main(sysArgs=None):
showTime = False showTime = False
confPath = None confPath = None
testMode = False testMode = False
spellTool = None
qtStyle = "Fusion" qtStyle = "Fusion"
# Parse Options # Parse Options
@@ -163,13 +161,10 @@ def main(sysArgs=None):
confPath = inArg confPath = inArg
elif inOpt in ("--testmode"): elif inOpt in ("--testmode"):
testMode = True testMode = True
elif inOpt in ("--spell"):
spellTool = inArg
# Set Config Options # Set Config Options
CONFIG.showGUI = not testMode CONFIG.showGUI = not testMode
CONFIG.debugInfo = debugLevel < logging.INFO CONFIG.debugInfo = debugLevel < logging.INFO
CONFIG.spellTool = spellTool
# Set Logging # Set Logging
if showTime: debugStr = timeStr+debugStr if showTime: debugStr = timeStr+debugStr
+33 -9
View File
@@ -41,7 +41,6 @@ class Config:
self.appHandle = nw.__package__.lower() self.appHandle = nw.__package__.lower()
self.showGUI = True self.showGUI = True
self.debugInfo = False self.debugInfo = False
self.spellTool = None
# Set Paths # Set Paths
self.confPath = None self.confPath = None
@@ -94,7 +93,8 @@ class Config:
self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO] self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO]
self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO] self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO]
self.spellLanguage = "en_GB" self.spellTool = None
self.spellLanguage = None
## Backup ## Backup
self.backupPath = "" self.backupPath = ""
@@ -137,9 +137,9 @@ class Config:
self.osDarwin = False self.osDarwin = False
self.osUnknown = False self.osUnknown = False
if self.osType.startswith("linux"): if self.osType.startswith("linux"):
self.osLinux = True self.osLinux = True
elif self.osType.startswith("darwin"): elif self.osType.startswith("darwin"):
self.osDarwin = True self.osDarwin = True
elif self.osType.startswith("win32"): elif self.osType.startswith("win32"):
self.osWindows = True self.osWindows = True
elif self.osType.startswith("cygwin"): elif self.osType.startswith("cygwin"):
@@ -147,6 +147,9 @@ class Config:
else: else:
self.osUnknown = True self.osUnknown = True
# Packages
self.hasEnchant = False
return return
## ##
@@ -186,13 +189,16 @@ class Config:
# If it exists, load it # If it exists, load it
self.loadConfig() self.loadConfig()
else: 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() self.saveConfig()
# Check the availability of optional packages
self._checkOptionalPackages()
if self.spellTool is None: if self.spellTool is None:
logger.warning("No spell check tool available") self.spellTool = "simple"
else: if self.spellLanguage is None:
logger.debug("Using spell check tool '%s'" % self.spellTool) self.spellLanguage = "en"
return True return True
@@ -288,8 +294,11 @@ class Config:
self.fmtDoubleQuotes = self._parseLine( self.fmtDoubleQuotes = self._parseLine(
cnfParse, cnfSec, "fmtdoublequote", self.CNF_LIST, self.fmtDoubleQuotes cnfParse, cnfSec, "fmtdoublequote", self.CNF_LIST, self.fmtDoubleQuotes
) )
self.spellTool = self._parseLine(
cnfParse, cnfSec, "spelltool", self.CNF_STR, self.spellTool
)
self.spellLanguage = self._parseLine( self.spellLanguage = self._parseLine(
cnfParse, cnfSec, "spellcheck", self.CNF_STR, self.spellLanguage cnfParse, cnfSec, "spellcheck", self.CNF_STR, self.spellLanguage
) )
self.showTabsNSpaces = self._parseLine( self.showTabsNSpaces = self._parseLine(
cnfParse, cnfSec, "showtabsnspaces", self.CNF_BOOL, self.showTabsNSpaces cnfParse, cnfSec, "showtabsnspaces", self.CNF_BOOL, self.showTabsNSpaces
@@ -380,6 +389,7 @@ class Config:
cnfParse.set(cnfSec,"repdots", str(self.doReplaceDots)) cnfParse.set(cnfSec,"repdots", str(self.doReplaceDots))
cnfParse.set(cnfSec,"fmtsinglequote", self._packList(self.fmtSingleQuotes)) cnfParse.set(cnfSec,"fmtsinglequote", self._packList(self.fmtSingleQuotes))
cnfParse.set(cnfSec,"fmtdoublequote", self._packList(self.fmtDoubleQuotes)) 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,"spellcheck", str(self.spellLanguage))
cnfParse.set(cnfSec,"showtabsnspaces", str(self.showTabsNSpaces)) cnfParse.set(cnfSec,"showtabsnspaces", str(self.showTabsNSpaces))
cnfParse.set(cnfSec,"showlineendings", str(self.showLineEndings)) cnfParse.set(cnfSec,"showlineendings", str(self.showLineEndings))
@@ -520,4 +530,18 @@ class Config:
return None return None
return checkVal 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 # End Class Config
+13 -6
View File
@@ -26,7 +26,7 @@ from PyQt5.QtGui import (
from nw.project import NWDoc from nw.project import NWDoc
from nw.gui.tools import GuiDocHighlighter, WordCounter 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 from nw.constants import nwFiles, nwUnicode, nwDocAction, nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -64,11 +64,7 @@ class GuiDocEditor(QTextEdit):
self.qDocument = self.document() self.qDocument = self.document()
self.qDocument.setDocumentMargin(self.mainConf.textMargin) self.qDocument.setDocumentMargin(self.mainConf.textMargin)
self.qDocument.contentsChange.connect(self._docChange) self.qDocument.contentsChange.connect(self._docChange)
if self.mainConf.spellTool == "enchant": self._setupSpellChecking()
from nw.tools.spellenchant import NWSpellEnchant
self.theDict = NWSpellEnchant()
else:
self.theDict = NWSpellCheck()
self.hLight = GuiDocHighlighter(self.qDocument, self.theParent) self.hLight = GuiDocHighlighter(self.qDocument, self.theParent)
self.hLight.setDict(self.theDict) self.hLight.setDict(self.theDict)
@@ -727,4 +723,15 @@ class GuiDocEditor(QTextEdit):
self._findNext() self._findNext()
return 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 # END Class GuiDocEditor