-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
176 lines (128 loc) · 4.85 KB
/
cli.py
File metadata and controls
176 lines (128 loc) · 4.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Copyright (C) 2024 He Lin <log_283375@163.com>
This file is part of Arcaea Offline OCR samples extract.
Arcaea Offline OCR samples extract 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.
Arcaea Offline OCR samples extract 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 Arcaea Offline OCR samples extract. If not, see <https://www.gnu.org/licenses/>.
"""
from enum import Enum
from pathlib import Path
from typing import Optional
import questionary
from rich.console import Console
from rich.table import Table
from src.extract import ExtractOption, Extractor, T1Extractor, T2Extractor
console = Console()
DEFAULT_SOURCES_PATH = Path("sources").resolve()
DEFAULT_OUTPUT_PATH = Path("outputs").resolve()
class SourcesType(Enum):
T1 = "T1"
T2 = "T2"
class PathExistValidator(questionary.Validator):
def validate(self, document):
path = Path(document.text)
if not path.exists():
raise questionary.ValidationError(
message="Please choose a valid path.",
cursor_position=len(document.text),
)
def ask_sources_path() -> Optional[Path]:
sources_path = questionary.path(
"Path of the source images directory?",
default=str(DEFAULT_SOURCES_PATH) if DEFAULT_SOURCES_PATH.exists() else "",
validate=PathExistValidator,
).ask()
if sources_path is None:
return None
return Path(sources_path)
def ask_sources_type() -> Optional[SourcesType]:
sources_type = questionary.select(
"Which type is it?",
choices=["T1", "T2"],
).ask()
if sources_type is None:
return None
return SourcesType[sources_type]
def ask_output_path() -> Optional[Path]:
output_path = questionary.path(
"Path of the output directory?",
default=str(DEFAULT_OUTPUT_PATH) if DEFAULT_OUTPUT_PATH.exists() else "",
).ask()
if output_path is None:
return None
output_path = Path(output_path)
if not output_path.exists():
confirm_create = questionary.confirm(
f"Output directory [{output_path.name}] does not exist. Create it?",
qmark="!> ",
).ask()
if not confirm_create:
return None
output_path.mkdir(parents=True)
return output_path
def ask_extract_options() -> Optional[list[ExtractOption]]:
def option_to_choice(option: ExtractOption) -> questionary.Choice:
return questionary.Choice(
title=option.value,
value=option,
checked=option in Extractor.DEFAULT_EXTRACT_OPTIONS,
)
choices = questionary.checkbox(
"Extract what components?",
[option_to_choice(option) for option in ExtractOption._member_map_.values()],
).ask()
return choices
def abort():
questionary.print("Abort.", style="yellow")
raise SystemExit(1)
def extract(
*,
sources_path: Path,
output_path: Path,
sources_type: SourcesType,
extract_options: list[ExtractOption],
):
globs = ["*.jpg", "*.png", "*.jpeg"]
table = Table(title="Summary", show_header=False)
table.add_column("Key", justify="right", style="blue", no_wrap=True)
table.add_column("Value")
table.add_row("From", str(sources_path.resolve()))
table.add_row("To", str(output_path.resolve()))
table.add_row("Type", sources_type.value)
table.add_row("Options", ", ".join([option.value for option in extract_options]))
table.add_row("Globs", ", ".join(globs))
console.print(table)
confirm = questionary.confirm("Is this OK?").ask()
if not confirm:
abort()
image_files = []
[image_files.extend(sources_path.rglob(g)) for g in globs]
if sources_type == SourcesType.T1:
extractor = T1Extractor(
image_files=image_files, output_dir=output_path, options=extract_options
)
elif sources_type == SourcesType.T2:
extractor = T2Extractor(
image_files=image_files, output_dir=output_path, options=extract_options
)
else:
raise ValueError(f"wait waht is {sources_type!r}")
extractor.extract()
if __name__ == "__main__":
sources_path = ask_sources_path() or abort()
sources_type = ask_sources_type() or abort()
output_path = ask_output_path() or abort()
extract_options = ask_extract_options() or abort()
extract(
sources_path=sources_path,
output_path=output_path,
sources_type=sources_type,
extract_options=extract_options,
)