Skip to content

Commit

Permalink
fix(cli): migrate from multimethod to singledispatchmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Nov 8, 2024
1 parent e4baab3 commit ea4c10e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
34 changes: 17 additions & 17 deletions arguebuf/cli/translator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from collections.abc import Iterable
from functools import singledispatchmethod

from deepl.translator import Translator as DeepLTranslator
from multimethod import multimethod

import arguebuf as ag

Expand Down Expand Up @@ -31,50 +31,50 @@ def _deepl_translate(self, text: str | Iterable[str]) -> str | list[str]:
else:
return result.text

@multimethod
def translate(self):
@singledispatchmethod
def translate(self, arg):
raise NotImplementedError()

@translate.register
def _(self, text: str) -> str:
trans = self._deepl_translate(text)
def _(self, arg: str) -> str:
trans = self._deepl_translate(arg)
assert isinstance(trans, str)
return trans

@translate.register
def _(self, texts: Iterable[str]) -> list[str]:
trans = self._deepl_translate(texts)
def _(self, arg: Iterable[str]) -> list[str]:
trans = self._deepl_translate(arg)
assert isinstance(trans, list)
return trans

@translate.register
def _(self, graph: ag.Graph) -> None:
def _(self, arg: ag.Graph) -> None:
original_resources = [
resource.plain_text for resource in graph.resources.values()
resource.plain_text for resource in arg.resources.values()
]
for resource, translation in zip(
graph.resources.values(), self.translate(original_resources), strict=True
arg.resources.values(), self.translate(original_resources), strict=True
):
resource.text = translation

references = [
atom.reference.plain_text
for atom in graph.atom_nodes.values()
for atom in arg.atom_nodes.values()
if atom.reference is not None
]
for atom, translation in zip(
graph.atom_nodes.values(), self.translate(references), strict=True
arg.atom_nodes.values(), self.translate(references), strict=True
):
if atom.reference is not None:
atom.reference.text = translation

atoms = [atom.plain_text for atom in graph.atom_nodes.values()]
atoms = [atom.plain_text for atom in arg.atom_nodes.values()]
for atom, translation in zip(
graph.atom_nodes.values(), self.translate(atoms), strict=True
arg.atom_nodes.values(), self.translate(atoms), strict=True
):
atom.text = translation

@translate.register
def _(self, graphs: Iterable[ag.Graph]) -> None:
for graph in graphs:
self.translate(graph)
def _(self, arg: Iterable[ag.Graph]) -> None:
for elem in arg:
self.translate(elem)
17 changes: 3 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ lxml = "^5.1"
typer = { version = "^0.9", extras = ["all"], optional = true }
deepl = { version = "^1.16", optional = true }
pygraphviz = { version = "^1.12", optional = true }
multimethod = { version = "^1.10", optional = true }

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.0"
Expand All @@ -35,7 +34,7 @@ lxml-stubs = "^0.5.0"
pdoc = "^15.0.0"

[tool.poetry.extras]
cli = ["typer", "deepl", "multimethod"]
cli = ["typer", "deepl"]
graphviz = ["pygraphviz"]

[tool.pytest.ini_options]
Expand Down

0 comments on commit ea4c10e

Please sign in to comment.