示例#1
0
    def unpackXML(self, xParent):
        """Unpack an XML tree and set the class values.
        """
        theLabels = []
        theColours = []

        for xChild in xParent:
            theLabels.append(xChild.text)
            cR = checkInt(xChild.attrib.get("red", 0), 0, False)
            cG = checkInt(xChild.attrib.get("green", 0), 0, False)
            cB = checkInt(xChild.attrib.get("blue", 0), 0, False)
            theColours.append((cR, cG, cB))

        if len(theLabels) > 0:
            self._theLabels = []
            self._theColours = []
            self._theCounts = []
            self._theMap = {}
            self._theLength = 0
            self._theIndex = 0

            for n in range(len(theLabels)):
                self.addEntry(theLabels[n], theColours[n])

        return True
示例#2
0
    def unpackEntries(self, xParent):

        theLabels = []
        theColours = []

        for xChild in xParent:
            theLabels.append(xChild.text)
            if "red" in xChild.attrib:
                cR = checkInt(xChild.attrib["red"], 0, False)
            else:
                cR = 0
            if "green" in xChild.attrib:
                cG = checkInt(xChild.attrib["green"], 0, False)
            else:
                cG = 0
            if "blue" in xChild.attrib:
                cB = checkInt(xChild.attrib["blue"], 0, False)
            else:
                cB = 0
            theColours.append((cR, cG, cB))

        if len(theLabels) > 0:
            self.theLabels = []
            self.theColours = []
            self.theCounts = []
            self.theMap = {}
            self.theLength = 0
            self.theIndex = 0

            for n in range(len(theLabels)):
                self.addEntry(theLabels[n], theColours[n])

        return True
示例#3
0
    def showItem(self, tHandle, sTitle):
        """Update the content of the tree with the given handle and line
        number pointing to a header.
        """
        try:
            nwItem = self.theProject.projTree[tHandle]
            novIdx = self.theIndex.novelIndex[tHandle][sTitle]
            theRefs = self.theIndex.getReferences(tHandle, sTitle)
        except Exception:
            return False

        if novIdx["level"] in self.LVL_MAP:
            self.titleLabel.setText("<b>%s</b>" %
                                    self.LVL_MAP[novIdx["level"]])
        else:
            self.titleLabel.setText("<b>Title</b>")
        self.titleValue.setText(novIdx["title"])

        self.fileValue.setText(nwItem.itemName)
        self.itemValue.setText(nwItem.itemStatus)

        cC = checkInt(novIdx["cCount"], 0)
        wC = checkInt(novIdx["wCount"], 0)
        pC = checkInt(novIdx["pCount"], 0)

        self.cCValue.setText(f"{cC:n}")
        self.wCValue.setText(f"{wC:n}")
        self.pCValue.setText(f"{pC:n}")

        self.synopValue.setText(novIdx["synopsis"])

        self.povKeyValue.setText(self._formatTags(theRefs, nwKeyWords.POV_KEY))
        self.chrKeyValue.setText(self._formatTags(theRefs,
                                                  nwKeyWords.CHAR_KEY))
        self.pltKeyValue.setText(self._formatTags(theRefs,
                                                  nwKeyWords.PLOT_KEY))
        self.timKeyValue.setText(self._formatTags(theRefs,
                                                  nwKeyWords.TIME_KEY))
        self.wldKeyValue.setText(
            self._formatTags(theRefs, nwKeyWords.WORLD_KEY))
        self.objKeyValue.setText(
            self._formatTags(theRefs, nwKeyWords.OBJECT_KEY))
        self.entKeyValue.setText(
            self._formatTags(theRefs, nwKeyWords.ENTITY_KEY))
        self.cstKeyValue.setText(
            self._formatTags(theRefs, nwKeyWords.CUSTOM_KEY))

        return True
示例#4
0
 def checkEntry(self, theStatus):
     if isinstance(theStatus, str):
         theStatus = theStatus.strip()
         if self.lookupEntry(theStatus) is not None:
             return theStatus
     theStatus = checkInt(theStatus, 0, False)
     if theStatus >= 0 and theStatus < self.theLength:
         return self.theLabels[theStatus]
示例#5
0
 def getSetting(self, setName):
     if setName in self.stringOpt:
         return checkString(self.theState[setName],self.theState[setName],False)
     elif setName in self.boolOpt:
         return checkBool(self.theState[setName],self.theState[setName],False)
     elif setName in self.intOpt:
         return checkInt(self.theState[setName],self.theState[setName],False)
     return None
示例#6
0
 def checkEntry(self, theStatus):
     """Check if a status value is valid, and returns the safe
     reference to be used internally.
     """
     if isinstance(theStatus, str):
         theStatus = theStatus.strip()
         if self.lookupEntry(theStatus) is not None:
             return theStatus
     theStatus = checkInt(theStatus, 0, False)
     if theStatus >= 0 and theStatus < self._theLength:
         return self._theLabels[theStatus]
