MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / get_unicode_xml_data

Function get_unicode_xml_data

scripts/test_helpers.py:28–59  ·  view source on GitHub ↗

Download and parse Unicode UCD XML, caching in temp directory. Args: version: Unicode version string (e.g., "17.0.0") Returns: Root Element of the parsed XML tree

(version: str = UNICODE_VERSION)

Source from the content-addressed store, hash-verified

26
27
28def get_unicode_xml_data(version: str = UNICODE_VERSION) -> ET.Element:
29 """Download and parse Unicode UCD XML, caching in temp directory.
30
31 Args:
32 version: Unicode version string (e.g., "17.0.0")
33
34 Returns:
35 Root Element of the parsed XML tree
36 """
37 cache_path = os.path.join(tempfile.gettempdir(), f"ucd-{version}.all.flat.xml")
38
39 if not os.path.exists(cache_path):
40 url = f"https://www.unicode.org/Public/{version}/ucdxml/ucd.all.flat.zip"
41 print(f"Downloading Unicode {version} UCD XML from {url}...")
42 try:
43 with urllib.request.urlopen(url) as response:
44 zip_data = response.read()
45 zip_bytes = io.BytesIO(zip_data)
46 with zipfile.ZipFile(zip_bytes) as zf:
47 xml_filename = zf.namelist()[0]
48 with zf.open(xml_filename) as xml_file:
49 xml_content = xml_file.read()
50 with open(cache_path, "wb") as f:
51 f.write(xml_content)
52 print(f"Cached to {cache_path}")
53 except Exception as e:
54 raise UnicodeDataDownloadError(f"Could not download UCD XML from {url}: {e}")
55 else:
56 print(f"Using cached Unicode {version} UCD XML: {cache_path}")
57
58 tree = ET.parse(cache_path)
59 return tree.getroot()
60
61def get_all_codepoints(version: str = UNICODE_VERSION) -> List[int]:
62 """Return all assigned/defined codepoints in Unicode."""

Callers 1

get_all_codepointsFunction · 0.85

Calls 2

writeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…