Skip to content

Commit

Permalink
Add type annotations to LocaleKeySorter
Browse files Browse the repository at this point in the history
Task-number: QTBUG-128634
Pick-to: 6.8
Change-Id: I9a4261746cac029b0abf26fbd03b1915a0035147
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
  • Loading branch information
Mate Barany committed Oct 16, 2024
1 parent 93136f7 commit bd475dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion util/locale_database/qlocalexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package manager lacks the jing package.
"""

from typing import Iterator
from xml.sax.saxutils import escape

from localetools import Error, qtVersion
Expand Down Expand Up @@ -261,7 +262,7 @@ def keyLikely(pair, kl=self.__keyLikely):
what.args += (have, give)
raise

def defaultMap(self):
def defaultMap(self) -> Iterator[tuple[tuple[int, int], int]]:
"""Map language and script to their default territory by ID.
Yields ((language, script), territory) wherever the likely
Expand Down
15 changes: 8 additions & 7 deletions util/locale_database/qlocalexml2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import datetime
import argparse
from pathlib import Path
from typing import Optional
from typing import Iterator, Optional

from qlocalexml import QLocaleXmlReader
from localetools import *
Expand Down Expand Up @@ -55,12 +55,12 @@ class LocaleKeySorter:
# QLocale's likely sub-tag matching algorithms. Make sure this is
# sorting in an order compatible with those algorithms.

def __init__(self, defaults):
self.map = dict(defaults)
def foreign(self, key):
default = self.map.get(key[:2])
def __init__(self, defaults: Iterator[tuple[tuple[int, int], int]]) -> None:
self.map: dict[tuple[int, int], int] = dict(defaults)
def foreign(self, key: tuple[int, int, int]) -> bool:
default: int | None = self.map.get(key[:2])
return default is None or default != key[2]
def __call__(self, key):
def __call__(self, key: tuple[int, int, int]) -> tuple[int, bool, int, int]:
# TODO: should we compare territory before or after script ?
return (key[0], self.foreign(key)) + key[1:]

Expand Down Expand Up @@ -1010,7 +1010,8 @@ def main(argv, out, err):
reader = QLocaleXmlReader(qlocalexml)
locale_map = dict(reader.loadLocaleMap(calendars, err.write))
reader.pruneZoneNaming(locale_map, mutter)
locale_keys = sorted(locale_map.keys(), key=LocaleKeySorter(reader.defaultMap()))
locale_keys: list[tuple[int, int, int]] = sorted(locale_map.keys(),
key=LocaleKeySorter(reader.defaultMap()))

code_data = LanguageCodeData(args.iso_path)

Expand Down

0 comments on commit bd475dd

Please sign in to comment.