-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the Wikipedia extension's functionality to download images confi…
…gurable
- Loading branch information
1 parent
b99abf6
commit c82ec1d
Showing
12 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Provide configuration for the Wikipedia extension. | ||
""" | ||
|
||
from typing import Self | ||
|
||
from betty.config import Configuration | ||
from betty.serde.dump import Dump, VoidableDump, minimize, VoidableDictDump | ||
from betty.serde.load import Asserter, Fields, OptionalField, Assertions | ||
|
||
|
||
class WikipediaConfiguration(Configuration): | ||
def __init__(self): | ||
super().__init__() | ||
self._populate_images = True | ||
|
||
@property | ||
def populate_images(self) -> bool: | ||
return self._populate_images | ||
|
||
@populate_images.setter | ||
def populate_images(self, populate_images: bool) -> None: | ||
self._populate_images = populate_images | ||
|
||
def update(self, other: Self) -> None: | ||
self._populate_images = other._populate_images | ||
self._dispatch_change() | ||
|
||
@classmethod | ||
def load( | ||
cls, | ||
dump: Dump, | ||
configuration: Self | None = None, | ||
) -> Self: | ||
if configuration is None: | ||
configuration = cls() | ||
asserter = Asserter() | ||
asserter.assert_record( | ||
Fields( | ||
OptionalField( | ||
"populate_images", | ||
Assertions(asserter.assert_bool()) | ||
| asserter.assert_setattr(configuration, "populate_images"), | ||
), | ||
) | ||
)(dump) | ||
return configuration | ||
|
||
def dump(self) -> VoidableDump: | ||
dump: VoidableDictDump[VoidableDump] = { | ||
"populate_images": self.populate_images, | ||
} | ||
return minimize(dump, True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Provide the GRaphical User Interface for the Wikipedia extension. | ||
""" | ||
|
||
from typing import Any | ||
|
||
from PyQt6.QtWidgets import ( | ||
QFormLayout, | ||
QWidget, | ||
QCheckBox, | ||
) | ||
|
||
from betty.app import App | ||
from betty.extension.wikipedia.config import WikipediaConfiguration | ||
from betty.gui.locale import LocalizedObject | ||
from betty.gui.text import Caption | ||
|
||
|
||
class _WikipediaGuiWidget(LocalizedObject, QWidget): | ||
def __init__( | ||
self, app: App, configuration: WikipediaConfiguration, *args: Any, **kwargs: Any | ||
): | ||
super().__init__(app, *args, **kwargs) | ||
self._app = app | ||
self._configuration = configuration | ||
layout = QFormLayout() | ||
|
||
self.setLayout(layout) | ||
|
||
def _update_configuration_populate_images(checked: bool) -> None: | ||
self._configuration.populate_images = checked | ||
|
||
self._populate_images = QCheckBox() | ||
self._populate_images.setChecked(self._configuration.populate_images) | ||
self._populate_images.toggled.connect(_update_configuration_populate_images) | ||
layout.addRow(self._populate_images) | ||
self._populate_images_caption = Caption() | ||
layout.addRow(self._populate_images_caption) | ||
|
||
def _set_translatables(self) -> None: | ||
super()._set_translatables() | ||
self._populate_images.setText(self._app.localizer._("Populate images")) | ||
self._populate_images_caption.setText( | ||
self._app.localizer._( | ||
"Download images from the Wikipedia links in your ancestry" | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from betty.extension.wikipedia.config import WikipediaConfiguration | ||
from betty.serde.dump import Dump | ||
from betty.serde.load import AssertionFailed | ||
from betty.tests.serde import raises_error | ||
|
||
|
||
class TestWikipediaConfiguration: | ||
async def test_load_with_minimal_configuration(self) -> None: | ||
dump: dict[str, Any] = {} | ||
WikipediaConfiguration().load(dump) | ||
|
||
async def test_load_without_dict_should_error(self) -> None: | ||
dump = None | ||
with raises_error(error_type=AssertionFailed): | ||
WikipediaConfiguration().load(dump) | ||
|
||
@pytest.mark.parametrize( | ||
"populate_images", | ||
[ | ||
True, | ||
False, | ||
], | ||
) | ||
async def test_load_with_populate_images( | ||
self, populate_images: bool | None | ||
) -> None: | ||
dump: Dump = { | ||
"populate_images": populate_images, | ||
} | ||
sut = WikipediaConfiguration.load(dump) | ||
assert sut.populate_images == populate_images | ||
|
||
async def test_dump_with_minimal_configuration(self) -> None: | ||
sut = WikipediaConfiguration() | ||
expected = { | ||
"populate_images": True, | ||
} | ||
assert expected == sut.dump() | ||
|
||
async def test_dump_with_populate_images(self) -> None: | ||
sut = WikipediaConfiguration() | ||
sut.populate_images = False | ||
expected = { | ||
"populate_images": False, | ||
} | ||
assert expected == sut.dump() | ||
|
||
async def test_update(self, tmp_path: Path) -> None: | ||
sut = WikipediaConfiguration() | ||
other = WikipediaConfiguration() | ||
other.populate_images = False | ||
sut.update(other) | ||
assert sut.populate_images is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
from betty.extension import Wikipedia | ||
from betty.tests.conftest import BettyQtBot | ||
|
||
|
||
class TestWikipediaGuiWidget: | ||
async def test_https_with_base_url( | ||
self, | ||
betty_qtbot: BettyQtBot, | ||
) -> None: | ||
betty_qtbot.app.project.configuration.extensions.enable(Wikipedia) | ||
wikipedia = betty_qtbot.app.extensions[Wikipedia] | ||
sut = wikipedia.gui_build() | ||
betty_qtbot.qtbot.addWidget(sut) | ||
sut.show() | ||
|
||
betty_qtbot.set_checked(sut._populate_images, False) | ||
assert not wikipedia.configuration.populate_images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.