halkyon/packages/halkyon_app/tool/subdivision_names.py
Tizian 「ティツィアーン」 af1dbee509
build: Melos, a Dart check and every path for the single repository
- Melos 8.9 as the root's dev dependency, with scripts across the
  workspace (analyze, test:dart, test:flutter, generate, check) and its
  filters (--diff, --include-dependents); cli_util overridden to 0.5.2
  for it, since Learn's flutter_launcher_icons asks for 0.4 and uses only
  its logger, unchanged in 0.5.
- tool/check.dart in place of tool/check.sh: packages, formatting of our
  own files, analysis, the tools' tests, then every package's tests
  through Melos (--changed-since=<commit> for only what changed and what
  depends on it). tool/pre-push is the hook: the signature check, then
  the checks. CI generates every server's code and runs the same check.
- The images build from the root with a workspace narrowed to what they
  need (tool/workspace_only.sh); compose, the deploy's migration test,
  the snap, the release script, the editor's launch files and Learn's
  asset tools (run from apps/learn) follow the new layout.
- The project's source is named as project-halkyon/halkyon (source link,
  snap, F-Droid listing, release uploads); the AGPL packages carry the
  licence's own text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-22 18:11:04 +02:00

66 lines
2.3 KiB
Python

"""Writes halkyon_app's subdivision names from a release of Unicode CLDR.
The names of states, regions and provinces (ISO 3166-2) that
`AssetSubdivisionNames` reads, in English and German, from CLDR's
`common/subdivisions/` at a tagged release, with CLDR's licence beside them:
python3 -I packages/halkyon_app/tool/subdivision_names.py [release-48-2]
CLDR names a subdivision by its country and code in lower case without the
hyphen (`usca`); the asset keys it as ISO 3166-2 does (`US-CA`). English is
kept whole; German only where it differs, as the app falls back to English.
Variants (`alt`) are left out. Run it again with a newer tag to update both
files, and say the release in `assets/subdivisions/ATTRIBUTION.md`.
"""
import json
import re
import sys
import urllib.request
import xml.etree.ElementTree as ElementTree
from pathlib import Path
RELEASE = sys.argv[1] if len(sys.argv) > 1 else 'release-48-2'
LANGUAGES = ('en', 'de')
SOURCE = 'https://raw.githubusercontent.com/unicode-org/cldr/' + RELEASE + '/'
ASSETS = Path(__file__).resolve().parent.parent / 'assets' / 'subdivisions'
CODE = re.compile(r'^[A-Z]{2}-[A-Z0-9]{1,3}$')
def fetch(path):
with urllib.request.urlopen(SOURCE + path, timeout=60) as response:
return response.read()
def names_in(language):
root = ElementTree.fromstring(fetch('common/subdivisions/' + language + '.xml'))
names = {}
for element in root.iter('subdivision'):
if 'alt' in element.attrib or not (element.text or '').strip():
continue
cldr = element.attrib['type']
code = cldr[:2].upper() + '-' + cldr[2:].upper()
if CODE.match(code):
names[code] = element.text.strip()
return names
def main():
english = names_in('en')
names = {'release': RELEASE, 'en': english}
for language in LANGUAGES[1:]:
names[language] = {
code: name
for code, name in names_in(language).items()
if english.get(code) != name
}
ASSETS.mkdir(parents=True, exist_ok=True)
(ASSETS / 'subdivisions.json').write_text(
json.dumps(names, ensure_ascii=False, sort_keys=True, separators=(',', ':')) + '\n',
encoding='utf-8',
)
(ASSETS / 'LICENSE-CLDR.txt').write_bytes(fetch('LICENSE'))
print(RELEASE, {language: len(names[language]) for language in LANGUAGES})
main()