From d27e2dc60fbb86c05cc43232236f188da6c95705 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 30 May 2020 00:15:07 +0200 Subject: [PATCH] Added code to write contents txt and json files on project close --- nw/constants/constants.py | 4 ++-- nw/core/project.py | 48 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index cf0ab4eb..f538f2fe 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -39,8 +39,8 @@ class nwFiles(): PROJ_FILE = "nwProject.nwx" PROJ_DICT = "wordlist.txt" PROJ_LOCK = "nwProject.lock" - TOC_TXT = "ToC.txt" - TOC_JSON = "ToC.json" + TOC_TXT = "content.txt" + TOC_JSON = "content.json" SESS_INFO = "sessionInfo.log" INDEX_FILE = "tagsIndex.json" OPTS_FILE = "guiOptions.json" diff --git a/nw/core/project.py b/nw/core/project.py index 132066b3..f2abbcf4 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -31,6 +31,7 @@ """ import logging +import json import nw from os import path, mkdir, listdir, unlink, rename, rmdir @@ -566,6 +567,7 @@ class NWProject(): def closeProject(self): """Close the current project and clear all meta data. """ + self.projTree.writeToCFiles() self._appendSessionStats() self._clearLockFile() self.clearProject() @@ -1282,7 +1284,7 @@ class NWTree(): for tHandle in self._treeOrder: tItem = self.__getitem__(tHandle) tItem.packXML(xContent) - return + return def unpackXML(self, xContent): """Iterate through all items of a content XML object and add @@ -1300,6 +1302,50 @@ class NWTree(): return True + def writeToCFiles(self): + """Write the convenience table of contents files in the root of + the project directory. These files are there to assist the user + if they wish to browse the stored files. + """ + tocText = path.join(self.theProject.projPath, nwFiles.TOC_TXT) + tocJson = path.join(self.theProject.projPath, nwFiles.TOC_JSON) + + jsonData = [] + try: + # Dump the text + with open(tocText, mode="w", encoding="utf8") as outFile: + outFile.write("\n") + outFile.write(" Table of Contents\n") + outFile.write("===================\n") + outFile.write("\n") + outFile.write(" %-25s %-9s %s\n" %("File Name","Class","Document Label")) + outFile.write("-"*80+"\n") + for tHandle in sorted(self._treeOrder): + tItem = self.__getitem__(tHandle) + if tItem is None: + continue + tFile = tHandle+".nwd" + if path.isfile(path.join(self.theProject.projContent, tFile)): + outFile.write(" %-25s %-9s %s\n" %( + path.join("content", tFile), + tItem.itemClass.name, + tItem.itemName, + )) + jsonData.append([ + path.join("content", tFile), + tItem.itemClass.name, + tItem.itemName, + ]) + + # Dump the JSON + with open(tocJson, mode="w+", encoding="utf8") as outFile: + outFile.write(json.dumps(jsonData, indent=2)) + + except Exception as e: + logger.error(str(e)) + + return + ## # Tree Structure Methods ##