From 619810544aa6a44b7df527e016a310b73d08e327 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 16 Aug 2020 21:55:36 +0200 Subject: [PATCH] Reduce the defacto maximum folder depth from ~200 to a hard 30 and enforce it --- nw/constants/constants.py | 2 ++ nw/core/document.py | 4 ++-- nw/core/tree.py | 10 +++++----- nw/gui/projtree.py | 18 +++++++++++++++--- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index a8859873..7c25fa46 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -33,6 +33,8 @@ class nwConst(): fStampFmt = "%Y-%m-%d %H.%M.%S" # FileName safe format dStampFmt = "%Y-%m-%d" # Date only format + maxDepth = 30 # Maximum folder depth of a project + # END Class nwConst class nwRegEx(): diff --git a/nw/core/document.py b/nw/core/document.py index 022e608e..9aec16f6 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -33,7 +33,7 @@ from os import path, rename, unlink from nw.core.item import NWItem from nw.constants import nwAlert from nw.common import isHandle -from nw.constants import nwItemLayout, nwItemClass +from nw.constants import nwItemLayout, nwItemClass, nwConst logger = logging.getLogger(__name__) @@ -213,7 +213,7 @@ class NWDoc(): # Scan for handles thePath = [] - for n in range(200): + for n in range(nwConst.maxDepth + 5): if len(theMeta) < 14: break if theMeta[13] == ":": diff --git a/nw/core/tree.py b/nw/core/tree.py index 7d1bef46..0fcf2061 100644 --- a/nw/core/tree.py +++ b/nw/core/tree.py @@ -35,7 +35,7 @@ from time import time from nw.core.item import NWItem from nw.common import checkString -from nw.constants import nwFiles, nwItemType, nwItemClass, nwItemLayout +from nw.constants import nwFiles, nwItemType, nwItemClass, nwItemLayout, nwConst logger = logging.getLogger(__name__) @@ -251,11 +251,11 @@ class NWTree(): def getRootItem(self, tHandle): """Iterate upwards in the tree until we find the item with parent None, the root item. We do this with a for loop with a - maximum depth of 200 to make infinite loops impossible. + maximum depth to make infinite loops impossible. """ tItem = self.__getitem__(tHandle) if tItem is not None: - for i in range(200): + for i in range(nwConst.maxDepth + 1): if tItem.parHandle is None: return tItem else: @@ -266,14 +266,14 @@ class NWTree(): def getItemPath(self, tHandle): """Iterate upwards in the tree until we find the item with parent None, the root item, and return the list of handles. - We do this with a for loop with a maximum depth of 200 to make + We do this with a for loop with a maximum depth to make infinite loops impossible. """ tTree = [] tItem = self.__getitem__(tHandle) if tItem is not None: tTree.append(tHandle) - for i in range(200): + for i in range(nwConst.maxDepth + 1): if tItem.parHandle is None: return tTree else: diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index a84e7176..2eaf832c 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -38,7 +38,7 @@ from PyQt5.QtWidgets import ( from nw.core import NWDoc from nw.constants import ( - nwLabels, nwItemType, nwItemClass, nwItemLayout, nwAlert + nwLabels, nwItemType, nwItemClass, nwItemLayout, nwAlert, nwConst ) logger = logging.getLogger(__name__) @@ -119,7 +119,6 @@ class GuiProjectTree(QTreeWidget): # Internal Mapping self.makeAlert = self.theParent.makeAlert - self.theParent.editItem() return @@ -215,17 +214,30 @@ class GuiProjectTree(QTreeWidget): ) return False + parTree = self.theProject.projTree.getItemPath(pHandle) + # If we're still here, add the file or folder if itemType == nwItemType.FILE: tHandle = self.theProject.newFile("New File", itemClass, pHandle) + elif itemType == nwItemType.FOLDER: + if len(parTree) >= nwConst.maxDepth - 1: + # Folders cannot be deeper than maxDepth - 1, leaving room + # for one more level of files. + self.makeAlert(( + "Cannot add new folder to this item. " + "Maximum folder depth has been reached." + ), nwAlert.ERROR) + return False tHandle = self.theProject.newFolder("New Folder", itemClass, pHandle) + else: logger.error("Failed to add new item") return False # Add the new item to the tree self.revealTreeItem(tHandle, nHandle) + self.theParent.editItem() return True @@ -541,7 +553,7 @@ class GuiProjectTree(QTreeWidget): pCount += int(pItem.child(i).text(self.C_COUNT)) pHandle = pItem.data(self.C_NAME, Qt.UserRole) - if not nDepth > 200 and pHandle != "": + if not nDepth > nwConst.maxDepth + 1 and pHandle != "": self.propagateCount(pHandle, pCount, nDepth+1) return