mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12:38 +02:00
add typed hints to Settings dictionary
Originally tied to implment following PEP 589 [1] – TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys for the Settings dict. But this quickly turned out to very challenging without redoing the code. So we opted instead for using the Any keyword. [1]: https://peps.python.org/pep-0589/
This commit is contained in:
parent
b1c039c4a4
commit
e76132a2f0
1 changed files with 8 additions and 5 deletions
|
@ -1,21 +1,22 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Any, Dict, Optional
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .global_common import GlobalCommon
|
from .global_common import GlobalCommon
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
|
settings: Dict[str, Any]
|
||||||
|
|
||||||
def __init__(self, global_common: "GlobalCommon") -> None:
|
def __init__(self, global_common: "GlobalCommon") -> None:
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.settings_filename = os.path.join(
|
self.settings_filename = os.path.join(
|
||||||
self.global_common.appdata_path, "settings.json"
|
self.global_common.appdata_path, "settings.json"
|
||||||
)
|
)
|
||||||
self.default_settings = {
|
self.default_settings: Dict[str, Any] = {
|
||||||
"save": True,
|
"save": True,
|
||||||
"ocr": True,
|
"ocr": True,
|
||||||
"ocr_language": "English",
|
"ocr_language": "English",
|
||||||
|
@ -25,14 +26,16 @@ class Settings:
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
def get(self, key: str):
|
def get(self, key: str) -> Any:
|
||||||
return self.settings[key]
|
return self.settings[key]
|
||||||
|
|
||||||
def set(self, key: str, val) -> None:
|
def set(self, key: str, val: Any) -> None:
|
||||||
self.settings[key] = val
|
self.settings[key] = val
|
||||||
|
|
||||||
def load(self) -> None:
|
def load(self) -> None:
|
||||||
if os.path.isfile(self.settings_filename):
|
if os.path.isfile(self.settings_filename):
|
||||||
|
self.settings = self.default_settings
|
||||||
|
|
||||||
# If the settings file exists, load it
|
# If the settings file exists, load it
|
||||||
try:
|
try:
|
||||||
with open(self.settings_filename, "r") as settings_file:
|
with open(self.settings_filename, "r") as settings_file:
|
||||||
|
|
Loading…
Reference in a new issue