mirror of
https://github.com/wiltodelta/remove-ai-watermarks.git
synced 2026-12-05 03:33:39 +01:00
74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
"""Generate the valid emoji flag tag codes from pinned CLDR 48.2 validity data."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import sys
|
|
import textwrap
|
|
from pathlib import Path
|
|
from xml.etree import ElementTree as ET
|
|
|
|
CLDR_REVISION = "11299982335beb974c1c63c45265184e759c0f41"
|
|
SOURCE_HASHES = {
|
|
"subdivision": "93b12c9d55938266c96d44a7ccbb66800afeef4f9dd48b0dc16edfab89833d95",
|
|
"region": "e751e0eedd46b52c38f3cdb72b0fab61ac8b48e052e8b28ba74b6ac26c4c8cb1",
|
|
}
|
|
VALID_STATUSES = {"regular", "deprecated", "macroregion"}
|
|
|
|
|
|
def read_codes(raw: bytes, kind: str) -> set[str]:
|
|
"""Expand the compact trailing-character ranges in the exact release files."""
|
|
if hashlib.sha256(raw).hexdigest() != SOURCE_HASHES[kind]:
|
|
raise ValueError(f"Use the pinned CLDR 48.2 {kind}.xml")
|
|
# Parsing is safe after the exact source digest check.
|
|
root = ET.fromstring(raw) # noqa: S314
|
|
codes = set()
|
|
for row in root.findall(f".//id[@type='{kind}']"):
|
|
if row.get("idStatus") not in VALID_STATUSES:
|
|
continue
|
|
for token in (row.text or "").lower().split():
|
|
first, sep, end = token.partition("~")
|
|
if sep:
|
|
if len(end) != 1 or first[-1] > end:
|
|
raise ValueError(f"Invalid CLDR range: {token}")
|
|
codes.update(first[:-1] + chr(cp) for cp in range(ord(first[-1]), ord(end) + 1))
|
|
else:
|
|
codes.add(first)
|
|
return codes
|
|
|
|
|
|
def render(subdivision: bytes, region: bytes) -> str:
|
|
subdivisions = read_codes(subdivision, "subdivision")
|
|
numeric_regions = {
|
|
code for code in read_codes(region, "region") if len(code) == 3 and code.isascii() and code.isdigit()
|
|
}
|
|
if len(subdivisions) != 5653 or len(numeric_regions) != 31:
|
|
raise ValueError("Unexpected CLDR 48.2 emoji flag tag code counts")
|
|
lines = textwrap.wrap(" ".join(sorted(subdivisions | numeric_regions)), width=100)
|
|
return "\n".join(
|
|
[
|
|
'"""Valid emoji flag tag codes for UTS #51 Annex C.',
|
|
"",
|
|
f"Source: https://github.com/unicode-org/cldr/tree/{CLDR_REVISION}/common/validity",
|
|
f"subdivision.xml SHA-256: {SOURCE_HASHES['subdivision']}",
|
|
f"region.xml SHA-256: {SOURCE_HASHES['region']}",
|
|
"CLDR data copyright 2004-2026 Unicode, Inc. Unicode License V3.",
|
|
"See licenses/unicode-v3.txt. Generated by scripts/generate_text_emoji_flag_codes.py.",
|
|
'"""',
|
|
"",
|
|
'_CODES = """',
|
|
*lines,
|
|
'"""',
|
|
"",
|
|
"EMOJI_FLAG_TAG_CODES = frozenset(_CODES.split())",
|
|
"",
|
|
]
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("subdivision", type=Path)
|
|
parser.add_argument("region", type=Path)
|
|
args = parser.parse_args()
|
|
sys.stdout.write(render(args.subdivision.read_bytes(), args.region.read_bytes()))
|