Repository navigation
Replace pycountry with country names from the Unicode CLDR #1634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = [ | ||
|
|
@@ -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)', | ||
| ] | ||
|
|
||
|
|
@@ -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"] | ||
|
|
@@ -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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
| """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() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.