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:
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:])
+1 -6
View File
@@ -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
+33 -9
View File
@@ -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
+13 -6
View File
@@ -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