Skip to content

Commit a807086

Browse files
authored
Create a dataclass for Languages (#255)
1 parent 044aba7 commit a807086

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

build_docs.py

+41-29
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151
TYPE_CHECKING = False
5252
if TYPE_CHECKING:
5353
from collections.abc import Iterator, Sequence
54-
from typing import Literal, TypeAlias
55-
56-
Languages: TypeAlias = Sequence["Language"]
54+
from typing import Literal
5755

5856
try:
5957
from os import EX_OK, EX_SOFTWARE as EX_FAILURE
@@ -215,13 +213,50 @@ def picker_label(self):
215213
return self.name
216214

217215

216+
@dataclass(frozen=True, slots=True)
217+
class Languages:
218+
_seq: Sequence[Language]
219+
220+
def __iter__(self) -> Iterator[Language]:
221+
return iter(self._seq)
222+
223+
def __reversed__(self) -> Iterator[Language]:
224+
return reversed(self._seq)
225+
226+
@classmethod
227+
def from_json(cls, defaults, languages) -> Languages:
228+
default_translated_name = defaults.get("translated_name", "")
229+
default_in_prod = defaults.get("in_prod", True)
230+
default_sphinxopts = defaults.get("sphinxopts", [])
231+
default_html_only = defaults.get("html_only", False)
232+
langs = [
233+
Language(
234+
iso639_tag=iso639_tag,
235+
name=section["name"],
236+
translated_name=section.get("translated_name", default_translated_name),
237+
in_prod=section.get("in_prod", default_in_prod),
238+
sphinxopts=section.get("sphinxopts", default_sphinxopts),
239+
html_only=section.get("html_only", default_html_only),
240+
)
241+
for iso639_tag, section in languages.items()
242+
]
243+
return cls(langs)
244+
245+
def filter(self, language_tags: Sequence[str] = ()) -> Sequence[Language]:
246+
"""Filter a sequence of languages according to --languages."""
247+
if language_tags:
248+
language_tags = frozenset(language_tags)
249+
return [l for l in self if l.tag in language_tags]
250+
return list(self)
251+
252+
218253
@dataclass(order=True, frozen=True, kw_only=True)
219254
class Language:
220255
iso639_tag: str
221256
name: str
222257
translated_name: str
223258
in_prod: bool
224-
sphinxopts: tuple
259+
sphinxopts: Sequence[str]
225260
html_only: bool = False
226261

227262
@property
@@ -234,14 +269,6 @@ def switcher_label(self):
234269
return f"{self.name} | {self.translated_name}"
235270
return self.name
236271

237-
@staticmethod
238-
def filter(languages, language_tags=None):
239-
"""Filter a sequence of languages according to --languages."""
240-
if language_tags:
241-
languages_dict = {language.tag: language for language in languages}
242-
return [languages_dict[tag] for tag in language_tags]
243-
return languages
244-
245272

246273
def run(cmd, cwd=None) -> subprocess.CompletedProcess:
247274
"""Like subprocess.run, with logging before and after the command execution."""
@@ -1031,7 +1058,7 @@ def build_docs(args) -> bool:
10311058
todo = [
10321059
(version, language)
10331060
for version in versions.filter(args.branch)
1034-
for language in reversed(Language.filter(languages, args.languages))
1061+
for language in reversed(languages.filter(args.languages))
10351062
]
10361063
del args.branch
10371064
del args.languages
@@ -1104,22 +1131,7 @@ def parse_versions_from_devguide(http: urllib3.PoolManager) -> Versions:
11041131
def parse_languages_from_config() -> Languages:
11051132
"""Read config.toml to discover languages to build."""
11061133
config = tomlkit.parse((HERE / "config.toml").read_text(encoding="UTF-8"))
1107-
defaults = config["defaults"]
1108-
default_translated_name = defaults.get("translated_name", "")
1109-
default_in_prod = defaults.get("in_prod", True)
1110-
default_sphinxopts = defaults.get("sphinxopts", [])
1111-
default_html_only = defaults.get("html_only", False)
1112-
return [
1113-
Language(
1114-
iso639_tag=iso639_tag,
1115-
name=section["name"],
1116-
translated_name=section.get("translated_name", default_translated_name),
1117-
in_prod=section.get("in_prod", default_in_prod),
1118-
sphinxopts=section.get("sphinxopts", default_sphinxopts),
1119-
html_only=section.get("html_only", default_html_only),
1120-
)
1121-
for iso639_tag, section in config["languages"].items()
1122-
]
1134+
return Languages.from_json(config["defaults"], config["languages"])
11231135

11241136

11251137
def build_sitemap(versions: Versions, languages: Languages, www_root: Path, group):

0 commit comments

Comments
 (0)