From 2c2ec9242843ecd4ebf91c9fd0196f15c7564a5f Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Sat, 7 Dec 2024 23:46:59 +0330 Subject: [PATCH] change `entry.detectDefiFormat` to accept `default` and return `str` --- pyglossary/entry.py | 24 +++++++++++++++--------- pyglossary/glossary_types.py | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pyglossary/entry.py b/pyglossary/entry.py index 872263a3d..7f6ad51ee 100644 --- a/pyglossary/entry.py +++ b/pyglossary/entry.py @@ -299,15 +299,21 @@ def defiFormat(self, defiFormat: str) -> None: """ self._defiFormat = defiFormat - def detectDefiFormat(self) -> None: - if self._defiFormat != "m": - return - if Entry.xdxfPattern.match(self.defi): - self._defiFormat = "x" - return - if Entry.htmlPattern.match(self.defi): - self._defiFormat = "h" - return + def detectDefiFormat(self, default: str = "") -> str: + if self._defiFormat == "h": + return "h" + if self._defiFormat == "x": + return "x" + if self._defiFormat == "m": + if Entry.xdxfPattern.match(self.defi): + self._defiFormat = "x" + return "x" + if Entry.htmlPattern.match(self.defi): + self._defiFormat = "h" + return "h" + return "m" + log.error(f"invalid defiFormat={self._defiFormat}, using {default!r}") + return default def byteProgress(self) -> tuple[int, int] | None: return self._byteProgress diff --git a/pyglossary/glossary_types.py b/pyglossary/glossary_types.py index 1c2ce790e..401745a91 100644 --- a/pyglossary/glossary_types.py +++ b/pyglossary/glossary_types.py @@ -79,7 +79,7 @@ def defiFormat(self, defiFormat: str) -> None: # TODO: type: Literal["m", "h", "x", "b"] ... - def detectDefiFormat(self) -> None: ... + def detectDefiFormat(self, default: str = "") -> str: ... def addAlt(self, alt: str) -> None: ...