-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
206 lines (162 loc) · 5.94 KB
/
cli.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
import os
import shutil
import subprocess
from logging import DEBUG, Formatter
from logging.handlers import RotatingFileHandler
from pathlib import Path
from time import time
import PyInstaller.__main__
import tomli
import yaml
from rich.logging import RichHandler
from typer import Option, Typer
from ww.commands.analyze import app as analyze
from ww.commands.crawl import app as crawl
from ww.commands.custom import app as custom
from ww.commands.migrate import app as migrate
from ww.commands.resonator import app as resonator
from ww.commands.weapon import app as weapon
from ww.docs.export import Docs
from ww.logging import logger_cli
_help = """
The CLI for ZetsuBou
"""
app = Typer(rich_markup_mode="rich", help=_help)
app.add_typer(analyze)
app.add_typer(crawl)
app.add_typer(custom)
app.add_typer(migrate)
app.add_typer(resonator)
app.add_typer(weapon)
def get_version() -> str:
with open("pyproject.toml", "rb") as f:
toml_dict = tomli.load(f)
version = toml_dict.get("tool", {}).get("poetry", {}).get("version", "")
return version
@app.command()
def build(version: str = Option(get_version())):
if not version:
return
# PyInstaller.__main__.run(["-y", "app.spec"])
home = Path("./dist/releases")
app_fname = "app.exe"
app_path = Path("./dist") / app_fname
if not app_path.exists():
return
version_1 = version
version_2 = f"{version}-ZetsuBouKyo"
version_1_path = home / version_1
version_2_path = home / version_2
os.makedirs(version_1_path, exist_ok=True)
os.makedirs(version_2_path, exist_ok=True)
# Copy common folders
folder_names = ["assets", "data", "docs", "html"]
version_paths = [version_1_path, version_2_path]
for version_path in version_paths:
for name in folder_names:
copied_folder_path = version_path / name
os.makedirs(copied_folder_path, exist_ok=True)
shutil.copytree(name, copied_folder_path, dirs_exist_ok=True)
# Copy `cache/v1/zh_tw/custom`
cache_custom_path = "cache/v1/zh_tw/custom"
copied_cache_custom_path = version_2_path / cache_custom_path
os.makedirs(copied_cache_custom_path, exist_ok=True)
shutil.copytree(cache_custom_path, copied_cache_custom_path, dirs_exist_ok=True)
# Copy `cache/v1/zh_tw/output/[calculated]resonators.tsv`
cache_calculated_resonators_fpath = (
"cache/v1/zh_tw/output/[calculated]resonators.tsv"
)
copied_cache_calculated_resonators_fpath = (
version_2_path
/ "cache"
/ "v1"
/ "zh_tw"
/ "output"
/ "[calculated]resonators.tsv"
)
os.makedirs(copied_cache_calculated_resonators_fpath.parent, exist_ok=True)
shutil.copy(
cache_calculated_resonators_fpath, copied_cache_calculated_resonators_fpath
)
# Copy `app.exe`
app_1_path = version_1_path / app_fname
app_2_path = version_2_path / app_fname
shutil.copy(app_path, app_1_path)
shutil.copy(app_path, app_2_path)
# Zip
zip_1_fpath = home / version_1
zip_2_fpath = home / version_2
shutil.make_archive(zip_1_fpath, "zip", version_1_path)
shutil.make_archive(zip_2_fpath, "zip", version_2_path)
@app.command()
def docs(
version: str = Option(get_version()),
config_file: str = Option("./build/html/mkdocs.yml"),
debug_level: int = Option(DEBUG),
):
import traceback
try:
fmt = "%(asctime)s - %(name)s - %(filename)s - %(lineno)d - %(levelname)s - %(message)s"
logging_fpath = "./build/cli.log"
stream_handler = RichHandler()
rotating_file_formatter = Formatter(fmt=fmt)
rotating_file_handler = RotatingFileHandler(
logging_fpath, maxBytes=2 * 1024 * 1024, backupCount=3, encoding="utf-8"
)
rotating_file_handler.setFormatter(rotating_file_formatter)
logger_cli.setLevel(debug_level)
logger_cli.addHandler(stream_handler)
logger_cli.addHandler(rotating_file_handler)
logger_cli.debug(f"Making docs...")
# Copy the assets
assets_src_path = "./assets"
assets_dest_path = "./build/html/docs/assets"
os.makedirs(assets_dest_path, exist_ok=True)
shutil.copytree(assets_src_path, assets_dest_path, dirs_exist_ok=True)
# Copy the docs
docs_src_path = "./docs/html"
docs_dest_path = "./build/html/docs"
shutil.copytree(docs_src_path, docs_dest_path, dirs_exist_ok=True)
docs = Docs()
docs.export()
logger_cli.debug(f"Markdown files created.")
except:
print(traceback.format_exc())
return
try:
# Build the mkdocs command
command = ["mkdocs", "build", "--config-file", config_file]
# Run the command
result = subprocess.run(command, check=True, capture_output=True, text=True)
# Print the output
print("MkDocs Build Output:")
print(result.stdout)
except subprocess.CalledProcessError as e:
print("Error during MkDocs build:")
print(e.stderr)
logger_cli.debug(f"HTML files created.")
@app.command()
def print_docs_settings(version: str = Option(get_version())):
with open("mkdocs.yml", encoding="utf-8") as stream:
try:
settings = yaml.safe_load(stream)
settings_str = json.dumps(settings, indent=4, ensure_ascii=False)
print(settings_str)
except yaml.YAMLError as exc:
print(exc)
@app.command()
def tmp():
# from ww.calc.simulated_resonators import Theory1SimulatedResonators
# from ww.crud.template import get_template
# from ww.locale import ZhTwEnum, _
# from ww.utils.pd import save_df
from ww.calc.simulated_echoes import SimulatedEchoes
from ww.crud.template import get_template
from ww.locale import ZhTwEnum, _
from ww.utils.pd import save_df
echoes = SimulatedEchoes()
df = echoes.get_df()
save_df("tmp.tsv", df, echoes.echoes_table_column_names)
if __name__ == "__main__":
app()