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)
| 26 | |
| 27 | |
| 28 | def 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 | |
| 61 | def get_all_codepoints(version: str = UNICODE_VERSION) -> List[int]: |
| 62 | """Return all assigned/defined codepoints in Unicode.""" |
no test coverage detected
searching dependent graphs…