Code cleanup in core classes and rename of parHandle to itemParent in NWItem class
This commit is contained in:
+7
-4
@@ -67,7 +67,9 @@ class NWDoc():
|
||||
|
||||
def openDocument(self, tHandle, showStatus=True, isOrphan=False):
|
||||
"""Open a document from handle, capturing potential file system
|
||||
errors and parse meta data.
|
||||
errors and parse meta data. If the document doesn't exist on
|
||||
disk, return an empty string. If something went wrong, return
|
||||
None.
|
||||
"""
|
||||
if not isHandle(tHandle):
|
||||
return None
|
||||
@@ -125,8 +127,8 @@ class NWDoc():
|
||||
return theText
|
||||
|
||||
def saveDocument(self, docText):
|
||||
"""Save the document via temp file in case of save failure, and
|
||||
in any case keep a backup of the file.
|
||||
"""Save the document. The file is saved via a temp file in case
|
||||
of save failure. Returns True if successful, False if not.
|
||||
"""
|
||||
if self._docHandle is None:
|
||||
return False
|
||||
@@ -139,6 +141,7 @@ class NWDoc():
|
||||
docPath = os.path.join(self.theProject.projContent, docFile)
|
||||
docTemp = os.path.join(self.theProject.projContent, docFile+"~")
|
||||
|
||||
# DocMeta line
|
||||
if self._theItem is None:
|
||||
docMeta = ""
|
||||
else:
|
||||
@@ -171,7 +174,7 @@ class NWDoc():
|
||||
return True
|
||||
|
||||
def deleteDocument(self, tHandle):
|
||||
"""Permanently delete a document source file and its backups
|
||||
"""Permanently delete a document source file and related files
|
||||
from the project data folder.
|
||||
"""
|
||||
if not isHandle(tHandle):
|
||||
|
||||
+3
-3
@@ -279,7 +279,7 @@ class NWIndex():
|
||||
if theItem.itemLayout == nwItemLayout.NO_LAYOUT:
|
||||
logger.info("Not indexing no-layout item %s" % tHandle)
|
||||
return False
|
||||
if theItem.parHandle is None:
|
||||
if theItem.itemParent is None:
|
||||
logger.info("Not indexing orphaned item %s" % tHandle)
|
||||
return False
|
||||
|
||||
@@ -288,7 +288,7 @@ class NWIndex():
|
||||
self.textCounts[tHandle] = [cC, wC, pC]
|
||||
|
||||
# If the file is archived or trashed, we don't index the file itself
|
||||
if self.theProject.projTree.isTrashRoot(theItem.parHandle):
|
||||
if self.theProject.projTree.isTrashRoot(theItem.itemParent):
|
||||
logger.info("Not indexing trash item %s" % tHandle)
|
||||
return False
|
||||
if theRoot.itemClass == nwItemClass.ARCHIVE:
|
||||
@@ -583,7 +583,7 @@ class NWIndex():
|
||||
|
||||
def getCounts(self, tHandle, sTitle=None):
|
||||
"""Returns the counts for a file, or a section of a file
|
||||
starting at title nTitle.
|
||||
starting at title sTitle if it is provided.
|
||||
"""
|
||||
cC = 0
|
||||
wC = 0
|
||||
|
||||
+11
-8
@@ -42,7 +42,7 @@ class NWItem():
|
||||
|
||||
self.itemName = ""
|
||||
self.itemHandle = None
|
||||
self.parHandle = None
|
||||
self.itemParent = None
|
||||
self.itemOrder = None
|
||||
self.itemType = nwItemType.NO_TYPE
|
||||
self.itemClass = nwItemClass.NO_CLASS
|
||||
@@ -70,7 +70,7 @@ class NWItem():
|
||||
xPack = etree.SubElement(xParent, "item", attrib={
|
||||
"handle" : str(self.itemHandle),
|
||||
"order" : str(self.itemOrder),
|
||||
"parent" : str(self.parHandle),
|
||||
"parent" : str(self.itemParent),
|
||||
})
|
||||
self._subPack(xPack, "name", text=str(self.itemName))
|
||||
self._subPack(xPack, "type", text=str(self.itemType.name))
|
||||
@@ -85,6 +85,7 @@ class NWItem():
|
||||
self._subPack(xPack, "cursorPos", text=str(self.cursorPos), none=False)
|
||||
else:
|
||||
self._subPack(xPack, "expanded", text=str(self.isExpanded))
|
||||
|
||||
return
|
||||
|
||||
def unpackXML(self, xItem):
|
||||
@@ -101,7 +102,7 @@ class NWItem():
|
||||
return False
|
||||
|
||||
if "parent" in xItem.attrib:
|
||||
self.parHandle = xItem.attrib["parent"]
|
||||
self.itemParent = xItem.attrib["parent"]
|
||||
|
||||
setMap = {
|
||||
"name" : self.setName,
|
||||
@@ -131,9 +132,11 @@ class NWItem():
|
||||
"""
|
||||
if not none and (text is None or text == "None"):
|
||||
return None
|
||||
xSub = etree.SubElement(xParent, name, attrib=attrib)
|
||||
xAttr = {} if attrib is None else attrib
|
||||
xSub = etree.SubElement(xParent, name, attrib=xAttr)
|
||||
if text is not None:
|
||||
xSub.text = text
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
@@ -162,14 +165,14 @@ class NWItem():
|
||||
"""Set the parent handle, and ensure that it is valid.
|
||||
"""
|
||||
if theParent is None:
|
||||
self.parHandle = None
|
||||
self.itemParent = None
|
||||
elif isinstance(theParent, str):
|
||||
if len(theParent) == 13:
|
||||
self.parHandle = theParent
|
||||
self.itemParent = theParent
|
||||
else:
|
||||
self.parHandle = None
|
||||
self.itemParent = None
|
||||
else:
|
||||
self.parHandle = None
|
||||
self.itemParent = None
|
||||
return
|
||||
|
||||
def setOrder(self, theOrder):
|
||||
|
||||
+3
-3
@@ -1139,16 +1139,16 @@ class NWProject():
|
||||
# Technically a bug since treeOrder is built from the
|
||||
# same data as projTree
|
||||
continue
|
||||
elif tItem.parHandle is None:
|
||||
elif tItem.itemParent is None:
|
||||
# Item is a root, or already been identified as an
|
||||
# orphaned item
|
||||
sentItems.append(tHandle)
|
||||
yield tItem
|
||||
elif tItem.parHandle in sentItems:
|
||||
elif tItem.itemParent in sentItems:
|
||||
# Item's parent has been sent, so all is fine
|
||||
sentItems.append(tHandle)
|
||||
yield tItem
|
||||
elif tItem.parHandle in iterItems:
|
||||
elif tItem.itemParent in iterItems:
|
||||
# Item's parent exists, but hasn't been sent yet, so add
|
||||
# it again to the end
|
||||
logger.warning("Item %s found before its parent" % tHandle)
|
||||
|
||||
+5
-5
@@ -134,7 +134,7 @@ class NWTree():
|
||||
for xItem in xContent:
|
||||
nwItem = NWItem(self.theProject)
|
||||
if nwItem.unpackXML(xItem):
|
||||
self.append(nwItem.itemHandle, nwItem.parHandle, nwItem)
|
||||
self.append(nwItem.itemHandle, nwItem.itemParent, nwItem)
|
||||
nwItem.saveInitialCount()
|
||||
|
||||
return True
|
||||
@@ -261,10 +261,10 @@ class NWTree():
|
||||
tItem = self.__getitem__(tHandle)
|
||||
if tItem is not None:
|
||||
for i in range(nwConst.maxDepth + 1):
|
||||
if tItem.parHandle is None:
|
||||
if tItem.itemParent is None:
|
||||
return tItem
|
||||
else:
|
||||
tHandle = tItem.parHandle
|
||||
tHandle = tItem.itemParent
|
||||
tItem = self.__getitem__(tHandle)
|
||||
return None
|
||||
|
||||
@@ -279,10 +279,10 @@ class NWTree():
|
||||
if tItem is not None:
|
||||
tTree.append(tHandle)
|
||||
for i in range(nwConst.maxDepth + 1):
|
||||
if tItem.parHandle is None:
|
||||
if tItem.itemParent is None:
|
||||
return tTree
|
||||
else:
|
||||
tHandle = tItem.parHandle
|
||||
tHandle = tItem.itemParent
|
||||
tItem = self.__getitem__(tHandle)
|
||||
if tItem is None:
|
||||
return tTree
|
||||
|
||||
Reference in New Issue
Block a user