From 11957a02bb696b41d1cc513cc02e4aeed3e07ade Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sat, 21 Dec 2024 15:35:01 +0800 Subject: [PATCH] =?UTF-8?q?snbt=20json=20=E6=94=AF=E6=8C=81=E4=BA=92?= =?UTF-8?q?=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...on.yaml => build_snbt_json_converter.yaml} | 26 ++--- README.md | 15 +-- snbt2json/main.py | 82 ---------------- snbt_json_converter/main.py | 96 +++++++++++++++++++ .../requirements.txt | 0 5 files changed, 117 insertions(+), 102 deletions(-) rename .github/workflows/{build_snbt2json.yaml => build_snbt_json_converter.yaml} (65%) delete mode 100644 snbt2json/main.py create mode 100644 snbt_json_converter/main.py rename {snbt2json => snbt_json_converter}/requirements.txt (100%) diff --git a/.github/workflows/build_snbt2json.yaml b/.github/workflows/build_snbt_json_converter.yaml similarity index 65% rename from .github/workflows/build_snbt2json.yaml rename to .github/workflows/build_snbt_json_converter.yaml index 0f021a7..d1a8e69 100644 --- a/.github/workflows/build_snbt2json.yaml +++ b/.github/workflows/build_snbt_json_converter.yaml @@ -1,9 +1,9 @@ -name: Build snbt to json and upload +name: Build snbt json converter and upload on: push: paths: - - 'snbt2json/**' + - 'snbt_json_converter/**' workflow_dispatch: jobs: @@ -21,16 +21,16 @@ jobs: - name: Install dependencies (Windows) run: | pip install pyinstaller - pip install -r snbt2json/requirements.txt + pip install -r snbt_json_converter/requirements.txt - name: Build with PyInstaller (Windows) run: | - python -m PyInstaller -F -n snbt-to-json-Windows snbt2json/main.py + python -m PyInstaller -F -n snbt-json-converter-Windows snbt_json_converter/main.py - name: Upload (Windows) uses: actions/upload-artifact@v4 with: - path: dist/snbt-to-json-Windows.exe + path: dist/snbt-json-converter-Windows.exe build-macos: runs-on: macos-latest @@ -46,17 +46,17 @@ jobs: - name: Install dependencies (macOS) run: | pip install pyinstaller - pip install -r snbt2json/requirements.txt + pip install -r snbt_json_converter/requirements.txt - name: Build with PyInstaller (macOS) run: | - python -m PyInstaller -F -n snbt-to-json-macOS snbt2json/main.py + python -m PyInstaller -F -n snbt-json-converter-macOS snbt_json_converter/main.py - name: Upload (macOS) uses: actions/upload-artifact@v4 with: - name: snbt-to-json-macOS - path: dist/snbt-to-json-macOS + name: snbt-json-converter-macOS + path: dist/snbt-json-converter-macOS build-linux: runs-on: ubuntu-latest @@ -72,14 +72,14 @@ jobs: - name: Install dependencies (Linux) run: | pip install pyinstaller - pip install -r snbt2json/requirements.txt + pip install -r snbt_json_converter/requirements.txt - name: Build with PyInstaller (Linux) run: | - python -m PyInstaller -F -n snbt-to-json-Linux snbt2json/main.py + python -m PyInstaller -F -n snbt-json-converter-Linux snbt_json_converter/main.py - name: Upload (Linux) uses: actions/upload-artifact@v4 with: - name: snbt-to-json-Linux - path: dist/snbt-to-json-Linux + name: snbt-json-converter-Linux + path: dist/snbt-json-converter-Linux diff --git a/README.md b/README.md index a3281f8..1d48d04 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,16 @@ 使用相关功能需要有Paratranz的项目id和有对应权限用户的token,可在Paratranz个人主页查看。 -## snbt 转 json 工具 +## snbt json 互转工具 -用于将FTB任务中的snbt文件转换为json。对于FTB任务在mc1.21+新加入的语言文件,转换后的json可直接在翻译平台导入进行翻译。 +用于将FTB任务在mc1.21+新加入的snbt语言文件文件与json格式互转,便于导入翻译平台进行翻译。 -输入一个snbt文件路径或目录,会转换所有snbt文件为json +输入一个文件路径或目录,会按照选择的模式转换所有文件 - 支持彩色提示信息 - 支持将语言文件导入翻译平台 -- 支持转换单个或整个目录的snbt文件 +- 支持转换单个或整个目录的文件 +- 支持 snbt json 互转 ## 下载 @@ -76,11 +77,11 @@ Paratranz译文同步工具: python -m PyInstaller paratranz_sync/main.spec ``` -snbt 转 json 工具: +snbt json 互转工具: ```bash pip install pyinstaller - pip install -r snbt2json/requirements.txt - python -m PyInstaller -F -n snbt-to-json snbt2json/main.py + pip install -r snbt_json_converter/requirements.txt + python -m PyInstaller -F -n snbt-json-converter snbt_json_converter/main.py ``` ## 星标 diff --git a/snbt2json/main.py b/snbt2json/main.py deleted file mode 100644 index 78b499f..0000000 --- a/snbt2json/main.py +++ /dev/null @@ -1,82 +0,0 @@ -import json -import os -from pathlib import Path -from typing import Union - -import snbtlib -from colorama import Fore, init - -init(autoreset=True) - - -def convert_list_to_string(data: Union[list, dict, str]) -> Union[str, dict]: - """ - 将 JSON 中的列表转换为单行字符串 - """ - if isinstance(data, list): - # 将列表内容拼接成字符串并转义换行符 - result = "\\n".join(data) - # 将 \\n 转换为 \n - return result.replace("\\\\n", "\\n") - elif isinstance(data, dict): - # 递归处理字典中的值 - return {k: convert_list_to_string(v) for k, v in data.items()} - else: - return data - - -def convert_snbt_file(file_path: Path) -> None: - try: - snbt_data = file_path.read_text(encoding="utf-8") - json_data = snbtlib.loads(snbt_data) - json_data = convert_list_to_string(json_data) - output_path = file_path.with_suffix(".json") - - output_path.write_text( - json.dumps(json_data, indent=4, ensure_ascii=False), encoding="utf-8" - ) - print(f"{Fore.CYAN}转换成功: {file_path} -> {output_path}") - except Exception as e: - print(f"{Fore.RED}转换失败: {file_path} - 错误: {e}") - - -def convert_snbt_directory(directory_path: Path) -> None: - for root, _, files in os.walk(directory_path): - for file in files: - if file.endswith(".snbt"): - snbt_file = Path(root) / file - convert_snbt_file(snbt_file) - - -def main() -> None: - print( - Fore.LIGHTGREEN_EX - + "snbt 转 json 工具[版本 1.0 (2024)]\n作者:Wulian233(捂脸)\n\n" - + Fore.RESET - + """VM之禅: - 一,即使翻译难易各异,译者应持己见自立。 - 二,即使遇到词句争议,组员勿必同心共力。 - 三,即使译途坏垣跌荡,仍应坚守质量保障。 - 四,即使成果乏人褒奖,仍不计事后短长。 - 五,即使面临质疑声涌,仍要对正道守望。 - 六,即使译句翻乱无章,仍应看向前方、重塑文章。 - """ - ) - - path = input("请输入 snbt 文件或目录路径:").strip() - input_path = Path(path) - - if input_path.is_dir(): - convert_snbt_directory(input_path) - elif input_path.is_file() and input_path.suffix == ".snbt": - convert_snbt_file(input_path) - else: - print(f"{Fore.RED}输入的路径无效,请输入正确的 snbt 文件或目录。") - return - - print(f"{Fore.GREEN}所有 snbt 文件已成功转换为 json!") - input("按任意键(关机键除外)退出...") - - -if __name__ == "__main__": - main() diff --git a/snbt_json_converter/main.py b/snbt_json_converter/main.py new file mode 100644 index 0000000..5f75978 --- /dev/null +++ b/snbt_json_converter/main.py @@ -0,0 +1,96 @@ +import json +import os +from pathlib import Path +from typing import Union + +import snbtlib +from colorama import Fore, init + +init(autoreset=True) + + +def convert_list_to_string(data: Union[list, dict, str]) -> Union[str, dict]: + """ + 将 JSON 中的列表转换为单行字符串 + """ + if isinstance(data, list): + return "\\n".join(data).replace("\\\\n", "\\n") + elif isinstance(data, dict): + return {k: convert_list_to_string(v) for k, v in data.items()} + return data + + +def convert_file(file_path: Path, to_json: bool) -> None: + try: + if to_json: + # SNBT -> JSON + snbt_data = file_path.read_text(encoding="utf-8") + json_data = snbtlib.loads(snbt_data) + json_data = convert_list_to_string(json_data) + output_path = file_path.with_suffix(".json") + output_path.write_text( + json.dumps(json_data, indent=4, ensure_ascii=False), encoding="utf-8" + ) + else: + # JSON -> SNBT + json_data = json.loads(file_path.read_text(encoding="utf-8")) + snbt_data = snbtlib.dumps(json_data) + output_path = file_path.with_suffix(".snbt") + output_path.write_text(snbt_data, encoding="utf-8") + print(f"{Fore.CYAN}转换成功: {file_path} -> {output_path}") + except Exception as e: + print(f"{Fore.RED}转换失败: {file_path} - 错误: {e}") + + +def convert_directory(directory_path: Path, to_json: bool) -> None: + for root, _, files in os.walk(directory_path): + for file in files: + if (to_json and file.endswith(".snbt")) or ( + not to_json and file.endswith(".json") + ): + file_path = Path(root) / file + convert_file(file_path, to_json) + + +def main() -> None: + print( + Fore.LIGHTGREEN_EX + + "snbt json 互转工具 [版本 1.1 (2024)]\n作者:Wulian233(捂脸)\n\n" + + Fore.RESET + + """VM之禅: + 一,即使翻译难易各异,译者应持己见自立。 + 二,即使遇到词句争议,组员勿必同心共力。 + 三,即使译途坏垣跌荡,仍应坚守质量保障。 + 四,即使成果乏人褒奖,仍不计事后短长。 + 五,即使面临质疑声涌,仍要对正道守望。 + 六,即使译句翻乱无章,仍应看向前方、重塑文章。 + """ + ) + + path = input("请输入文件或目录路径:").strip() + input_path = Path(path) + + action = input("请选择操作:1. SNBT -> JSON 2. JSON -> SNBT\n").strip() + if action not in {"1", "2"}: + print(f"{Fore.RED}无效操作!请输入 1 或 2") + return + + to_json = action == "1" + + if input_path.is_dir(): + convert_directory(input_path, to_json) + elif input_path.is_file() and ( + (to_json and input_path.suffix == ".snbt") + or (not to_json and input_path.suffix == ".json") + ): + convert_file(input_path, to_json) + else: + print(f"{Fore.RED}输入的路径无效,请输入正确的文件或目录") + return + + print(f"{Fore.GREEN}所有文件已成功转换!") + input("按任意键(关机键除外)退出...\n") + + +if __name__ == "__main__": + main() diff --git a/snbt2json/requirements.txt b/snbt_json_converter/requirements.txt similarity index 100% rename from snbt2json/requirements.txt rename to snbt_json_converter/requirements.txt