From 9832462b8fc16cd1112bdf01136ad650bdf9008d Mon Sep 17 00:00:00 2001 From: glyh Date: Sat, 28 Sep 2024 13:06:08 +0800 Subject: [PATCH] add support for interactive/non-interactive mode --- CHANGELOG.md | 1 + config.yml | 2 ++ javsp/__main__.py | 3 ++- javsp/config.py | 1 + javsp/func.py | 3 ++- javsp/prompt.py | 7 +++++++ tools/config_migration.py | 2 ++ 7 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 javsp/prompt.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 15bdc127d..e0459f37d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - 添加新的爬虫`arzon`, `arzon_iv` [#377](https://github.com/Yuukiy/JavSP/pull/377) - Slimeface人脸识别 [#380](https://github.com/Yuukiy/JavSP/pull/380) - 支持Linux和MacOS(x64)二进制 [a754e1c](https://github.com/Yuukiy/JavSP/commit/a754e1ce0f14b0ca9dcc6d43d8e7d322a3da1c43) +- 添加选项`other.interactive`来表示程序是否应该在interactive模式下运行 ### Changed - 使用 Poetry 作为构建系统 [134b279](https://github.com/Yuukiy/JavSP/commit/134b279151aead587db0b12d1a30781f2e1be5b1) diff --git a/config.yml b/config.yml index 53fac4863..88f738f25 100644 --- a/config.yml +++ b/config.yml @@ -189,6 +189,8 @@ translator: ################################ other: + # 是否在stdin/stdout进行交互 + interactive: true # 是否允许检查更新。如果允许,在有新版本时会显示提示信息和新版功能 check_update: true # 是否允许检查到新版本时自动下载 diff --git a/javsp/__main__.py b/javsp/__main__.py index 7771170e7..f9d21658f 100644 --- a/javsp/__main__.py +++ b/javsp/__main__.py @@ -46,6 +46,7 @@ from javsp.web.translate import translate_movie_info from javsp.config import Cfg, CrawlerID +from javsp.prompt import prompt actressAliasMap = {} @@ -353,7 +354,7 @@ def reviewMovieID(all_movies, root): print(f'[{i}/{count}]\t{Fore.LIGHTMAGENTA_EX}{id}{Style.RESET_ALL}, 对应文件:') relpaths = [os.path.relpath(i, root) for i in movie.files] print('\n'.join([' '+i for i in relpaths])) - s = input("回车确认当前番号,或直接输入更正后的番号(如'ABC-123'或'cid:sqte00300')") + s = prompt("回车确认当前番号,或直接输入更正后的番号(如'ABC-123'或'cid:sqte00300')", "更正后的番号") if not s: logger.info(f"已确认影片番号: {','.join(relpaths)}: {id}") else: diff --git a/javsp/config.py b/javsp/config.py index 3fbc8f071..ccf2371fb 100644 --- a/javsp/config.py +++ b/javsp/config.py @@ -209,6 +209,7 @@ class Translator(BaseConfig): fields: TranslateField class Other(BaseConfig): + interactive: bool check_update: bool auto_update: bool diff --git a/javsp/func.py b/javsp/func.py index 042afea5c..f7855825d 100644 --- a/javsp/func.py +++ b/javsp/func.py @@ -26,6 +26,7 @@ from javsp.web.base import * from javsp.lib import re_escape, resource_path +from javsp.prompt import prompt __all__ = ['select_folder', 'get_scan_dir', 'remove_trail_actor_in_title', 'shutdown', 'CLEAR_LINE', 'check_update', 'split_by_punc'] @@ -61,7 +62,7 @@ def get_scan_dir(cfg_scan_dir: Path | None) -> str | None: print('请选择要整理的文件夹:', end='') root = select_folder() else: - root = input('请选择要整理的文件夹路径,必须是绝对路径: ') + root = prompt('请选择要整理的文件夹路径,必须是绝对路径: ', "要整理的文件夹") print(root) return root diff --git a/javsp/prompt.py b/javsp/prompt.py new file mode 100644 index 000000000..dedb829d2 --- /dev/null +++ b/javsp/prompt.py @@ -0,0 +1,7 @@ +from javsp.config import Cfg +def prompt(message: str, what: str) -> str: + if Cfg().other.interactive: + return input(message) + else: + print(f"缺少{what}") + exit(1) diff --git a/tools/config_migration.py b/tools/config_migration.py index 95adc45d6..655bb9b2f 100644 --- a/tools/config_migration.py +++ b/tools/config_migration.py @@ -237,6 +237,8 @@ def fix_pat(p): ################################ other: + # 是否在stdin/stdout进行交互 + interactive: true # 是否允许检查更新。如果允许,在有新版本时会显示提示信息和新版功能 check_update: {yes_to_true(cfg['Other']['check_update'])} # 是否允许检查到新版本时自动下载