-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathwrite_csv.py
More file actions
113 lines (95 loc) · 3.37 KB
/
write_csv.py
File metadata and controls
113 lines (95 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from global_var import (
DisplayCJKTablesList,
CJKGroup,
CJKTable,
DisplayLanguage,
DisplayUnicodeBlocksList,
)
from localise import get_localised_label
import csv
from pathlib import Path
def write(
output_fullpath: Path, cjk_char_count, unicode_char_count, lang: DisplayLanguage
):
output_file = output_fullpath.open("w", encoding="utf-8", newline="")
# newline="" lets csv module control newlines
output_writer = csv.DictWriter(
output_file, ("name", "count", "full_size"), quotechar="'",
)
cjk_output_order = [CJKGroup.FAN, CJKGroup.JIANFAN, CJKGroup.JIAN]
if lang == DisplayLanguage.ZHS:
cjk_output_order = [CJKGroup.JIAN, CJKGroup.JIANFAN, CJKGroup.FAN]
for table_group in cjk_output_order:
write_table(
output_writer,
lang,
get_localised_label(lang, "section_titles")[table_group],
DisplayCJKTablesList.get_ordered_tables_in_group(table_group),
cjk_char_count,
)
output_file.write("\n")
output_file.write("\n")
write_block(
output_writer,
lang,
unicode_char_count,
)
output_file.write("\n")
output_file.close()
def write_table(
output_writer: csv.DictWriter,
lang: DisplayLanguage,
title: str,
name_list: dict[str, CJKTable],
count_arr: dict[str, int],
):
output_writer.writerow({"name": "===", "count": title, "full_size": "==="})
# language localization header
if lang == DisplayLanguage.ZHS:
csv_header = {"name": "#名称", "count": "计数", "full_size": "总数"}
elif lang == DisplayLanguage.ZHT:
csv_header = {"name": "#名稱", "count": "計數", "full_size": "總數"}
else:
csv_header = {"name": "#name", "count": "count", "full_size": "full_size"}
output_writer.writerow(csv_header)
# contents
write_dict = []
for varname, table in name_list.items():
write_dict.append(
{
"name": '"' + table.localised_name(lang) + '"',
"count": count_arr[varname],
"full_size": table.count,
}
)
output_writer.writerows(write_dict)
def write_block(
output_writer: csv.DictWriter,
lang: DisplayLanguage,
count_arr: dict[str, int],
):
output_writer.writerow(
{
"name": "===",
"count": get_localised_label(lang, "section_titles.uni"),
"full_size": "===",
}
)
# language localization header
if lang == DisplayLanguage.ZHS:
csv_header = {"name": "#名称", "count": "计数", "full_size": "总数"}
elif lang == DisplayLanguage.ZHT:
csv_header = {"name": "#名稱", "count": "計數", "full_size": "總數"}
else:
csv_header = {"name": "#name", "count": "count", "full_size": "full_size"}
output_writer.writerow(csv_header)
write_dict = []
for varname, block in DisplayUnicodeBlocksList.get_ordered_blocks().items():
write_dict.append(
{
"name": '"' + get_localised_label(lang, "unicode_blocks").get(block.name, block.name) + '"',
"count": count_arr[varname],
"full_size": len(block.assigned_ranges),
}
)
output_writer.writerows(write_dict)