From b877ab1bddd5cd66fbbbc411b20e627f7bdb21a4 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Thu, 13 Mar 2025 00:20:01 +0200 Subject: [PATCH 1/2] Have better typing for the main API --- src/mars_patcher/cli.py | 4 +--- src/mars_patcher/patcher.py | 11 +++++++---- src/mars_patcher/rom.py | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/mars_patcher/cli.py b/src/mars_patcher/cli.py index 61b029a..5a329e1 100644 --- a/src/mars_patcher/cli.py +++ b/src/mars_patcher/cli.py @@ -18,11 +18,9 @@ def main() -> None: with open(args.patch_data_path, encoding="utf-8") as f: patch_data = json.load(f) - validate_patch_data(patch_data) - patch( args.rom_path, args.out_path, - typing.cast(MarsSchema, copy.copy(patch_data)), + validate_patch_data(patch_data), lambda message, progress: print(message), ) diff --git a/src/mars_patcher/patcher.py b/src/mars_patcher/patcher.py index a18b42f..31e389b 100644 --- a/src/mars_patcher/patcher.py +++ b/src/mars_patcher/patcher.py @@ -1,5 +1,7 @@ import json -from typing import Callable +import typing +from os import PathLike +from typing import Callable, Union from jsonschema import validate @@ -33,7 +35,7 @@ from mars_patcher.text import write_seed_hash -def validate_patch_data(patch_data: dict) -> None: +def validate_patch_data(patch_data: dict) -> MarsSchema: """ Validates whether the specified patch_data satisfies the schema for it. @@ -43,11 +45,12 @@ def validate_patch_data(patch_data: dict) -> None: with open(get_data_path("schema.json")) as f: schema = json.load(f) validate(patch_data, schema) + return typing.cast(MarsSchema, patch_data) def patch( - input_path: str, - output_path: str, + input_path: Union[str, PathLike[str]], + output_path: Union[str, PathLike[str]], patch_data: MarsSchema, status_update: Callable[[str, float], None], ) -> None: diff --git a/src/mars_patcher/rom.py b/src/mars_patcher/rom.py index 69fc16f..f20ef60 100644 --- a/src/mars_patcher/rom.py +++ b/src/mars_patcher/rom.py @@ -82,7 +82,7 @@ class Rom: }, } - def __init__(self, path: str): + def __init__(self, path: Union[str, PathLike[str]]): # Read file with open(path, "rb") as f: self.data = bytearray(f.read()) From 1415bfad4a30c341b1c56c9e54ce0ef19c336c45 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Mar 2025 22:21:46 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/mars_patcher/cli.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mars_patcher/cli.py b/src/mars_patcher/cli.py index 5a329e1..7642918 100644 --- a/src/mars_patcher/cli.py +++ b/src/mars_patcher/cli.py @@ -1,9 +1,6 @@ import argparse -import copy import json -import typing -from mars_patcher.auto_generated_types import MarsSchema from mars_patcher.patcher import patch, validate_patch_data