Merge pull request #81 from vkbo/windows_fixes

Windows fixes
This commit is contained in:
Veronica K. Berglyd Olsen
2019-10-28 14:23:26 +01:00
committed by GitHub
15 changed files with 56 additions and 34 deletions
+9 -6
View File
@@ -15,7 +15,7 @@ import configparser
import sys import sys
import nw import nw
from os import path, mkdir, getcwd from os import path, mkdir, makedirs, getcwd
from appdirs import user_config_dir from appdirs import user_config_dir
from datetime import datetime from datetime import datetime
@@ -166,9 +166,12 @@ class Config:
# If config folder does not exist, make it. # If config folder does not exist, make it.
# This assumes that the os config folder itself exists. # This assumes that the os config folder itself exists.
# TODO: This does not work on Windows if self.osWindows:
if not path.isdir(self.confPath): if not path.isdir(self.confPath):
mkdir(self.confPath) makedirs(self.confPath)
else:
if not path.isdir(self.confPath):
mkdir(self.confPath)
# Check if config file exists # Check if config file exists
if path.isfile(path.join(self.confPath,self.confFile)): if path.isfile(path.join(self.confPath,self.confFile)):
@@ -190,7 +193,7 @@ class Config:
logger.debug("Loading config file") logger.debug("Loading config file")
cnfParse = configparser.ConfigParser() cnfParse = configparser.ConfigParser()
try: try:
cnfParse.read_file(open(path.join(self.confPath,self.confFile))) cnfParse.read_file(open(path.join(self.confPath,self.confFile),mode="r",encoding="utf8"))
except Exception as e: except Exception as e:
logger.error("Could not load config file") logger.error("Could not load config file")
return False return False
@@ -312,7 +315,7 @@ class Config:
# Write config file # Write config file
try: try:
cnfParse.write(open(path.join(self.confPath,self.confFile),"w")) cnfParse.write(open(path.join(self.confPath,self.confFile),mode="w",encoding="utf8"))
self.confChanged = False self.confChanged = False
except Exception as e: except Exception as e:
logger.error("Could not save config file") logger.error("Could not save config file")
+1 -1
View File
@@ -63,7 +63,7 @@ class ConcatFile(TextFile):
def _doOpenFile(self, filePath): def _doOpenFile(self, filePath):
try: try:
self.outFile = open(filePath,mode="wt+") self.outFile = open(filePath,mode="wt+",encoding="utf8")
except Exception as e: except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR) self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False return False
+1 -1
View File
@@ -34,7 +34,7 @@ class HtmlFile(TextFile):
def _doOpenFile(self, filePath): def _doOpenFile(self, filePath):
try: try:
self.outFile = open(filePath,mode="wt+") self.outFile = open(filePath,mode="wt+",encoding="utf8")
self.outFile.write("<!DOCTYPE html>\n") self.outFile.write("<!DOCTYPE html>\n")
self.outFile.write("<html>\n") self.outFile.write("<html>\n")
self.outFile.write("<head>\n") self.outFile.write("<head>\n")
+1 -1
View File
@@ -34,7 +34,7 @@ class LaTeXFile(TextFile):
def _doOpenFile(self, filePath): def _doOpenFile(self, filePath):
try: try:
self.outFile = open(filePath,mode="wt+") self.outFile = open(filePath,mode="wt+",encoding="utf8")
self.outFile.write("\\documentclass[12pt]{report}\n") self.outFile.write("\\documentclass[12pt]{report}\n")
self.outFile.write("\\usepackage[utf8]{inputenc}\n") self.outFile.write("\\usepackage[utf8]{inputenc}\n")
self.outFile.write("\n") self.outFile.write("\n")
+1 -1
View File
@@ -34,7 +34,7 @@ class MarkdownFile(TextFile):
def _doOpenFile(self, filePath): def _doOpenFile(self, filePath):
try: try:
self.outFile = open(filePath,mode="wt+") self.outFile = open(filePath,mode="wt+",encoding="utf8")
except Exception as e: except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR) self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False return False
+1 -1
View File
@@ -153,7 +153,7 @@ class TextFile():
that uses a different file format that requires a different approach. that uses a different file format that requires a different approach.
""" """
try: try:
self.outFile = open(filePath,mode="wt+") self.outFile = open(filePath,mode="wt+",encoding="utf8")
self.outFile.write("\n\n") self.outFile.write("\n\n")
except Exception as e: except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR) self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
+13 -1
View File
@@ -162,9 +162,21 @@ class GuiDocEditor(QTextEdit):
return True return True
def loadText(self, tHandle): def loadText(self, tHandle):
"""Load text from a document into the editor. If we have an io error, we must handle this
and clear the editor so that we don't risk overwriting the file if it exists. This can for
instance happen of the file contains binary elements or an encoding that novelWriter does
not support. If load is successful, ot the document is new (empty string) we set up the
editor for editing the file.
"""
theDoc = self.nwDocument.openDocument(tHandle)
if theDoc is None:
# There was an io error
self.clearEditor()
return False
self.hLight.setHandle(tHandle) self.hLight.setHandle(tHandle)
self.setPlainText(self.nwDocument.openDocument(tHandle)) self.setPlainText(theDoc)
self.setCursorPosition(self.nwDocument.theItem.cursorPos) self.setCursorPosition(self.nwDocument.theItem.cursorPos)
self.lastEdit = time() self.lastEdit = time()
self._runCounter() self._runCounter()
+1 -1
View File
@@ -147,7 +147,7 @@ class GuiSessionLogView(QDialog):
logger.debug("Loading session log file") logger.debug("Loading session log file")
try: try:
with open(logFile,mode="r") as inFile: with open(logFile,mode="r",encoding="utf8") as inFile:
for inLine in inFile: for inLine in inFile:
inData = inLine.split() inData = inLine.split()
if len(inData) != 8: if len(inData) != 8:
+7 -5
View File
@@ -332,10 +332,12 @@ class GuiMain(QMainWindow):
def openDocument(self, tHandle): def openDocument(self, tHandle):
if self.hasProject: if self.hasProject:
self.closeDocument() self.closeDocument()
self.docEditor.loadText(tHandle) if self.docEditor.loadText(tHandle):
self.docEditor.setFocus() self.docEditor.setFocus()
self.docEditor.changeWidth() self.docEditor.changeWidth()
self.theProject.setLastEdited(tHandle) self.theProject.setLastEdited(tHandle)
else:
return False
return True return True
def saveDocument(self): def saveDocument(self):
@@ -390,7 +392,7 @@ class GuiMain(QMainWindow):
theText = None theText = None
try: try:
with open(loadFile,mode="rt") as inFile: with open(loadFile,mode="rt",encoding="utf8") as inFile:
theText = inFile.read() theText = inFile.read()
except Exception as e: except Exception as e:
self.makeAlert(["Could not read file. The file cannot be a binary file.",str(e)], nwAlert.ERROR) self.makeAlert(["Could not read file. The file cannot be a binary file.",str(e)], nwAlert.ERROR)
+8 -3
View File
@@ -53,12 +53,17 @@ class NWDoc():
if path.isfile(docPath): if path.isfile(docPath):
try: try:
with open(docPath,mode="r") as inFile: with open(docPath,mode="r",encoding="utf8") as inFile:
theDoc = inFile.read() theDoc = inFile.read()
except Exception as e: except Exception as e:
self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR) self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR)
return "" # Note: Document must be cleared in case of an io error, or else the auto-save or
# save will try to overwrite it with an empty file. Return None to alert the caller.
self.clearDocument()
return None
else: else:
# The document file does not exist, so we assume it's a new document and initialise an
# empty text string.
logger.debug("The requested document does not exist.") logger.debug("The requested document does not exist.")
return "" return ""
@@ -89,7 +94,7 @@ class NWDoc():
if path.isfile(docPath): rename(docPath,docBack) if path.isfile(docPath): rename(docPath,docBack)
try: try:
with open(docPath,mode="w") as outFile: with open(docPath,mode="w",encoding="utf8") as outFile:
outFile.write(docText) outFile.write(docText)
except Exception as e: except Exception as e:
self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR)
+2 -2
View File
@@ -91,7 +91,7 @@ class NWIndex():
if path.isfile(indexFile): if path.isfile(indexFile):
logger.debug("Loading index file") logger.debug("Loading index file")
try: try:
with open(indexFile,mode="r") as inFile: with open(indexFile,mode="r",encoding="utf8") as inFile:
theJson = inFile.read() theJson = inFile.read()
theData = json.loads(theJson) theData = json.loads(theJson)
except Exception as e: except Exception as e:
@@ -119,7 +119,7 @@ class NWIndex():
else: else:
nIndent = None nIndent = None
try: try:
with open(indexFile,mode="w+") as outFile: with open(indexFile,mode="w+",encoding="utf8") as outFile:
outFile.write(json.dumps({ outFile.write(json.dumps({
"tagIndex" : self.tagIndex, "tagIndex" : self.tagIndex,
"refIndex" : self.refIndex, "refIndex" : self.refIndex,
+2 -2
View File
@@ -328,7 +328,7 @@ class NWProject():
# Write the xml tree to file # Write the xml tree to file
saveFile = path.join(self.projPath,self.projFile) saveFile = path.join(self.projPath,self.projFile)
try: try:
with open(saveFile,"wb") as outFile: with open(saveFile,mode="wb") as outFile:
outFile.write(etree.tostring( outFile.write(etree.tostring(
nwXML, nwXML,
pretty_print = True, pretty_print = True,
@@ -657,7 +657,7 @@ class NWProject():
if self.projMeta is None: if self.projMeta is None:
return False return False
with open(path.join(self.projMeta, nwFiles.SESS_INFO), mode="a+") as outFile: with open(path.join(self.projMeta, nwFiles.SESS_INFO),mode="a+",encoding="utf8") as outFile:
print(( print((
"Start: {opened:s} " "Start: {opened:s} "
"End: {closed:s} " "End: {closed:s} "
+5 -5
View File
@@ -137,7 +137,7 @@ class Theme:
cssData = "" cssData = ""
try: try:
if path.isfile(self.cssFile): if path.isfile(self.cssFile):
with open(self.cssFile,mode="r") as inFile: with open(self.cssFile,mode="r",encoding="utf8") as inFile:
cssData = inFile.read() cssData = inFile.read()
except Exception as e: except Exception as e:
logger.error("Could not load theme css file") logger.error("Could not load theme css file")
@@ -154,7 +154,7 @@ class Theme:
# Config File # Config File
confParser = configparser.ConfigParser() confParser = configparser.ConfigParser()
try: try:
confParser.read_file(open(self.confFile)) confParser.read_file(open(self.confFile,mode="r",encoding="utf8"))
except Exception as e: except Exception as e:
logger.error("Could not load theme settings from: %s" % self.confFile) logger.error("Could not load theme settings from: %s" % self.confFile)
return False return False
@@ -205,7 +205,7 @@ class Theme:
confParser = configparser.ConfigParser() confParser = configparser.ConfigParser()
try: try:
confParser.read_file(open(self.syntaxFile)) confParser.read_file(open(self.syntaxFile,mode="r",encoding="utf8"))
except Exception as e: except Exception as e:
logger.error("Could not load syntax colours from: %s" % self.syntaxFile) logger.error("Could not load syntax colours from: %s" % self.syntaxFile)
return False return False
@@ -251,7 +251,7 @@ class Theme:
themeConf = path.join(self.mainConf.themeRoot, self.guiPath, themeDir, self.confName) themeConf = path.join(self.mainConf.themeRoot, self.guiPath, themeDir, self.confName)
logger.verbose("Checking theme config for '%s'" % themeDir) logger.verbose("Checking theme config for '%s'" % themeDir)
try: try:
confParser.read_file(open(themeConf)) confParser.read_file(open(themeConf,mode="r",encoding="utf8"))
except Exception as e: except Exception as e:
self.theParent.makeAlert(["Could not load theme config file",str(e)],nwAlert.ERROR) self.theParent.makeAlert(["Could not load theme config file",str(e)],nwAlert.ERROR)
continue continue
@@ -279,7 +279,7 @@ class Theme:
continue continue
logger.verbose("Checking theme syntax for '%s'" % syntaxFile) logger.verbose("Checking theme syntax for '%s'" % syntaxFile)
try: try:
confParser.read_file(open(syntaxPath)) confParser.read_file(open(syntaxPath,moe="r",encoding="utf8"))
except Exception as e: except Exception as e:
self.theParent.makeAlert(["Could not load syntax file",str(e)],nwAlert.ERROR) self.theParent.makeAlert(["Could not load syntax file",str(e)],nwAlert.ERROR)
return [] return []
+2 -2
View File
@@ -37,7 +37,7 @@ class OptLastState():
if path.isfile(stateFile): if path.isfile(stateFile):
logger.debug("Loading options file") logger.debug("Loading options file")
try: try:
with open(stateFile,mode="r") as inFile: with open(stateFile,mode="r",encoding="utf8") as inFile:
theJson = inFile.read() theJson = inFile.read()
theState = json.loads(theJson) theState = json.loads(theJson)
except Exception as e: except Exception as e:
@@ -52,7 +52,7 @@ class OptLastState():
stateFile = path.join(self.theProject.projMeta,self.theFile) stateFile = path.join(self.theProject.projMeta,self.theFile)
logger.debug("Saving options file") logger.debug("Saving options file")
try: try:
with open(stateFile,mode="w+") as outFile: with open(stateFile,mode="w+",encoding="utf8") as outFile:
outFile.write(json.dumps(self.theState, indent=2)) outFile.write(json.dumps(self.theState, indent=2))
except Exception as e: except Exception as e:
logger.error("Failed to save options file") logger.error("Failed to save options file")
+2 -2
View File
@@ -13,13 +13,13 @@ def ensureDir(theDir):
def cmpFiles(fileOne, fileTwo, ignoreLines=[]): def cmpFiles(fileOne, fileTwo, ignoreLines=[]):
try: try:
foOne = open(fileOne,mode="r") foOne = open(fileOne,mode="r",encoding="utf8")
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
return False return False
try: try:
foTwo = open(fileTwo,mode="r") foTwo = open(fileTwo,mode="r",encoding="utf8")
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
return False return False