-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjitenbot.py
124 lines (107 loc) · 3.67 KB
/
jitenbot.py
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
114
115
116
117
118
119
120
121
122
123
124
""" jitenbot
Copyright (C) 2023 Stephen Kraus
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import sys
import argparse
import subprocess
from bot.targets import Targets
from bot.factory import new_crawler
def filename(f):
if not os.path.isfile(f):
raise argparse.ArgumentTypeError(f"`{f}` is not a valid filename")
elif not os.access(f, os.R_OK):
raise argparse.ArgumentTypeError(f"Cannot access file `{f}`")
else:
return f
def directory(d):
if not os.path.isdir(d):
raise argparse.ArgumentTypeError(f"`{d}` is not a valid directory")
elif not os.access(d, os.R_OK):
raise argparse.ArgumentTypeError(f"Cannot access directory `{d}`")
else:
return d
def parse_args(target_names):
parser = argparse.ArgumentParser(
prog="jitenbot",
description="Convert Japanese dictionary files to new formats.",
epilog="See README.md for details regarding media directory structures",
)
parser.add_argument(
"target",
choices=target_names,
help="name of dictionary to convert",
)
parser.add_argument(
"-p", "--page-dir",
help="path to directory containing XML page files",
type=directory,
)
parser.add_argument(
"-m", "--media-dir",
help="path to directory containing media folders (gaiji, graphics, audio, etc.)",
type=directory,
)
parser.add_argument(
"-i", "--mdict-icon",
help="path to icon file to be used with MDict",
type=filename,
)
parser.add_argument(
"--no-mdict-export",
help="skip export of dictionary data to MDict format",
action='store_true',
)
parser.add_argument(
"--no-yomichan-export",
help="skip export of dictionary data to Yomichan format",
action='store_true',
)
parser.add_argument(
"--validate-yomichan-terms",
help="validate JSON structure of exported Yomichan dictionary terms",
action='store_true',
)
args = parser.parse_args()
return args
def test_mdict():
try:
subprocess.run(
["mdict", "--version"],
check=True,
stdout=subprocess.DEVNULL,
)
except FileNotFoundError:
print("Could not find `mdict` pack tool.")
print("Ensure that mdict-utils is installed and")
print("included in the environment PATH.\n")
print("Mdict export functionality may also be")
print("disabled with the --no-mdict-export flag.")
sys.exit()
def main():
target_names = [x.value for x in Targets]
args = parse_args(target_names)
if not args.no_mdict_export:
test_mdict()
selected_target = Targets(args.target)
crawler = new_crawler(selected_target)
crawler.collect_pages(args.page_dir)
crawler.read_pages()
if not args.no_yomichan_export:
crawler.make_yomichan_dictionary(
args.media_dir, args.validate_yomichan_terms)
if not args.no_mdict_export:
crawler.make_mdict_dictionary(
args.media_dir, args.mdict_icon)
if __name__ == "__main__":
main()