| 1 |
import json |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import regex as re |
| 7 |
from ruamel.yaml import YAML |
| 8 |
|
| 9 |
from dateparser_scripts.order_languages import avoid_languages |
| 10 |
from dateparser_scripts.utils import combine_dicts |
| 11 |
|
| 12 |
root = Path(__file__).parent.parent |
| 13 |
|
| 14 |
cldr_date_directory = root / "dateparser_data/cldr_language_data/date_translation_data" |
| 15 |
supplementary_directory = root / "dateparser_data/supplementary_language_data" |
| 16 |
supplementary_date_directory = ( |
| 17 |
root / "dateparser_data/supplementary_language_data/date_translation_data" |
| 18 |
) |
| 19 |
translation_data_directory = root / "dateparser/data" |
| 20 |
date_translation_directory = root / "dateparser/data/date_translation_data" |
| 21 |
|
| 22 |
cldr_languages = list( |
| 23 |
set(map(lambda x: x[:-5], os.listdir(cldr_date_directory))) - avoid_languages |
| 24 |
) |
| 25 |
supplementary_languages = [x[:-5] for x in os.listdir(supplementary_date_directory)] |
| 26 |
all_languages = set(cldr_languages).union(set(supplementary_languages)) |
| 27 |
|
| 28 |
RELATIVE_PATTERN = re.compile(r"\{0\}") |
| 29 |
POSSESSIVE_DIGIT_PATTERN = re.compile(r"\\d\+\[\.,\]\?\\d\*") |
| 30 |
|
| 31 |
|
| 32 |
def _make_possessive(s): |
| 33 |
return POSSESSIVE_DIGIT_PATTERN.sub(r"\\d++[.,]?\\d*+", s) |
| 34 |
|
| 35 |
|
| 36 |
def _to_plain_types(obj): |
| 37 |
"""Recursively convert ruamel.yaml CommentedMap/CommentedSeq to plain |
| 38 |
dict/list so that json.dumps produces stable output across all |
| 39 |
Python versions. |
| 40 |
|
| 41 |
Python 3.14 changed the json C encoder to bypass the Python-level |
| 42 |
``__iter__``/``items()`` of dict subclasses and access the underlying C |
| 43 |
dict directly. ruamel.yaml's CommentedMap relies on its Python-level |
| 44 |
iteration for correct key ordering, so the C shortcut produces a |
| 45 |
different (non-deterministic) key order on 3.14. Converting to plain |
| 46 |
types before serialisation avoids the issue entirely. |
| 47 |
""" |
| 48 |
if isinstance(obj, dict): |
| 49 |
return {k: _to_plain_types(v) for k, v in obj.items()} |
| 50 |
elif isinstance(obj, list): |
| 51 |
return [_to_plain_types(v) for v in obj] |
| 52 |
return obj |
| 53 |
|
| 54 |
|
| 55 |
def _modify_relative_data(relative_data): |
| 56 |
modified_relative_data = {} |
| 57 |
for key, value in relative_data.items(): |
| 58 |
for i, string in enumerate(value): |
| 59 |
string = RELATIVE_PATTERN.sub(r"(\\d++[.,]?\\d*+)", string) |
| 60 |
string = _make_possessive(string) |
| 61 |
value[i] = string |
| 62 |
modified_relative_data[key] = value |
| 63 |
return modified_relative_data |
| 64 |
|
| 65 |
|
| 66 |
def _modify_simplifications(simplifications): |
| 67 |
for simplification in simplifications: |
| 68 |
for pattern in list(simplification.keys()): |
| 69 |
new_pattern = _make_possessive(pattern) |
| 70 |
if new_pattern != pattern: |
| 71 |
simplification[new_pattern] = simplification.pop(pattern) |
| 72 |
|
| 73 |
|
| 74 |
def _modify_data(language_data): |
| 75 |
relative_data = language_data.get("relative-type-regex", {}) |
| 76 |
relative_data = _modify_relative_data(relative_data) |
| 77 |
simplifications = language_data.get("simplifications", []) |
| 78 |
_modify_simplifications(simplifications) |
| 79 |
locale_specific_data = language_data.get("locale_specific", {}) |
| 80 |
for _, info in locale_specific_data.items(): |
| 81 |
locale_relative_data = info.get("relative-type-regex", {}) |
| 82 |
locale_relative_data = _modify_relative_data(locale_relative_data) |
| 83 |
|
| 84 |
|
| 85 |
def _get_complete_date_translation_data(language): |
| 86 |
cldr_data = {} |
| 87 |
supplementary_data = {} |
| 88 |
if language in cldr_languages: |
| 89 |
with open(cldr_date_directory / f"{language}.json") as f: |
| 90 |
cldr_data = json.load(f) |
| 91 |
if language in supplementary_languages: |
| 92 |
with open(supplementary_date_directory / f"{language}.yaml") as g: |
| 93 |
yaml = YAML() |
| 94 |
supplementary_data = dict(yaml.load(g)) |
| 95 |
complete_data = combine_dicts(cldr_data, supplementary_data) |
| 96 |
if "name" not in complete_data: |
| 97 |
complete_data["name"] = language |
| 98 |
return complete_data |
| 99 |
|
| 100 |
|
| 101 |
def _write_file(filename, text, mode, in_memory, in_memory_result): |
| 102 |
if in_memory: |
| 103 |
in_memory_result[filename] = text |
| 104 |
else: |
| 105 |
with open(filename, mode) as out: |
| 106 |
out.write(text) |
| 107 |
|
| 108 |
|
| 109 |
def write_complete_data(in_memory=False): |
| 110 |
""" |
| 111 |
This function is responsible of generating the needed py files from the |
| 112 |
CLDR files (JSON format) and supplementary language data (YAML format). |
| 113 |
|
| 114 |
Use it with in_memory=True to avoid writing real files and getting a |
| 115 |
dictionary containing the file names and their content (used when testing). |
| 116 |
""" |
| 117 |
in_memory_result = {} |
| 118 |
|
| 119 |
if not in_memory: |
| 120 |
if not os.path.isdir(translation_data_directory): |
| 121 |
os.mkdir(translation_data_directory) |
| 122 |
if os.path.isdir(date_translation_directory): |
| 123 |
shutil.rmtree(date_translation_directory) |
| 124 |
os.mkdir(date_translation_directory) |
| 125 |
|
| 126 |
with open(supplementary_directory / "base_data.yaml") as f: |
| 127 |
yaml = YAML() |
| 128 |
base_data = yaml.load(f) |
| 129 |
|
| 130 |
for language in all_languages: |
| 131 |
date_translation_data = _get_complete_date_translation_data(language) |
| 132 |
date_translation_data = combine_dicts(date_translation_data, base_data) |
| 133 |
date_translation_data = _to_plain_types(date_translation_data) |
| 134 |
_modify_data(date_translation_data) |
| 135 |
translation_data = json.dumps( |
| 136 |
date_translation_data, indent=4, separators=(",", ": "), ensure_ascii=False |
| 137 |
) |
| 138 |
out_text = ("info = " + translation_data + "\n").encode("utf-8") |
| 139 |
_write_file( |
| 140 |
date_translation_directory / f"{language}.py", |
| 141 |
out_text, |
| 142 |
"wb", |
| 143 |
in_memory, |
| 144 |
in_memory_result, |
| 145 |
) |
| 146 |
|
| 147 |
init_text = ( |
| 148 |
"from dateparser.data import date_translation_data\n" |
| 149 |
"\n" |
| 150 |
"from .languages_info import language_locale_dict, language_order\n" |
| 151 |
) |
| 152 |
|
| 153 |
_write_file( |
| 154 |
translation_data_directory / "__init__.py", |
| 155 |
init_text, |
| 156 |
"w", |
| 157 |
False, |
| 158 |
in_memory_result, |
| 159 |
) |
| 160 |
_write_file( |
| 161 |
date_translation_directory / "__init__.py", "", "w", False, in_memory_result |
| 162 |
) |
| 163 |
|
| 164 |
return in_memory_result |
| 165 |
|
| 166 |
|
| 167 |
if __name__ == "__main__": |
| 168 |
write_complete_data() |