Fix some typing issues
This commit is contained in:
@@ -150,17 +150,17 @@ class BuildSettings:
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the build name."""
|
||||
"""The build name."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def buildID(self) -> str:
|
||||
"""Return the build ID."""
|
||||
"""The build ID as an UUID."""
|
||||
return self._uuid
|
||||
|
||||
@property
|
||||
def changed(self) -> bool:
|
||||
"""Return the changed status of the build."""
|
||||
"""The changed status of the build."""
|
||||
return self._changed
|
||||
|
||||
##
|
||||
@@ -190,7 +190,7 @@ class BuildSettings:
|
||||
return 0
|
||||
|
||||
def getFloat(self, key: str) -> float:
|
||||
"""Type safe value access for float."""
|
||||
"""Type safe value access for floats."""
|
||||
value = self._settings.get(key, SETTINGS_TEMPLATE.get(key, (None, None)[1]))
|
||||
if isinstance(value, float):
|
||||
return value
|
||||
|
||||
+27
-19
@@ -1,7 +1,6 @@
|
||||
"""
|
||||
novelWriter – Project Options Cache
|
||||
===================================
|
||||
Data class for user-defined GUI project options
|
||||
|
||||
File History:
|
||||
Created: 2019-10-21 [0.3.1]
|
||||
@@ -23,17 +22,22 @@ General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from pathlib import Path
|
||||
|
||||
from novelwriter.error import logException
|
||||
from novelwriter.common import checkBool, checkFloat, checkInt, checkString
|
||||
from novelwriter.constants import nwFiles
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from novelwriter.core.project import NWProject
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
VALID_MAP = {
|
||||
@@ -63,9 +67,15 @@ VALID_MAP = {
|
||||
|
||||
|
||||
class OptionState:
|
||||
"""Core: GUI Options Storage
|
||||
|
||||
def __init__(self, theProject):
|
||||
self.theProject = theProject
|
||||
A class for storing the state of the GUI. The data is stored per
|
||||
project. Settings that should be project-independent are stored in
|
||||
the Config instead.
|
||||
"""
|
||||
|
||||
def __init__(self, project: NWProject):
|
||||
self._project = project
|
||||
self._theState = {}
|
||||
return
|
||||
|
||||
@@ -73,10 +83,10 @@ class OptionState:
|
||||
# Load and Save Cache
|
||||
##
|
||||
|
||||
def loadSettings(self):
|
||||
def loadSettings(self) -> bool:
|
||||
"""Load the options dictionary from the project settings file.
|
||||
"""
|
||||
stateFile = self.theProject.storage.getMetaFile(nwFiles.OPTS_FILE)
|
||||
stateFile = self._project.storage.getMetaFile(nwFiles.OPTS_FILE)
|
||||
if not isinstance(stateFile, Path):
|
||||
return False
|
||||
|
||||
@@ -101,10 +111,9 @@ class OptionState:
|
||||
|
||||
return True
|
||||
|
||||
def saveSettings(self):
|
||||
"""Save the options dictionary to the project settings file.
|
||||
"""
|
||||
stateFile = self.theProject.storage.getMetaFile(nwFiles.OPTS_FILE)
|
||||
def saveSettings(self) -> bool:
|
||||
"""Save the options dictionary to the project settings file."""
|
||||
stateFile = self._project.storage.getMetaFile(nwFiles.OPTS_FILE)
|
||||
if not isinstance(stateFile, Path):
|
||||
return False
|
||||
|
||||
@@ -123,9 +132,8 @@ class OptionState:
|
||||
# Setters
|
||||
##
|
||||
|
||||
def setValue(self, group, name, value):
|
||||
"""Save a value, with a given group and name.
|
||||
"""
|
||||
def setValue(self, group: str, name: str, value: Any) -> bool:
|
||||
"""Save a value, with a given group and name."""
|
||||
if group not in VALID_MAP:
|
||||
logger.error("Unknown option group '%s'", group)
|
||||
return False
|
||||
@@ -148,7 +156,7 @@ class OptionState:
|
||||
# Getters
|
||||
##
|
||||
|
||||
def getValue(self, group, name, default):
|
||||
def getValue(self, group: str, name: str, default: Any) -> Any:
|
||||
"""Return an arbitrary type value, if it exists. Otherwise,
|
||||
return the default value.
|
||||
"""
|
||||
@@ -156,7 +164,7 @@ class OptionState:
|
||||
return self._theState[group].get(name, default)
|
||||
return default
|
||||
|
||||
def getString(self, group, name, default):
|
||||
def getString(self, group: str, name: str, default: str) -> str:
|
||||
"""Return the value as a string, if it exists. Otherwise, return
|
||||
the default value.
|
||||
"""
|
||||
@@ -164,7 +172,7 @@ class OptionState:
|
||||
return checkString(self._theState[group].get(name, default), default)
|
||||
return default
|
||||
|
||||
def getInt(self, group, name, default):
|
||||
def getInt(self, group: str, name: str, default: int) -> int:
|
||||
"""Return the value as an int, if it exists. Otherwise, return
|
||||
the default value.
|
||||
"""
|
||||
@@ -172,7 +180,7 @@ class OptionState:
|
||||
return checkInt(self._theState[group].get(name, default), default)
|
||||
return default
|
||||
|
||||
def getFloat(self, group, name, default):
|
||||
def getFloat(self, group: str, name: str, default: float) -> float:
|
||||
"""Return the value as a float, if it exists. Otherwise, return
|
||||
the default value.
|
||||
"""
|
||||
@@ -180,7 +188,7 @@ class OptionState:
|
||||
return checkFloat(self._theState[group].get(name, default), default)
|
||||
return default
|
||||
|
||||
def getBool(self, group, name, default):
|
||||
def getBool(self, group: str, name: str, default: bool) -> bool:
|
||||
"""Return the value as a bool, if it exists. Otherwise, return
|
||||
the default value.
|
||||
"""
|
||||
@@ -188,9 +196,9 @@ class OptionState:
|
||||
return checkBool(self._theState[group].get(name, default), default)
|
||||
return default
|
||||
|
||||
def getEnum(self, group, name, lookup, default):
|
||||
def getEnum(self, group: str, name: str, lookup: type, default: Enum) -> Enum:
|
||||
"""Return the value mapped to an enum. Otherwise return the
|
||||
default value
|
||||
default value.
|
||||
"""
|
||||
if issubclass(lookup, Enum):
|
||||
if group in self._theState:
|
||||
|
||||
@@ -285,15 +285,20 @@ class NWProjectData:
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setLastHandle(self, value: dict, component: str | None = None):
|
||||
"""Set a last used handle into the handle registry. If component
|
||||
is None, the value is assumed to be the whole dictionary of
|
||||
values.
|
||||
def setLastHandle(self, value: str | None, component: str):
|
||||
"""Set a last used handle into the handle registry for a given
|
||||
component.
|
||||
"""
|
||||
if isinstance(component, str):
|
||||
self._lastHandle[component] = checkStringNone(value, None)
|
||||
self.theProject.setProjectChanged(True)
|
||||
elif isinstance(value, dict):
|
||||
return
|
||||
|
||||
def setLastHandles(self, value: dict):
|
||||
"""Set the full last handles dictionary to a new set of values.
|
||||
This is intended to be used at project load.
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
for key, entry in value.items():
|
||||
if key in self._lastHandle:
|
||||
self._lastHandle[key] = str(entry) if isHandle(entry) else None
|
||||
|
||||
@@ -30,8 +30,8 @@ import xml.etree.ElementTree as ET
|
||||
|
||||
from enum import Enum
|
||||
from time import time
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from pathlib import Path
|
||||
|
||||
from novelwriter import __version__, __hexversion__
|
||||
from novelwriter.common import (
|
||||
@@ -284,7 +284,7 @@ class ProjectXMLReader:
|
||||
elif xItem.tag == "importance":
|
||||
self._parseStatusImport(xItem, data.itemImport)
|
||||
elif xItem.tag == "lastHandle":
|
||||
data.setLastHandle(self._parseDictKeyText(xItem))
|
||||
data.setLastHandles(self._parseDictKeyText(xItem))
|
||||
elif xItem.tag == "autoReplace":
|
||||
if self._version >= 0x0102:
|
||||
data.setAutoReplace(self._parseDictKeyText(xItem))
|
||||
|
||||
@@ -170,7 +170,7 @@ class NWStorage:
|
||||
xmlWriter = ProjectXMLWriter(self._runtimePath)
|
||||
return xmlWriter
|
||||
|
||||
def getDocument(self, tHandle: str) -> NWDocument:
|
||||
def getDocument(self, tHandle: str | None) -> NWDocument:
|
||||
"""Return a document wrapper object."""
|
||||
if self._runtimePath is not None:
|
||||
return NWDocument(self._project, tHandle)
|
||||
|
||||
Reference in New Issue
Block a user