Add a UUID to projects

This commit is contained in:
Veronica Berglyd Olsen
2022-11-05 17:03:45 +01:00
parent 8b4dac4e13
commit c6d3f680cc
22 changed files with 91 additions and 40 deletions
+10
View File
@@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import json
import uuid
import hashlib
import logging
@@ -113,6 +114,15 @@ def checkHandle(value, default, allowNone=False):
return default
def checkUuid(value, default):
"""Try to process a value as an uuid, or return a default.
"""
try:
return str(uuid.UUID(value))
except Exception:
return default
# =============================================================================================== #
# Validator Functions
# =============================================================================================== #
+1
View File
@@ -322,6 +322,7 @@ class ProjectBuilder:
projTitle = data.get("projTitle", lblNewProject)
projAuthors = data.get("projAuthors", "")
project.data.setUuid(None)
project.data.setName(projName)
project.data.setTitle(projTitle)
project.data.setAuthors(projAuthors)
+15 -1
View File
@@ -25,9 +25,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
import uuid
import logging
from novelwriter.common import checkBool, checkInt, checkStringNone, isHandle, simplified
from novelwriter.common import (
checkBool, checkInt, checkStringNone, checkUuid, isHandle, simplified
)
from novelwriter.core.status import NWStatus
logger = logging.getLogger(__name__)
@@ -196,6 +199,17 @@ class NWProjectData:
# Setters
##
def setUuid(self, value):
"""Set the project id.
"""
value = checkUuid(value, "")
if not value:
self._uuid = str(uuid.uuid4())
elif value != self._uuid:
self._uuid = value
self.theProject.setProjectChanged(True)
return
def setName(self, value):
"""Set a new project name.
"""
+2 -1
View File
@@ -224,6 +224,7 @@ class ProjectXMLReader:
"""Parse the project section of the XML file.
"""
logger.debug("Parsing <project> section")
projData.setUuid(xSection.attrib.get("id", None))
for xItem in xSection:
if xItem.tag == "name":
projData.setName(xItem.text)
@@ -476,7 +477,7 @@ class ProjectXMLWriter:
})
# Save Project Meta
xProject = etree.SubElement(xRoot, "project")
xProject = etree.SubElement(xRoot, "project", attrib={"id": projData.uuid})
self._packSingleValue(xProject, "name", projData.name)
self._packSingleValue(xProject, "title", projData.title)
self._packListValue(xProject, "author", projData.authors)