Added some error handling for export file, and improved getting and setting of export options

This commit is contained in:
Veronica K. B. Olsen
2019-10-19 11:32:11 +02:00
parent 287d94369f
commit d325e671dd
3 changed files with 45 additions and 48 deletions
+10 -2
View File
@@ -94,6 +94,9 @@ class TextFile():
self._doOpenFile(filePath) self._doOpenFile(filePath)
if self.outFile is None:
return False
return True return True
def closeFile(self): def closeFile(self):
@@ -125,7 +128,7 @@ class TextFile():
if self.winEnding: if self.winEnding:
self.theConv.windowsEndings() self.theConv.windowsEndings()
if self.theConv.theResult is not None: if self.theConv.theResult is not None and self.outFile is not None:
self.outFile.write(self.theConv.theResult) self.outFile.write(self.theConv.theResult)
return True return True
@@ -140,6 +143,10 @@ class TextFile():
""" """
try: try:
self.outFile = open(filePath,mode="w+") self.outFile = open(filePath,mode="w+")
if self.winEnding:
self.outFile.write("\r\n\r\n")
else:
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)
return False return False
@@ -149,7 +156,8 @@ class TextFile():
"""This function closes a file, and is meant to be overloaded by the subclass for other """This function closes a file, and is meant to be overloaded by the subclass for other
file formats. file formats.
""" """
self.outFile.close() if self.outFile is not None:
self.outFile.close()
return True return True
# END Class OutFile # END Class OutFile
+5 -3
View File
@@ -221,7 +221,9 @@ class Tokenizer():
elif tType == self.T_HEAD3: elif tType == self.T_HEAD3:
tTemp = self._doFormatScene(tText) tTemp = self._doFormatScene(tText)
if tTemp == self.fmtScene: if tTemp == "":
self.theTokens[n] = (self.T_EMPTY,"",None,self.A_LEFT)
elif tTemp == self.fmtScene:
if self.firstScene: if self.firstScene:
self.theTokens[n] = (self.T_EMPTY,"",None,self.A_LEFT) self.theTokens[n] = (self.T_EMPTY,"",None,self.A_LEFT)
else: else:
@@ -231,10 +233,10 @@ class Tokenizer():
self.firstScene = False self.firstScene = False
elif tType == self.T_HEAD4: elif tType == self.T_HEAD4:
if self.noSection: tTemp = self._doFormatSection(tText)
if tTemp == "":
self.theTokens[n] = (self.T_EMPTY,"",None,self.A_LEFT) self.theTokens[n] = (self.T_EMPTY,"",None,self.A_LEFT)
else: else:
tTemp = self._doFormatSection(tText)
self.theTokens[n] = (self.T_SEP,tTemp,None,self.A_LEFT) self.theTokens[n] = (self.T_SEP,tTemp,None,self.A_LEFT)
# For title page and partitions, we need to centre all text # For title page and partitions, we need to centre all text
+30 -43
View File
@@ -120,10 +120,13 @@ class GuiExport(QDialog):
if outFile is None: if outFile is None:
return False return False
outFile.openFile(saveTo,"testfile") if outFile.openFile(saveTo,"testfile"):
outFile.setComments(wComments) outFile.setComments(wComments)
outFile.setExportNovel(wNovel) outFile.setExportNovel(wNovel)
outFile.setExportNotes(wNotes) outFile.setExportNotes(wNotes)
else:
self.exportStatus.setText("Failed to open file for writing ...")
return False
nDone = 0 nDone = 0
for tHandle in self.theProject.treeOrder: for tHandle in self.theProject.treeOrder:
@@ -233,9 +236,9 @@ class GuiExportMain(QWidget):
self.expNovel.setToolTip("Include all novel files in the exported document") self.expNovel.setToolTip("Include all novel files in the exported document")
self.expNotes.setToolTip("Include all note files in the exported document") self.expNotes.setToolTip("Include all note files in the exported document")
self.expTOC.setToolTip("Generate a Table of Contents (ToC)") self.expTOC.setToolTip("Generate a Table of Contents (ToC)")
self.expNovel.setChecked(self.optState.wNovel()) self.expNovel.setChecked(self.optState.getSetting("wNovel"))
self.expNotes.setChecked(self.optState.wNotes()) self.expNotes.setChecked(self.optState.getSetting("wNotes"))
self.expTOC.setChecked(self.optState.wTOC()) self.expTOC.setChecked(self.optState.getSetting("wTOC"))
self.guiFilesForm.addWidget(QLabel("Novel files"), 0, 0) self.guiFilesForm.addWidget(QLabel("Novel files"), 0, 0)
self.guiFilesForm.addWidget(self.expNovel, 0, 1) self.guiFilesForm.addWidget(self.expNovel, 0, 1)
@@ -245,16 +248,16 @@ class GuiExportMain(QWidget):
self.guiFilesForm.addWidget(self.expTOC, 2, 1) self.guiFilesForm.addWidget(self.expTOC, 2, 1)
# Chapter Settings # Chapter Settings
self.guiChapters = QGroupBox("Chapters", self) self.guiChapters = QGroupBox("Chapter Heading", self)
self.guiChaptersForm = QGridLayout(self) self.guiChaptersForm = QGridLayout(self)
self.guiChapters.setLayout(self.guiChaptersForm) self.guiChapters.setLayout(self.guiChaptersForm)
self.chapterFormat = QLineEdit() self.chapterFormat = QLineEdit()
self.chapterFormat.setText(self.optState.chFormat()) self.chapterFormat.setText(self.optState.getSetting("chFormat"))
self.chapterFormat.setToolTip("Available formats: %num%, %numword%, %title%, %label%") self.chapterFormat.setToolTip("Available formats: %num%, %numword%, %title%, %label%")
self.chapterFormat.setMinimumWidth(250) self.chapterFormat.setMinimumWidth(250)
self.guiChaptersForm.addWidget(QLabel("Format"), 0, 0) self.guiChaptersForm.addWidget(QLabel("Numbered"), 0, 0)
self.guiChaptersForm.addWidget(self.chapterFormat, 0, 1) self.guiChaptersForm.addWidget(self.chapterFormat, 0, 1)
# Output Format # Output Format
@@ -263,7 +266,7 @@ class GuiExportMain(QWidget):
self.guiOutput.setLayout(self.guiOutputForm) self.guiOutput.setLayout(self.guiOutputForm)
self.outputComments = QCheckBox("include comments", self) self.outputComments = QCheckBox("include comments", self)
self.outputComments.setChecked(self.optState.wComments()) self.outputComments.setChecked(self.optState.getSetting("wComments"))
self.outputHelp = QLabel("") self.outputHelp = QLabel("")
self.outputHelp.setWordWrap(True) self.outputHelp.setWordWrap(True)
@@ -279,7 +282,7 @@ class GuiExportMain(QWidget):
# self.outputFormat.addItem("LaTeX (PDF)", self.FMT_TEX) # self.outputFormat.addItem("LaTeX (PDF)", self.FMT_TEX)
self.outputFormat.currentIndexChanged.connect(self._updateFormatHelp) self.outputFormat.currentIndexChanged.connect(self._updateFormatHelp)
optIdx = self.outputFormat.findData(self.optState.eFormat()) optIdx = self.outputFormat.findData(self.optState.getSetting("eFormat"))
if optIdx != -1: if optIdx != -1:
self.outputFormat.setCurrentIndex(optIdx) self.outputFormat.setCurrentIndex(optIdx)
self._updateFormatHelp(optIdx) self._updateFormatHelp(optIdx)
@@ -296,7 +299,7 @@ class GuiExportMain(QWidget):
self.guiScenes.setLayout(self.guiScenesForm) self.guiScenes.setLayout(self.guiScenesForm)
self.sceneFormat = QLineEdit() self.sceneFormat = QLineEdit()
self.sceneFormat.setText(self.optState.scFormat()) self.sceneFormat.setText(self.optState.getSetting("scFormat"))
self.sceneFormat.setToolTip("Available formats: %title%") self.sceneFormat.setToolTip("Available formats: %title%")
self.sceneFormat.setMinimumWidth(100) self.sceneFormat.setMinimumWidth(100)
@@ -308,7 +311,7 @@ class GuiExportMain(QWidget):
self.exportToForm = QGridLayout(self) self.exportToForm = QGridLayout(self)
self.exportTo.setLayout(self.exportToForm) self.exportTo.setLayout(self.exportToForm)
self.exportPath = QLineEdit(self.optState.saveTo()) self.exportPath = QLineEdit(self.optState.getSetting("saveTo"))
self.exportGetPath = QPushButton(self.theTheme.getIcon("folder"),"") self.exportGetPath = QPushButton(self.theTheme.getIcon("folder"),"")
self.exportGetPath.clicked.connect(self._exportFolder) self.exportGetPath.clicked.connect(self._exportFolder)
@@ -368,7 +371,6 @@ class GuiExportMain(QWidget):
class ExportLastState(): class ExportLastState():
def __init__(self, theProject): def __init__(self, theProject):
self.theProject = theProject self.theProject = theProject
self.theState = { self.theState = {
"wNovel" : True, "wNovel" : True,
@@ -377,15 +379,18 @@ class ExportLastState():
"eFormat" : 2, "eFormat" : 2,
"wComments" : False, "wComments" : False,
"chFormat" : "Chapter %numword%", "chFormat" : "Chapter %numword%",
"unFormat" : "%title%",
"scFormat" : "* * *", "scFormat" : "* * *",
"seFormat" : "",
"saveTo" : "", "saveTo" : "",
} }
self.stringOpt = ("chFormat","unFormat","scFormat","seFormat","saveTo")
self.boolOpt = ("wNovel","wNotes","wTOC","wComments")
self.intOpt = ("eFormat")
self.loadSettings() self.loadSettings()
return return
def loadSettings(self): def loadSettings(self):
stateFile = path.join(self.theProject.projMeta, nwFiles.EXPORT_OPT) stateFile = path.join(self.theProject.projMeta, nwFiles.EXPORT_OPT)
if path.isfile(stateFile): if path.isfile(stateFile):
logger.debug("Loading export options file") logger.debug("Loading export options file")
@@ -397,11 +402,9 @@ class ExportLastState():
logger.error("Failed to load export options file") logger.error("Failed to load export options file")
logger.error(str(e)) logger.error(str(e))
return False return False
return True return True
def saveSettings(self): def saveSettings(self):
stateFile = path.join(self.theProject.projMeta, nwFiles.EXPORT_OPT) stateFile = path.join(self.theProject.projMeta, nwFiles.EXPORT_OPT)
logger.debug("Saving export options file") logger.debug("Saving export options file")
try: try:
@@ -411,7 +414,6 @@ class ExportLastState():
logger.error("Failed to save export options file") logger.error("Failed to save export options file")
logger.error(str(e)) logger.error(str(e))
return False return False
return True return True
def setSetting(self, setName, setValue): def setSetting(self, setName, setValue):
@@ -421,28 +423,13 @@ class ExportLastState():
return False return False
return True return True
def wNovel(self): def getSetting(self, setName):
return checkBool(self.theState["wNovel"],False,False) if setName in self.stringOpt:
return checkString(self.theState[setName],self.theState[setName],False)
def wNotes(self): elif setName in self.boolOpt:
return checkBool(self.theState["wNotes"],False,False) return checkBool(self.theState[setName],self.theState[setName],False)
elif setName in self.intOpt:
def wTOC(self): return checkInt(self.theState[setName],self.theState[setName],False)
return checkBool(self.theState["wTOC"],False,False) return None
def eFormat(self):
return checkInt(self.theState["eFormat"],1,False)
def wComments(self):
return checkBool(self.theState["wComments"],False,False)
def chFormat(self):
return checkString(self.theState["chFormat"],"Chapter %numword%",False)
def scFormat(self):
return checkString(self.theState["scFormat"],"* * *",False)
def saveTo(self):
return checkString(self.theState["saveTo"],"",False)
# END Class ExportLastState # END Class ExportLastState