Skip to content

Commit

Permalink
Make the Wikipedia extension's functionality to download images confi…
Browse files Browse the repository at this point in the history
…gurable
  • Loading branch information
bartfeenstra committed May 8, 2024
1 parent b99abf6 commit c82ec1d
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 9 deletions.
8 changes: 7 additions & 1 deletion betty/assets/betty.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"POT-Creation-Date: 2024-05-08 17:53+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -328,6 +328,9 @@ msgstr ""
msgid "Divorce"
msgstr ""

msgid "Download images from the Wikipedia links in your ancestry"
msgstr ""

#, python-format
msgid "E.g. \"%(example)s\""
msgstr ""
Expand Down Expand Up @@ -581,6 +584,9 @@ msgstr ""
msgid "Places"
msgstr ""

msgid "Populate images"
msgstr ""

msgid "Pre-built Webpack front-end assets are available"
msgstr ""

Expand Down
8 changes: 7 additions & 1 deletion betty/assets/locale/de-DE/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"POT-Creation-Date: 2024-05-08 17:53+0100\n"
"PO-Revision-Date: 2024-02-08 13:24+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: de\n"
Expand Down Expand Up @@ -482,6 +482,9 @@ msgstr ""
msgid "Divorce"
msgstr "Scheidung"

msgid "Download images from the Wikipedia links in your ancestry"
msgstr ""

#, python-format
msgid "E.g. \"%(example)s\""
msgstr "Z.B. \"%(example)s\""
Expand Down Expand Up @@ -751,6 +754,9 @@ msgstr "Ort"
msgid "Places"
msgstr "Orte"

msgid "Populate images"
msgstr ""

msgid "Pre-built Webpack front-end assets are available"
msgstr "Vorgefertigte Webpack front-end assets sind verfügbar"

Expand Down
8 changes: 7 additions & 1 deletion betty/assets/locale/fr-FR/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"POT-Creation-Date: 2024-05-08 17:53+0100\n"
"PO-Revision-Date: 2024-02-08 13:24+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: fr\n"
Expand Down Expand Up @@ -402,6 +402,9 @@ msgstr ""
msgid "Divorce"
msgstr "Divorce"

msgid "Download images from the Wikipedia links in your ancestry"
msgstr ""

#, python-format
msgid "E.g. \"%(example)s\""
msgstr "Par exemple \"%(example)s\""
Expand Down Expand Up @@ -665,6 +668,9 @@ msgstr "Lieu"
msgid "Places"
msgstr "Lieux"

msgid "Populate images"
msgstr ""

msgid "Pre-built Webpack front-end assets are available"
msgstr ""

Expand Down
8 changes: 7 additions & 1 deletion betty/assets/locale/nl-NL/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"POT-Creation-Date: 2024-05-08 17:53+0100\n"
"PO-Revision-Date: 2024-02-11 15:31+0000\n"
"Last-Translator: Bart Feenstra <bart@bartfeenstra.com>\n"
"Language: nl\n"
Expand Down Expand Up @@ -475,6 +475,9 @@ msgstr ""
msgid "Divorce"
msgstr "Echtscheiding"

msgid "Download images from the Wikipedia links in your ancestry"
msgstr "Download afbeeldingen gebaseerd op de Wikipedialinks in je familiegeschiedenis"

#, python-format
msgid "E.g. \"%(example)s\""
msgstr "Bijvoorbeeld \"%(example)s\""
Expand Down Expand Up @@ -746,6 +749,9 @@ msgstr "Plaats"
msgid "Places"
msgstr "Plaatsen"

msgid "Populate images"
msgstr "Afbeeldingen aanvullen"

msgid "Pre-built Webpack front-end assets are available"
msgstr "Vooraf gebouwde Webpack front-end assets zijn beschikbaar."

Expand Down
8 changes: 7 additions & 1 deletion betty/assets/locale/uk/betty.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Betty VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-30 22:56+0100\n"
"POT-Creation-Date: 2024-05-08 17:53+0100\n"
"PO-Revision-Date: 2024-02-08 13:08+0000\n"
"Last-Translator: Rainer Thieringer <rainerthi@gmail.com>\n"
"Language: uk\n"
Expand Down Expand Up @@ -403,6 +403,9 @@ msgstr ""
msgid "Divorce"
msgstr "Розлучення"

msgid "Download images from the Wikipedia links in your ancestry"
msgstr ""

#, python-format
msgid "E.g. \"%(example)s\""
msgstr "Наприклад «%(example)s»"
Expand Down Expand Up @@ -665,6 +668,9 @@ msgstr "Місце"
msgid "Places"
msgstr "Місця"

msgid "Populate images"
msgstr ""

msgid "Pre-built Webpack front-end assets are available"
msgstr ""

Expand Down
20 changes: 18 additions & 2 deletions betty/extension/wikipedia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@
from jinja2.runtime import Context

from betty import wikipedia
from betty.app.extension import UserFacingExtension
from betty.app.extension import UserFacingExtension, ConfigurableExtension
from betty.asyncio import gather
from betty.extension.wikipedia.config import WikipediaConfiguration
from betty.extension.wikipedia.gui import _WikipediaGuiWidget
from betty.gui import GuiBuilder
from betty.jinja2 import Jinja2Provider, context_localizer
from betty.load import PostLoader
from betty.locale import negotiate_locale, Str
from betty.model.ancestry import Link
from betty.wikipedia import Summary, _parse_url, NotAPageError, RetrievalError


class Wikipedia(UserFacingExtension, Jinja2Provider, PostLoader):
class Wikipedia(
ConfigurableExtension[WikipediaConfiguration],
UserFacingExtension,
Jinja2Provider,
PostLoader,
GuiBuilder,
):
@classmethod
def name(cls) -> str:
return "betty.extension.Wikipedia"
Expand Down Expand Up @@ -102,3 +111,10 @@ def description(cls) -> Str:
{{% endwith %}}
</code></pre>"""
)

@classmethod
def default_configuration(cls) -> WikipediaConfiguration:
return WikipediaConfiguration()

def gui_build(self) -> _WikipediaGuiWidget:
return _WikipediaGuiWidget(self._app, self._configuration)
53 changes: 53 additions & 0 deletions betty/extension/wikipedia/config.py
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)
47 changes: 47 additions & 0 deletions betty/extension/wikipedia/gui.py
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"
)
)
58 changes: 58 additions & 0 deletions betty/tests/extension/wikipedia/test_config.py
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
19 changes: 19 additions & 0 deletions betty/tests/extension/wikipedia/test_gui.py
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
1 change: 0 additions & 1 deletion documentation/usage/extension/gramps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ The *Gramps* extension
The :py:class:`betty.extension.Gramps` extension loads entities from `Gramps <https://gramps-project.org>`_ family trees into your Betty ancestry.

Enable this extension through Betty Desktop, or in your project's :doc:`configuration file </usage/project/configuration>` as follows:

.. md-tab-set::

.. md-tab-item:: YAML
Expand Down
Loading

0 comments on commit c82ec1d

Please sign in to comment.