mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Decouple the Settings
class from DangerzoneCore
No real reason to pass the whole object where what we really need is just the location of the configuration folder.
This commit is contained in:
parent
5b5bef2a58
commit
1353fa76d9
3 changed files with 27 additions and 52 deletions
|
@ -23,16 +23,13 @@ class DangerzoneCore(object):
|
||||||
# Initialize terminal colors
|
# Initialize terminal colors
|
||||||
colorama.init(autoreset=True)
|
colorama.init(autoreset=True)
|
||||||
|
|
||||||
# App data folder
|
|
||||||
self.appdata_path = util.get_config_dir()
|
|
||||||
|
|
||||||
# Languages supported by tesseract
|
# Languages supported by tesseract
|
||||||
with get_resource_path("ocr-languages.json").open("r") as f:
|
with get_resource_path("ocr-languages.json").open("r") as f:
|
||||||
unsorted_ocr_languages = json.load(f)
|
unsorted_ocr_languages = json.load(f)
|
||||||
self.ocr_languages = dict(sorted(unsorted_ocr_languages.items()))
|
self.ocr_languages = dict(sorted(unsorted_ocr_languages.items()))
|
||||||
|
|
||||||
# Load settings
|
# Load settings
|
||||||
self.settings = Settings(self)
|
self.settings = Settings()
|
||||||
self.documents: List[Document] = []
|
self.documents: List[Document] = []
|
||||||
self.isolation_provider = isolation_provider
|
self.isolation_provider = isolation_provider
|
||||||
|
|
||||||
|
|
|
@ -6,24 +6,18 @@ from typing import TYPE_CHECKING, Any, Dict
|
||||||
from packaging import version
|
from packaging import version
|
||||||
|
|
||||||
from .document import SAFE_EXTENSION
|
from .document import SAFE_EXTENSION
|
||||||
from .util import get_version
|
from .util import get_config_dir, get_version
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from .logic import DangerzoneCore
|
|
||||||
|
|
||||||
SETTINGS_FILENAME: str = "settings.json"
|
SETTINGS_FILENAME: str = "settings.json"
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
settings: Dict[str, Any]
|
settings: Dict[str, Any]
|
||||||
|
|
||||||
def __init__(self, dangerzone: "DangerzoneCore") -> None:
|
def __init__(self) -> None:
|
||||||
self.dangerzone = dangerzone
|
self.settings_filename = get_config_dir() / SETTINGS_FILENAME
|
||||||
self.settings_filename = os.path.join(
|
|
||||||
self.dangerzone.appdata_path, SETTINGS_FILENAME
|
|
||||||
)
|
|
||||||
self.default_settings: Dict[str, Any] = self.generate_default_settings()
|
self.default_settings: Dict[str, Any] = self.generate_default_settings()
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
|
@ -91,6 +85,6 @@ class Settings:
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
os.makedirs(self.dangerzone.appdata_path, exist_ok=True)
|
self.settings_filename.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with open(self.settings_filename, "w") as settings_file:
|
with self.settings_filename.open("w") as settings_file:
|
||||||
json.dump(self.settings, settings_file, indent=4)
|
json.dump(self.settings, settings_file, indent=4)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import PropertyMock
|
from unittest.mock import PropertyMock
|
||||||
|
|
||||||
|
@ -22,40 +21,31 @@ def default_settings_0_4_1() -> dict:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_no_settings_file_creates_new_one(
|
||||||
def settings(tmp_path: Path, mocker: MockerFixture) -> Settings:
|
tmp_path: Path,
|
||||||
dz_core = mocker.MagicMock()
|
mocker: MockerFixture,
|
||||||
type(dz_core).appdata_path = PropertyMock(return_value=tmp_path)
|
) -> None:
|
||||||
return Settings(dz_core)
|
|
||||||
|
|
||||||
|
|
||||||
def save_settings(tmp_path: Path, settings: dict) -> None:
|
|
||||||
"""Mimic the way Settings save a dictionary to a settings.json file."""
|
|
||||||
settings_filename = tmp_path / "settings.json"
|
|
||||||
with open(settings_filename, "w") as settings_file:
|
|
||||||
json.dump(settings, settings_file, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
def test_no_settings_file_creates_new_one(settings: Settings) -> None:
|
|
||||||
"""Default settings file is created on first run"""
|
"""Default settings file is created on first run"""
|
||||||
assert os.path.isfile(settings.settings_filename)
|
mocker.patch("dangerzone.settings.get_config_dir", return_value=tmp_path)
|
||||||
new_settings_dict = json.load(open(settings.settings_filename))
|
settings = Settings()
|
||||||
assert sorted(new_settings_dict.items()) == sorted(
|
|
||||||
settings.generate_default_settings().items()
|
assert settings.settings_filename.is_file()
|
||||||
)
|
with settings.settings_filename.open() as settings_file:
|
||||||
|
new_settings_dict = json.load(settings_file)
|
||||||
|
assert sorted(new_settings_dict.items()) == sorted(
|
||||||
|
settings.generate_default_settings().items()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_corrupt_settings(tmp_path: Path, mocker: MockerFixture) -> None:
|
def test_corrupt_settings(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
# Set some broken settings file
|
# Set some broken settings file
|
||||||
corrupt_settings_dict = "{:}"
|
corrupt_settings_dict = "{:}"
|
||||||
with open(tmp_path / SETTINGS_FILENAME, "w") as settings_file:
|
with (tmp_path / SETTINGS_FILENAME).open("w") as settings_file:
|
||||||
settings_file.write(corrupt_settings_dict)
|
settings_file.write(corrupt_settings_dict)
|
||||||
|
|
||||||
# Initialize settings
|
mocker.patch("dangerzone.settings.get_config_dir", return_value=tmp_path)
|
||||||
dz_core = mocker.MagicMock()
|
settings = Settings()
|
||||||
type(dz_core).appdata_path = PropertyMock(return_value=tmp_path)
|
assert settings.settings_filename.is_file()
|
||||||
settings = Settings(dz_core)
|
|
||||||
assert os.path.isfile(settings.settings_filename)
|
|
||||||
|
|
||||||
# Check if settings file was reset to the default
|
# Check if settings file was reset to the default
|
||||||
new_settings_dict = json.load(open(settings.settings_filename))
|
new_settings_dict = json.load(open(settings.settings_filename))
|
||||||
|
@ -66,10 +56,7 @@ def test_corrupt_settings(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
|
|
||||||
|
|
||||||
def test_new_default_setting(tmp_path: Path, mocker: MockerFixture) -> None:
|
def test_new_default_setting(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
# Initialize settings
|
settings = Settings()
|
||||||
dz_core = mocker.MagicMock()
|
|
||||||
type(dz_core).appdata_path = PropertyMock(return_value=tmp_path)
|
|
||||||
settings = Settings(dz_core)
|
|
||||||
settings.save()
|
settings.save()
|
||||||
|
|
||||||
# Ensure new default setting is imported into settings
|
# Ensure new default setting is imported into settings
|
||||||
|
@ -78,15 +65,12 @@ def test_new_default_setting(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
return_value={"mock_setting": 1},
|
return_value={"mock_setting": 1},
|
||||||
)
|
)
|
||||||
|
|
||||||
settings2 = Settings(dz_core)
|
settings2 = Settings()
|
||||||
assert settings2.get("mock_setting") == 1
|
assert settings2.get("mock_setting") == 1
|
||||||
|
|
||||||
|
|
||||||
def test_new_settings_added(tmp_path: Path, mocker: MockerFixture) -> None:
|
def test_new_settings_added(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
# Initialize settings
|
settings = Settings()
|
||||||
dz_core = mocker.MagicMock()
|
|
||||||
type(dz_core).appdata_path = PropertyMock(return_value=tmp_path)
|
|
||||||
settings = Settings(dz_core)
|
|
||||||
|
|
||||||
# Add new setting
|
# Add new setting
|
||||||
settings.set("new_setting_autosaved", 20, autosave=True)
|
settings.set("new_setting_autosaved", 20, autosave=True)
|
||||||
|
@ -95,7 +79,7 @@ def test_new_settings_added(tmp_path: Path, mocker: MockerFixture) -> None:
|
||||||
) # XXX has to be afterwards; otherwise this will be saved
|
) # XXX has to be afterwards; otherwise this will be saved
|
||||||
|
|
||||||
# Simulate new app startup (settings recreation)
|
# Simulate new app startup (settings recreation)
|
||||||
settings2 = Settings(dz_core)
|
settings2 = Settings()
|
||||||
|
|
||||||
# Check if new setting persisted
|
# Check if new setting persisted
|
||||||
assert 20 == settings2.get("new_setting_autosaved")
|
assert 20 == settings2.get("new_setting_autosaved")
|
||||||
|
|
Loading…
Reference in a new issue