Add type hints to LocaleSourceEditor and TimeZoneDataWriter

Task-number: QTBUG-128634
Change-Id: I5dabb5e721b610bc9edb01b86aa173d03c36a48a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit dd698dcb4d14ec59fdd60416018b15e43438ecfe)
This commit is contained in:
Mate Barany 2024-09-19 17:28:20 +02:00
parent 0d06c01611
commit 52909797ac

View File

@ -19,7 +19,7 @@ import argparse
from pathlib import Path from pathlib import Path
from typing import Callable, Iterator, Optional from typing import Callable, Iterator, Optional
from qlocalexml import QLocaleXmlReader from qlocalexml import Locale, QLocaleXmlReader
from localetools import * from localetools import *
from iso639_3 import LanguageCodeData from iso639_3 import LanguageCodeData
from zonedata import utcIdList, windowsIdList from zonedata import utcIdList, windowsIdList
@ -161,7 +161,7 @@ def currencyIsoCodeData(s: str) -> str:
return "{0,0,0}" return "{0,0,0}"
class LocaleSourceEditor (SourceFileEditor): class LocaleSourceEditor (SourceFileEditor):
def __init__(self, path: Path, temp: Path, version: str): def __init__(self, path: Path, temp: Path, version: str) -> None:
super().__init__(path, temp) super().__init__(path, temp)
self.version = version self.version = version
@ -182,7 +182,7 @@ class LocaleSourceEditor (SourceFileEditor):
""") """)
class TimeZoneDataWriter (LocaleSourceEditor): class TimeZoneDataWriter (LocaleSourceEditor):
def __init__(self, path: Path, temp: Path, version: str): def __init__(self, path: Path, temp: Path, version: str) -> None:
super().__init__(path, temp, version) super().__init__(path, temp, version)
self.__ianaTable = ByteArrayData() # Single IANA IDs self.__ianaTable = ByteArrayData() # Single IANA IDs
self.__ianaListTable = ByteArrayData() # Space-joined lists of IDs self.__ianaListTable = ByteArrayData() # Space-joined lists of IDs
@ -192,23 +192,25 @@ class TimeZoneDataWriter (LocaleSourceEditor):
self.windowsKey = {name: (key, off) for key, (name, off) self.windowsKey = {name: (key, off) for key, (name, off)
in enumerate(self.__windowsList, 1)} in enumerate(self.__windowsList, 1)}
def utcTable(self): def utcTable(self) -> None:
offsetMap, out = {}, self.writer.write offsetMap: dict[int, tuple[str, ...]] = {}
out: Callable[[str], int] = self.writer.write
for name in utcIdList: for name in utcIdList:
offset = self.__offsetOf(name) offset: int = self.__offsetOf(name)
offsetMap[offset] = offsetMap.get(offset, ()) + (name,) offsetMap[offset] = offsetMap.get(offset, ()) + (name,)
# Write UTC ID key table # Write UTC ID key table
out('// IANA ID Index, UTC Offset\n') out('// IANA ID Index, UTC Offset\n')
out('static constexpr UtcData utcDataTable[] = {\n') out('static constexpr UtcData utcDataTable[] = {\n')
for offset in sorted(offsetMap.keys()): # Sort so C++ can binary-chop. for offset in sorted(offsetMap.keys()): # Sort so C++ can binary-chop.
names = offsetMap[offset]; names: tuple[str, ...] = offsetMap[offset]
joined = self.__ianaListTable.append(' '.join(names)) joined: int = self.__ianaListTable.append(' '.join(names))
out(f' {{ {joined:6d},{offset:6d} }}, // {names[0]}\n') out(f' {{ {joined:6d},{offset:6d} }}, // {names[0]}\n')
out('};\n') out('};\n')
def aliasToIana(self, pairs): def aliasToIana(self, pairs: Iterator[tuple[str, str]]) -> None:
out, store = self.writer.write, self.__ianaTable.append out: Callable[[str], int] = self.writer.write
store: Callable[[str], int] = self.__ianaTable.append
out('// Alias ID Index, Alias ID Index\n') out('// Alias ID Index, Alias ID Index\n')
out('static constexpr AliasData aliasMappingTable[] = {\n') out('static constexpr AliasData aliasMappingTable[] = {\n')
@ -218,10 +220,11 @@ class TimeZoneDataWriter (LocaleSourceEditor):
f' // {name} -> {iana}\n') f' // {name} -> {iana}\n')
out('};\n\n') out('};\n\n')
def msToIana(self, pairs): def msToIana(self, pairs: Iterator[tuple[str, str]]) -> None:
out, winStore = self.writer.write, self.__windowsTable.append out: Callable[[str], int] = self.writer.write
ianaStore = self.__ianaListTable.append # TODO: Should be __ianaTable winStore: Callable[[str], int] = self.__windowsTable.append
alias = dict(pairs) # {MS name: IANA ID} ianaStore: Callable[[str], int] = self.__ianaTable.append
alias: dict[str, str] = dict(pairs) # {MS name: IANA ID}
out('// Windows ID Key, Windows ID Index, IANA ID Index, UTC Offset\n') out('// Windows ID Key, Windows ID Index, IANA ID Index, UTC Offset\n')
out('static constexpr WindowsData windowsDataTable[] = {\n') out('static constexpr WindowsData windowsDataTable[] = {\n')
@ -232,12 +235,16 @@ class TimeZoneDataWriter (LocaleSourceEditor):
f'{ianaStore(alias[name]):6d},{offset:6d} }}, // {name}\n') f'{ianaStore(alias[name]):6d},{offset:6d} }}, // {name}\n')
out('};\n\n') out('};\n\n')
def msLandIanas(self, triples): # (MS name, territory code, IANA list) def msLandIanas(self, triples: Iterator[tuple[str, str, str]]) -> None:
out, store = self.writer.write, self.__ianaListTable.append # triples (MS name, territory code, IANA list)
out: Callable[[str], int] = self.writer.write
store: Callable[[str], int] = self.__ianaListTable.append
from enumdata import territory_map from enumdata import territory_map
landKey = {code: (i, name) for i, (name, code) in territory_map.items()} landKey: dict[str, tuple[int, str]] = {code: (i, name) for i, (name, code)
seq = sorted((self.windowsKey[name][0], landKey[land][0], name, landKey[land][1], ianas) in territory_map.items()}
for name, land, ianas in triples) seq: list[tuple[int, int, str, str, str]] = sorted(
(self.windowsKey[name][0], landKey[land][0], name, landKey[land][1], ianas)
for name, land, ianas in triples)
out('// Windows ID Key, Territory Enum, IANA ID Index\n') out('// Windows ID Key, Territory Enum, IANA ID Index\n')
out('static constexpr ZoneData zoneDataTable[] = {\n') out('static constexpr ZoneData zoneDataTable[] = {\n')
@ -247,7 +254,7 @@ class TimeZoneDataWriter (LocaleSourceEditor):
f' // {name} / {land}\n') f' // {name} / {land}\n')
out('};\n\n') out('};\n\n')
def writeTables(self): def writeTables(self) -> None:
self.__windowsTable.write(self.writer.write, 'windowsIdData') self.__windowsTable.write(self.writer.write, 'windowsIdData')
# TODO: these are misnamed, entries in the first are lists, # TODO: these are misnamed, entries in the first are lists,
# those in the next are single IANA IDs # those in the next are single IANA IDs
@ -256,7 +263,7 @@ class TimeZoneDataWriter (LocaleSourceEditor):
# Implementation details: # Implementation details:
@staticmethod @staticmethod
def __offsetOf(utcName): def __offsetOf(utcName: str) -> int:
"Maps a UTC±HH:mm name to its offset in seconds" "Maps a UTC±HH:mm name to its offset in seconds"
assert utcName.startswith('UTC') assert utcName.startswith('UTC')
if len(utcName) == 3: if len(utcName) == 3: