| 1 |
import json |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
from collections import OrderedDict |
| 5 |
|
| 6 |
import regex as re |
| 7 |
from ruamel.yaml import RoundTripLoader |
| 8 |
|
| 9 |
from dateparser_scripts.order_languages import avoid_languages |
| 10 |
from dateparser_scripts.utils import combine_dicts |
| 11 |
|
| 12 |
cldr_date_directory = "../dateparser_data/cldr_language_data/date_translation_data/" |
| 13 |
supplementary_directory = "../dateparser_data/supplementary_language_data/" |
| 14 |
supplementary_date_directory = ( |
| 15 |
"../dateparser_data/supplementary_language_data/date_translation_data/" |
| 16 |
) |
| 17 |
translation_data_directory = "../dateparser/data/" |
| 18 |
date_translation_directory = "../dateparser/data/date_translation_data/" |
| 19 |
|
| 20 |
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
| 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 |
|
| 30 |
|
| 31 |
def _modify_relative_data(relative_data): |
| 32 |
modified_relative_data = OrderedDict() |
| 33 |
for key, value in relative_data.items(): |
| 34 |
for i, string in enumerate(value): |
| 35 |
string = RELATIVE_PATTERN.sub(r"(\\d+[.,]?\\d*)", string) |
| 36 |
value[i] = string |
| 37 |
modified_relative_data[key] = value |
| 38 |
return modified_relative_data |
| 39 |
|
| 40 |
|
| 41 |
def _modify_data(language_data): |
| 42 |
relative_data = language_data.get("relative-type-regex", {}) |
| 43 |
relative_data = _modify_relative_data(relative_data) |
| 44 |
locale_specific_data = language_data.get("locale_specific", {}) |
| 45 |
for _, info in locale_specific_data.items(): |
| 46 |
locale_relative_data = info.get("relative-type-regex", {}) |
| 47 |
locale_relative_data = _modify_relative_data(locale_relative_data) |
| 48 |
|
| 49 |
|
| 50 |
def _get_complete_date_translation_data(language): |
| 51 |
cldr_data = {} |
| 52 |
supplementary_data = {} |
| 53 |
if language in cldr_languages: |
| 54 |
with open(cldr_date_directory + language + ".json") as f: |
| 55 |
cldr_data = json.load(f, object_pairs_hook=OrderedDict) |
| 56 |
if language in supplementary_languages: |
| 57 |
with open(supplementary_date_directory + language + ".yaml") as g: |
| 58 |
supplementary_data = OrderedDict(RoundTripLoader(g).get_data()) |
| 59 |
complete_data = combine_dicts(cldr_data, supplementary_data) |
| 60 |
if "name" not in complete_data: |
| 61 |
complete_data["name"] = language |
| 62 |
return complete_data |
| 63 |
|
| 64 |
|
| 65 |
def _write_file(filename, text, mode, in_memory, in_memory_result): |
| 66 |
if in_memory: |
| 67 |
in_memory_result[filename] = text |
| 68 |
else: |
| 69 |
with open(filename, mode) as out: |
| 70 |
out.write(text) |
| 71 |
|
| 72 |
|
| 73 |
def write_complete_data(in_memory=False): |
| 74 |
""" |
| 75 |
This function is responsible of generating the needed py files from the |
| 76 |
CLDR files (JSON format) and supplementary language data (YAML format). |
| 77 |
|
| 78 |
Use it with in_memory=True to avoid writing real files and getting a |
| 79 |
dictionary containing the file names and their content (used when testing). |
| 80 |
""" |
| 81 |
in_memory_result = {} |
| 82 |
|
| 83 |
if not in_memory: |
| 84 |
if not os.path.isdir(translation_data_directory): |
| 85 |
os.mkdir(translation_data_directory) |
| 86 |
if os.path.isdir(date_translation_directory): |
| 87 |
shutil.rmtree(date_translation_directory) |
| 88 |
os.mkdir(date_translation_directory) |
| 89 |
|
| 90 |
with open(supplementary_directory + "base_data.yaml") as f: |
| 91 |
base_data = RoundTripLoader(f).get_data() |
| 92 |
|
| 93 |
for language in all_languages: |
| 94 |
date_translation_data = _get_complete_date_translation_data(language) |
| 95 |
date_translation_data = combine_dicts(date_translation_data, base_data) |
| 96 |
_modify_data(date_translation_data) |
| 97 |
translation_data = json.dumps( |
| 98 |
date_translation_data, indent=4, separators=(",", ": "), ensure_ascii=False |
| 99 |
) |
| 100 |
out_text = ("info = " + translation_data + "\n").encode("utf-8") |
| 101 |
_write_file( |
| 102 |
date_translation_directory + language + ".py", |
| 103 |
out_text, |
| 104 |
"wb", |
| 105 |
in_memory, |
| 106 |
in_memory_result, |
| 107 |
) |
| 108 |
|
| 109 |
init_text = ( |
| 110 |
"from dateparser.data import date_translation_data\n" |
| 111 |
"from .languages_info import language_order, language_locale_dict\n" |
| 112 |
) |
| 113 |
|
| 114 |
_write_file( |
| 115 |
translation_data_directory + "__init__.py", |
| 116 |
init_text, |
| 117 |
"w", |
| 118 |
False, |
| 119 |
in_memory_result, |
| 120 |
) |
| 121 |
_write_file( |
| 122 |
date_translation_directory + "__init__.py", "", "w", False, in_memory_result |
| 123 |
) |
| 124 |
|
| 125 |
return in_memory_result |
| 126 |
|
| 127 |
|
| 128 |
if __name__ == "__main__": |
| 129 |
write_complete_data() |