Beispiel #1
0
 def newFolder(self, folderName, folderClass, pHandle):
     newItem = NWItem(self)
     newItem.setName(folderName)
     newItem.setType(nwItemType.FOLDER)
     newItem.setClass(folderClass)
     newItem.setStatus(0)
     self._appendItem(None, pHandle, newItem)
     return newItem.itemHandle
Beispiel #2
0
 def newRoot(self, rootName, rootClass):
     if not self.checkRootUnique(rootClass):
         self.makeAlert("Duplicate root item detected!", nwAlert.ERROR)
         return None
     newItem = NWItem(self)
     newItem.setName(rootName)
     newItem.setType(nwItemType.ROOT)
     newItem.setClass(rootClass)
     newItem.setStatus(0)
     self._appendItem(None, None, newItem)
     return newItem.itemHandle
Beispiel #3
0
    def _scanProjectFolder(self):

        if self.projPath is None:
            return

        # First, scan the project data folders
        itemList = []
        for subItem in listdir(self.projPath):
            if subItem[:5] != "data_":
                continue
            dataDir = path.join(self.projPath, subItem)
            for subFile in listdir(dataDir):
                if subFile[-4:] == ".nwd":
                    newItem = path.join(subItem, subFile)
                    itemList.append(newItem)

        # Then check the valid files
        orphanFiles = []
        for fileItem in itemList:
            if len(fileItem) != 28:
                # Just to be safe, shouldn't happen
                logger.warning("Skipping file %s" % fileItem)
                continue
            fHandle = fileItem[5] + fileItem[7:19]
            if fHandle in self.treeOrder:
                logger.debug("Checking file %s, handle %s: OK" %
                             (fileItem, fHandle))
            else:
                logger.debug("Checking file %s, handle %s: Orphaned" %
                             (fileItem, fHandle))
                orphanFiles.append(fHandle)

        # Report status
        if len(orphanFiles) > 0:
            self.makeAlert(
                "Found %d orphaned file(s) in project folder!" %
                len(orphanFiles), nwAlert.WARN)
        else:
            logger.debug("File check OK")
            return

        # Handle orphans
        nOrph = 0
        for oHandle in orphanFiles:
            nOrph += 1
            orItem = NWItem(self)
            orItem.setName("Orphaned File %d" % nOrph)
            orItem.setType(nwItemType.FILE)
            orItem.setClass(nwItemClass.NO_CLASS)
            orItem.setLayout(nwItemLayout.NO_LAYOUT)
            self._appendItem(oHandle, None, orItem)

        return
Beispiel #4
0
 def addTrash(self):
     newItem = NWItem(self)
     newItem.setName("Trash")
     newItem.setType(nwItemType.TRASH)
     newItem.setClass(nwItemClass.TRASH)
     self._appendItem(None, None, newItem)
     return newItem.itemHandle
Beispiel #5
0
    def openProject(self, fileName):
        """Open the project file provided, or if doesn't exist, assume
        it is a folder, and look for the file within it. If successful,
        parse the XML of the file and populate the project variables and
        build the tree of project items.
        """

        if not path.isfile(fileName):
            fileName = path.join(fileName, nwFiles.PROJ_FILE)
            if not path.isfile(fileName):
                self.makeAlert("File not found: %s" % fileName, nwAlert.ERROR)
                return False

        self.clearProject()
        self.projPath = path.abspath(path.dirname(fileName))
        logger.debug("Opening project: %s" % self.projPath)

        self.projMeta = path.join(self.projPath, "meta")
        self.projDict = path.join(self.projMeta, nwFiles.PROJ_DICT)

        if not self._checkFolder(self.projMeta):
            return

        try:
            projectMaintenance(self)
        except Exception as E:
            logger.error(str(E))

        try:
            nwXML = etree.parse(fileName)
        except Exception as e:
            self.makeAlert(["Failed to parse project xml.",
                            str(e)], nwAlert.ERROR)

            # Trying to open backup file instead
            backFile = fileName[:-3] + "bak"
            if path.isfile(backFile):
                self.makeAlert(
                    "Attempting to open backup project file instead.",
                    nwAlert.INFO)
                try:
                    nwXML = etree.parse(backFile)
                except Exception as e:
                    self.makeAlert(["Failed to parse project xml.",
                                    str(e)], nwAlert.ERROR)
                    self.clearProject()
                    return False
            else:
                self.clearProject()
                return False

        xRoot = nwXML.getroot()
        nwxRoot = xRoot.tag

        appVersion = xRoot.attrib["appVersion"]
        fileVersion = xRoot.attrib["fileVersion"]

        logger.verbose("XML root is %s" % nwxRoot)
        logger.verbose("File version is %s" % fileVersion)

        if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0":
            self.makeAlert(
                "Project file does not appear to be a novelWriterXML file version 1.0",
                nwAlert.ERROR)
            return False

        for xChild in xRoot:
            if xChild.tag == "project":
                logger.debug("Found project meta")
                for xItem in xChild:
                    if xItem.text is None:
                        continue
                    if xItem.tag == "name":
                        logger.verbose("Working Title: '%s'" % xItem.text)
                        self.projName = xItem.text
                    elif xItem.tag == "title":
                        logger.verbose("Title is '%s'" % xItem.text)
                        self.bookTitle = xItem.text
                    elif xItem.tag == "author":
                        logger.verbose("Author: '%s'" % xItem.text)
                        self.bookAuthors.append(xItem.text)
                    elif xItem.tag == "backup":
                        self.doBackup = checkBool(xItem.text, False)
            elif xChild.tag == "settings":
                logger.debug("Found project settings")
                for xItem in xChild:
                    if xItem.text is None:
                        continue
                    if xItem.tag == "spellCheck":
                        self.spellCheck = checkBool(xItem.text, False)
                    elif xItem.tag == "lastEdited":
                        self.lastEdited = checkString(xItem.text, None, True)
                    elif xItem.tag == "lastViewed":
                        self.lastViewed = checkString(xItem.text, None, True)
                    elif xItem.tag == "lastWordCount":
                        self.lastWCount = checkInt(xItem.text, 0, False)
                    elif xItem.tag == "status":
                        self.statusItems.unpackEntries(xItem)
                    elif xItem.tag == "importance":
                        self.importItems.unpackEntries(xItem)
                    elif xItem.tag == "autoReplace":
                        for xEntry in xItem:
                            self.autoReplace[xEntry.tag] = checkString(
                                xEntry.text, None, False)
            elif xChild.tag == "content":
                logger.debug("Found project content")
                for xItem in xChild:
                    itemAttrib = xItem.attrib
                    if "handle" in xItem.attrib:
                        tHandle = itemAttrib["handle"]
                    else:
                        logger.error("Skipping entry missing handle")
                        continue
                    if "parent" in xItem.attrib:
                        pHandle = itemAttrib["parent"]
                    else:
                        pHandle = None
                    nwItem = NWItem(self)
                    for xValue in xItem:
                        nwItem.setFromTag(xValue.tag, xValue.text)
                    self._appendItem(tHandle, pHandle, nwItem)

        self.mainConf.setRecent(self.projPath)
        self.theParent.setStatus("Opened Project: %s" % self.projName)

        self._scanProjectFolder()
        self.setProjectChanged(False)
        self.projOpened = time()
        self.projAltered = False

        return True
