From 11545137106b3a3474c6a3050edc2f687af24656 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 14 Oct 2018 23:19:54 +0200 Subject: [PATCH] Started adding code for writing the main project file. --- nw/__init__.py | 2 +- nw/gui/projecteditor.py | 8 ++--- nw/project/project.py | 66 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/nw/__init__.py b/nw/__init__.py index da82b5e0..1476db51 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -155,7 +155,7 @@ def main(sysArgs): debugLevel = logging.INFO elif inArg == "DEBUG": debugLevel = logging.DEBUG - debugStr = "{name:>20}:{lineno:<4d} {levelname:8} {message:}" + debugStr = "{name:>22}:{lineno:<4d} {levelname:8} {message:}" else: print("Invalid debug level") exit(2) diff --git a/nw/gui/projecteditor.py b/nw/gui/projecteditor.py index 41fd4375..9655a6de 100644 --- a/nw/gui/projecteditor.py +++ b/nw/gui/projecteditor.py @@ -42,10 +42,10 @@ class GuiProjectEditor(QWidget): self.saveBox.addWidget(self.savePath) self.saveBox.addWidget(self.saveButton) - self.mainForm.addRow("Working Name", self.editName) - self.mainForm.addRow("Book Title", self.editTitle) - self.mainForm.addRow("Book Authors", self.editAuthors) - self.mainForm.addRow("Save Path", self.saveBox) + self.mainForm.addRow("Working Title", self.editName) + self.mainForm.addRow("Book Title", self.editTitle) + self.mainForm.addRow("Book Authors", self.editAuthors) + self.mainForm.addRow("Save Path", self.saveBox) self.mainGroup.setLayout(self.mainForm) self.outerBox.addWidget(self.mainGroup) diff --git a/nw/project/project.py b/nw/project/project.py index 24db0882..900775b5 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -13,7 +13,8 @@ import logging import nw -from os import path +from os import path, mkdir +from lxml import etree logger = logging.getLogger(__name__) @@ -24,10 +25,73 @@ class NWProject(): self.projFolder = None self.projTree = [] + # Project Settings + self.projPath = "" + self.projData = "" + self.projName = "" + self.bookTitle = "" + self.bookAuthors = [] + return def buildProjectTree(self): return True + def saveProject(self): + + projDir = path.dirname(self.projPath) + projFile = path.basename(self.projPath) + logger.vverbose("Project folder is %s" % projDir) + logger.vverbose("Project file is %s" % projFile) + + if bookFile[-4:] == ".nwx": + self.projData = path.join(projDir,projFile[:-4]+".nwd") + if not path.isdir(self.projData): + logger.info("Created folder %s" % self.projData) + mkdir(self.projData) + + # Root element and book details + nwXML = etree.Element("novelWriterXML",attrib={ + "fileVersion" : "1.0", + "appVersion" : str(nw.__version__), + "timeStamp" : getTimeStamp("-"), + }) + xBook = etree.SubElement(nwXML,"book") + xBookTitle = etree.SubElement(xBook,"title") + xBookTitle.text = self.bookTitle + for bookAuthor in self.bookAuthors: + if bookAuthor == "": continue + xBookAuthor = etree.SubElement(xBook,"author") + xBookAuthor.text = bookAuthor + + # Write the xml tree to file + with open(self.bookPath,"wb") as outFile: + outFile.write(etree.tostring( + nwXML, + pretty_print = True, + encoding = "utf-8", + xml_declaration = True + )) + + return True + + # + # Set Functions + # + + def setProjectName(self, projName): + self.projName = projName.strip() + return True + + def setBookTitle(self, bookTitle): + self.bookTitle = bookTitle.strip() + return True + + def setBookAuthors(self, bookAuthors): + self.bookAuthors = [] + for bookAuthor in bookAuthors.split(","): + self.bookAuthors.append(bookAuthor.strip()) + return True + # END Class NWProject