Replace key generator in the NWStatus class

This commit is contained in:
Veronica Berglyd Olsen
2022-04-16 14:59:25 +02:00
parent ac904ee9fc
commit e98e942f19
2 changed files with 21 additions and 10 deletions
+3 -2
View File
@@ -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
+18 -8
View File
@@ -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