From e98e942f198b957b27121371ba8d7eba0856be4f Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 16 Apr 2022 14:59:25 +0200 Subject: [PATCH] Replace key generator in the NWStatus class --- novelwriter/core/project.py | 5 +++-- novelwriter/core/status.py | 26 ++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index d17cffc3..b00b73cf 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -218,12 +218,12 @@ class NWProject(): } self.spellCheck = False self.autoOutline = True - self.statusItems = NWStatus("s") + self.statusItems = NWStatus(NWStatus.STATUS) self.statusItems.write(None, self.tr("New"), (100, 100, 100)) self.statusItems.write(None, self.tr("Note"), (200, 50, 0)) self.statusItems.write(None, self.tr("Draft"), (200, 150, 0)) self.statusItems.write(None, self.tr("Finished"), (50, 200, 0)) - self.importItems = NWStatus("i") + self.importItems = NWStatus(NWStatus.IMPORT) self.importItems.write(None, self.tr("New"), (100, 100, 100)) self.importItems.write(None, self.tr("Minor"), (200, 50, 0)) self.importItems.write(None, self.tr("Major"), (200, 150, 0)) @@ -267,6 +267,7 @@ class NWProject(): logger.error("No project path set for the new project") return False + self.clearProject() if not self.setProjectPath(projPath, newProject=True): return False diff --git a/novelwriter/core/status.py b/novelwriter/core/status.py index 4aee93b2..a3792764 100644 --- a/novelwriter/core/status.py +++ b/novelwriter/core/status.py @@ -39,9 +39,12 @@ logger = logging.getLogger(__name__) class NWStatus(): + STATUS = 1 + IMPORT = 2 + def __init__(self, type): - self._type = str(type) + self._type = type self._store = {} self._reverse = {} self._default = None @@ -51,6 +54,13 @@ class NWStatus(): pixmap.fill(QColor(100, 100, 100)) self._defaultIcon = QIcon(pixmap) + if self._type == self.STATUS: + self._prefix = "s" + elif self._type == self.IMPORT: + self._prefix = "i" + else: + raise Exception("This is a bug!") + return def write(self, key, name, cols, count=None): @@ -209,21 +219,21 @@ class NWStatus(): flags. The Python recursion limit is given the job to handle the extreme case and will cause an app crash. """ - key = f"{self._type}{random.randint(0, 0xffffff):06x}" + key = f"{self._prefix}{random.getrandbits(24):06x}" if key in self._store: key = self._newKey() return key - def _isKey(self, key): - """Check if a string is a key or not. + def _isKey(self, value): + """Check if a value is a key or not. """ - if not isinstance(key, str): + if not isinstance(value, str): return False - if len(key) != 7: + if len(value) != 7: return False - if key[0] != self._type: + if value[0] != self._prefix: return False - for c in key[1:]: + for c in value[1:]: if c not in "0123456789abcdef": return False return True