示例#7
0
def testCheckInt():
    assert checkInt(None, 3, True) is None
    assert checkInt("None", 3, True) is None
    assert checkInt(None, 3, False) == 3
    assert checkInt(1, 3, False) == 1
    assert checkInt(1.0, 3, False) == 1
    assert checkInt(True, 3, False) == 1
示例#8
0
    def _treeDoubleClick(self, tItem, tCol):
        """Extract the handle and line number of the title double-
        clicked, and send it to the main gui class for opening in the
        document editor.
        """
        theData = tItem.data(self.C_TITLE, Qt.UserRole)
        tHandle = theData[0]
        tLine = checkInt(theData[1], 1)

        logger.verbose("User selected entry with handle %s on line %s" %
                       (tHandle, tLine))
        self.theParent.openDocument(tHandle, tLine=tLine - 1, doScroll=True)

        return
def testBaseCommon_CheckInt():
    """Test the checkInt function.
    """
    assert checkInt(None, 3, True) is None
    assert checkInt("None", 3, True) is None
    assert checkInt(None, 3, False) == 3
    assert checkInt(1, 3, False) == 1
    assert checkInt(1.0, 3, False) == 1
    assert checkInt(True, 3, False) == 1
    def _loadLogFile(self):
        """Load the content of the log file into a buffer.
        """
        logger.debug("Loading session log file")

        self.logData = []
        self.wordOffset = 0

        ttNovel = 0
        ttNotes = 0
        ttTime  = 0

        logFile = os.path.join(self.theProject.projMeta, nwFiles.SESS_STATS)
        if not os.path.isfile(logFile):
            logger.info("This project has no writing stats logfile")
            return False

        try:
            with open(logFile, mode="r", encoding="utf8") as inFile:
                for inLine in inFile:
                    if inLine.startswith("#"):
                        if inLine.startswith("# Offset"):
                            self.wordOffset = checkInt(inLine[9:].strip(), 0)
                            logger.verbose(
                                "Initial word count when log was started is %d" % self.wordOffset
                            )
                        continue

                    inData = inLine.split()
                    if len(inData) != 6:
                        continue

                    dStart = datetime.strptime(
                        "%s %s" % (inData[0], inData[1]), nwConst.FMT_TSTAMP
                    )
                    dEnd = datetime.strptime(
                        "%s %s" % (inData[2], inData[3]), nwConst.FMT_TSTAMP
                    )

                    tDiff = dEnd - dStart
                    sDiff = tDiff.total_seconds()
                    ttTime += sDiff

                    wcNovel = int(inData[4])
                    wcNotes = int(inData[5])
                    ttNovel = wcNovel
                    ttNotes = wcNotes

                    self.logData.append((dStart, sDiff, wcNovel, wcNotes))

        except Exception as e:
            self.theParent.makeAlert(
                ["Failed to read session log file.", str(e)], nwAlert.ERROR
            )
            return False

        ttWords = ttNovel + ttNotes
        self.labelTotal.setText(formatTime(round(ttTime)))
        self.novelWords.setText(f"{ttNovel:n}")
        self.notesWords.setText(f"{ttNotes:n}")
        self.totalWords.setText(f"{ttWords:n}")

        return True
示例#11
0
 def setCursorPos(self, thePosition):
     """Set the cursor position, and ensure that it is an integer.
     """
     self.cursorPos = checkInt(thePosition, 0)
     return
示例#12
0
 def setParaCount(self, theCount):
     """Set the paragraph count, and ensure that it is an integer.
     """
     self.paraCount = checkInt(theCount, 0)
     return
示例#13
0
 def setWordCount(self, theCount):
     """Set the word count, and ensure that it is an integer.
     """
     self.wordCount = checkInt(theCount, 0)
     return
示例#14
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
示例#15
0
 def setParaCount(self, theCount):
     theCount = checkInt(theCount,0)
     self.paraCount = theCount
     return
示例#16
0
 def setCharCount(self, theCount):
     theCount = checkInt(theCount,0)
     self.charCount = theCount
     return
示例#17
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
示例#18
0
 def setWordCount(self, theCount):
     theCount = checkInt(theCount,0)
     self.wordCount = theCount
     return
示例#19
0
 def setOrder(self, theOrder):
     """Set the item order, and ensure that it is valid. This value
     is purely a meta value, not actually used by novelWriter.
     """
     self.itemOrder = checkInt(theOrder, 0)
     return
示例#20
0
 def setCursorPos(self, thePosition):
     thePosition = checkInt(thePosition,0)
     self.cursorPos = thePosition
     return
示例#21
0
 def setCharCount(self, theCount):
     """Set the character count, and ensure that it is an integer.
     """
     self.charCount = checkInt(theCount, 0)
     return