Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ repos:
name: "\u2705 Check code for common misspellings"
language: system
types: [text]
# Station corrections hold names, tags and URLs in many languages,
# not English prose: "Cadena SER" is a name, not a typo.
exclude: ^(poetry\.lock|src/radios/corrections/.+\.json)$
# Station corrections and country names are data in many languages,
# not English prose: "Cadena SER" is a name, and "FO" a country code.
exclude: ^(poetry\.lock|src/radios/corrections/.+\.json|src/radios/country_names\.py)$
entry: poetry run codespell
- id: detect-private-key
name: "\U0001F575\uFE0F Detect Private Keys"
Expand Down
7 changes: 5 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ manager exits.
| ` const.py` | Enums, supported orders, and language flags |
| ` exceptions.py` | `RadioBrowserError` and its connection subclasses |
| ` corrections/` | Station corrections: loader, schema, data per country |
| ` country_names.py` | Country names by code, generated from the Unicode CLDR |
| `tests/` | pytest suite; each test has a one-line docstring |
| `examples/` | Runnable example against the live API |
| `scripts/` | Maintenance scripts, like the country names generator |

## Commands

Expand Down Expand Up @@ -90,8 +92,9 @@ See [AI_POLICY.md](AI_POLICY.md) for the contribution policy around AI tooling.
that is what the API expects.
- The API returns countries as ISO 3166-1 alpha-2 codes, and lists a few of
them twice, once in lowercase. `countries()` fetches the whole list, merges
those, resolves the codes to names with `pycountry` (with a special case for
Kosovo, `XK`), and sorts and pages locally.
those, resolves the codes to names with `COUNTRY_NAMES`, and sorts and pages
locally. `country_names.py` is generated from the Unicode CLDR by
`scripts/update_country_names.py`; do not edit it by hand.
- The API uses names of its own for countries in some places, like "The
Netherlands" for states. `states()` looks those up from a country code.
- Not every endpoint can sort by every `Order`. `LIST_ORDERS` and
Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
description = "Asynchronous Python client for the Radio Browser API."
authors = [{ name = "Franck Nijhof", email = "[email protected]" }]
maintainers = [{ name = "Franck Nijhof", email = "[email protected]" }]
license = "MIT"
license = "MIT AND Unicode-3.0"
requires-python = ">=3.12"
readme = "README.md"
keywords = [
Expand Down Expand Up @@ -41,7 +41,6 @@ dependencies = [
'orjson (>=3.9.8)',
'probatio (>=0.13.0)',
'pycares (>=5.0.0)',
'pycountry (>=24.0.0)',
'yarl (>=1.6.0)',
]

Expand Down Expand Up @@ -71,6 +70,7 @@ syrupy = "6.1.1"
ty = "0.0.84"
yamllint = "1.38.0"
zizmor = "1.30.1"
pycountry = "24.6.1"

[tool.coverage.run]
plugins = ["covdefaults"]
Expand Down Expand Up @@ -127,6 +127,9 @@ ignore = [
"FBT001", # Boolean positional args are idiomatic in parametrized tests
"FBT002", # Boolean defaults are idiomatic in parametrized tests
]
"scripts/*.py" = [
"INP001", # Standalone scripts, run by hand, not a package
]

[tool.ruff.lint.flake8-pytest-style]
mark-parentheses = false
Expand Down
82 changes: 82 additions & 0 deletions scripts/update_country_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Generate src/radios/country_names.py from the Unicode CLDR.

The country names come from the English locale of the Unicode Common Locale
Data Repository (CLDR), the data browsers and operating systems use too. The
codes are the ISO 3166-1 alpha-2 codes pycountry knows, plus Kosovo, which has
a user-assigned code the Radio Browser API uses.

Run it with `poetry run python scripts/update_country_names.py`.
"""

import json
import pathlib
import urllib.request

import pycountry

CLDR_VERSION = "48.2.3"
CLDR_BASE = f"https://raw.githubusercontent.com/unicode-org/cldr-json/{CLDR_VERSION}"
CLDR_URL = f"{CLDR_BASE}/cldr-json/cldr-localenames-full/main/en/territories.json"
LICENSE_URL = f"{CLDR_BASE}/LICENSE"
OUTPUT = pathlib.Path(__file__).parent.parent / "src" / "radios" / "country_names.py"

# Where the CLDR default name reads awkwardly in a list of stations, the short
# or variant name CLDR also has is used instead.
ALTERNATIVES = {
"CD": "CD-alt-variant", # Congo (DRC), instead of Congo - Kinshasa
"CG": "CG-alt-variant", # Congo (Republic), instead of Congo - Brazzaville
"HK": "HK-alt-short", # Hong Kong, instead of Hong Kong SAR China
"MM": "MM-alt-short", # Myanmar, instead of Myanmar (Burma)
"MO": "MO-alt-short", # Macao, instead of Macao SAR China
"PS": "PS-alt-short", # Palestine, instead of Palestinian Territories
}


def main() -> None:
Comment thread
frenck marked this conversation as resolved.
Resolved
"""Fetch the country names, and write them out as a Python module."""
with urllib.request.urlopen(CLDR_URL, timeout=30) as response: # noqa: S310
territories = json.load(response)["main"]["en"]["localeDisplayNames"][
"territories"
]

# The Unicode license asks for its notice to come with every copy of the
# data, so it goes into the generated file.
with urllib.request.urlopen(LICENSE_URL, timeout=30) as response: # noqa: S310
notice = response.read().decode().strip()

codes = sorted({country.alpha_2 for country in pycountry.countries} | {"XK"})
# CLDR uses a typographic apostrophe (U+2019), like in Côte d'Ivoire. A
# plain one is what people type when they search or sort.
names = {
code: territories[ALTERNATIVES.get(code, code)].replace("\u2019", "'")
for code in codes
}

license_comment = "\n".join(f"# {line}".rstrip() for line in notice.splitlines())

# A JSON string is a valid Python string, in the double quotes ruff wants.
body = "\n".join(
f" {json.dumps(code)}: {json.dumps(name, ensure_ascii=False)},"
for code, name in names.items()
)
OUTPUT.write_text(
f'''"""English names of countries, by ISO 3166-1 alpha-2 code.

Generated by scripts/update_country_names.py from the Unicode CLDR
{CLDR_VERSION}. Do not edit this file by hand, run that script instead.

The names are data from the Unicode CLDR, under the Unicode License V3:
"""

{license_comment}

COUNTRY_NAMES: dict[str, str] = {{
{body}
}}
''',
encoding="utf-8",
)


if __name__ == "__main__":
main()
Loading
Loading