Beispiel #6
0
 def newFile(self, fileName, fileClass, pHandle):
     newItem = NWItem(self)
     newItem.setName(fileName)
     newItem.setType(nwItemType.FILE)
     if fileClass == nwItemClass.NOVEL:
         newItem.setLayout(nwItemLayout.SCENE)
     else:
         newItem.setLayout(nwItemLayout.NOTE)
     newItem.setClass(fileClass)
     newItem.setStatus(0)
     self._appendItem(None, pHandle, newItem)
     return newItem.itemHandle
Beispiel #7
0
    def openProject(self, fileName):

        if not path.isfile(fileName):
            fileName = path.join(fileName, nwFiles.PROJ_FILE)
            if not path.isfile(fileName):
                self.makeAlert("File not found: %s" % fileName, nwAlert.ERROR)
                return False

        self.clearProject()
        self.projPath = path.dirname(fileName)
        logger.debug("Opening project: %s" % self.projPath)

        self.projMeta  = path.join(self.projPath,"meta")
        self.projCache = path.join(self.projPath,"cache")
        self.projDict  = path.join(self.projMeta, nwFiles.PROJ_DICT)

        if not self._checkFolder(self.projMeta):  return
        if not self._checkFolder(self.projCache): return

        nwXML = etree.parse(fileName)
        xRoot = nwXML.getroot()

        nwxRoot     = xRoot.tag
        appVersion  = xRoot.attrib["appVersion"]
        fileVersion = xRoot.attrib["fileVersion"]

        logger.verbose("XML root is %s" % nwxRoot)
        logger.verbose("File version is %s" % fileVersion)

        if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0":
            self.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0", nwAlert.ERROR)
            return False

        for xChild in xRoot:
            if xChild.tag == "project":
                logger.debug("Found project meta")
                for xItem in xChild:
                    if xItem.text is None: continue
                    if xItem.tag == "name":
                        logger.verbose("Working Title: '%s'" % xItem.text)
                        self.projName = xItem.text
                    elif xItem.tag == "title":
                        logger.verbose("Title is '%s'" % xItem.text)
                        self.bookTitle = xItem.text
                    elif xItem.tag == "author":
                        logger.verbose("Author: '%s'" % xItem.text)
                        self.bookAuthors.append(xItem.text)
                    elif xItem.tag == "backup":
                        self.doBackup = checkBool(xItem.text,False)
            elif xChild.tag == "settings":
                logger.debug("Found project settings")
                for xItem in xChild:
                    if xItem.text is None: continue
                    if xItem.tag == "spellCheck":
                        self.spellCheck = checkBool(xItem.text,False)
                    elif xItem.tag == "lastEdited":
                        self.lastEdited = checkString(xItem.text,None,True)
                    elif xItem.tag == "lastViewed":
                        self.lastViewed = checkString(xItem.text,None,True)
                    elif xItem.tag == "lastWordCount":
                        self.lastWCount = checkInt(xItem.text,0,False)
                    elif xItem.tag == "status":
                        self.statusItems.unpackEntries(xItem)
                    elif xItem.tag == "importance":
                        self.importItems.unpackEntries(xItem)
                    elif xItem.tag == "autoReplace":
                        for xEntry in xItem:
                            self.autoReplace[xEntry.tag] = checkString(xEntry.text,None,False)
            elif xChild.tag == "content":
                logger.debug("Found project content")
                for xItem in xChild:
                    itemAttrib = xItem.attrib
                    if "handle" in xItem.attrib:
                        tHandle = itemAttrib["handle"]
                    else:
                        logger.error("Skipping entry missing handle")
                        continue
                    if "parent" in xItem.attrib:
                        pHandle = itemAttrib["parent"]
                    else:
                        pHandle = None
                    nwItem = NWItem(self)
                    for xValue in xItem:
                        nwItem.setFromTag(xValue.tag,xValue.text)
                    self._appendItem(tHandle,pHandle,nwItem)

        self.mainConf.setRecent(self.projPath)
        self.theParent.setStatus("Opened Project: %s" % self.projName)

        self._scanProjectFolder()
        self.setProjectChanged(False)
        self.projOpened = time()

        return True