diff --git a/tools/json2bin/evo.py b/tools/json2bin/evo.py index 4b1edab561..59d8517385 100644 --- a/tools/json2bin/evo.py +++ b/tools/json2bin/evo.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from collections.abc import Sequence import pathlib @@ -17,7 +18,7 @@ ) -def get_evo_params(method: evo_methods.EvoMethod, evo: list): +def get_evo_params(method: evo_methods.EvoMethod, evo: Sequence): maybe_param = evo[1] final_param = 0 #None of these take an extra parameter @@ -70,7 +71,7 @@ def table_line(evo_method: int, evo_params: int, species: int) -> bytes: return bytes(binary) -def parse_evolutions(table: list, _size: int, _enum: None) -> bytes: +def parse_evolutions(table: Sequence, _size: int, _enum: None) -> bytes: out = bytearray([]) for j in range(min(len(table), 7)): evo = table[j] diff --git a/tools/json2bin/json2bin.py b/tools/json2bin/json2bin.py index 153dde6a7e..193a02465c 100644 --- a/tools/json2bin/json2bin.py +++ b/tools/json2bin/json2bin.py @@ -2,6 +2,9 @@ import pathlib import subprocess +from collections.abc import MutableMapping, MutableSequence, Mapping, Sequence +from typing import Dict, List, Optional, Tuple, Type, Union + from argparse import ArgumentParser from enum import Enum, Flag, auto from types import FunctionType, LambdaType @@ -43,7 +46,7 @@ def __init__(self): self.field_index = 0 - def register_name(self, func: FunctionType | LambdaType | None) -> 'Parser': + def register_name(self, func: Optional[Union[FunctionType, LambdaType]]) -> 'Parser': ''' Register a function for processing the name key within the JSON structure. This differs from the standard function registration in @@ -60,9 +63,9 @@ def register_name(self, func: FunctionType | LambdaType | None) -> 'Parser': def register(self, field_name: str, - size: int | tuple[int,int], - func: FunctionType | LambdaType, - const_type: type[Enum] | None = None, + size: Union[int, Tuple[int,int]], + func: Union[FunctionType, LambdaType], + const_type: Optional[Type[Enum]] = None, optional: OptionalBehavior = OptionalBehavior.DISALLOW) -> 'Parser': ''' Register a function for processing a given key within the JSON @@ -93,17 +96,17 @@ def pad(self, size: int, value: int = 0) -> 'Parser': return self - def _walk(self, data: dict, key_seq: list[str]) -> any: + def _walk(self, data: dict, key_seq: Sequence[str]) -> any: data_val = data for step in key_seq: - if type(data_val) == list: + if isinstance(data_val, list): data_val = data_val[int(step)] else: data_val = data_val.get(step, {}) # All future walks will return {} return data_val - def parse(self, data: dict) -> bytes: + def parse(self, data: Mapping) -> bytes: ''' Parse the given JSON structure according to the currently-defined data schema. @@ -138,7 +141,7 @@ def parse(self, data: dict) -> bytes: return binary -def pack_flags(flags: list[str], size: int, consts: type[Flag]) -> bytes: +def pack_flags(flags: Sequence[str], size: int, consts: Type[Flag]) -> bytes: ''' Pack a list of flag constants into a bitmask. Flag values are defined by the specified consts type, which must descend from the enum.Flag @@ -150,7 +153,7 @@ def pack_flags(flags: list[str], size: int, consts: type[Flag]) -> bytes: return result.value.to_bytes(size, 'little') -def parse_const(val: str, size: int, consts: type[Enum]) -> bytes: +def parse_const(val: str, size: int, consts: Type[Enum]) -> bytes: ''' Simple parse wrapper for a value belonging to a set of constants, represented in JSON as a raw string name. @@ -158,14 +161,14 @@ def parse_const(val: str, size: int, consts: type[Enum]) -> bytes: return consts[val].value.to_bytes(size, 'little') -def parse_int(val: int, size: int, _consts: type[Enum] = None) -> bytes: +def parse_int(val: int, size: int, _consts: Type[Enum] = None) -> bytes: ''' Simple parse wrapper for an integer. ''' return val.to_bytes(size, 'little') -def parse_sint(val: int, size: int, _consts: type[Enum] = None) -> bytes: +def parse_sint(val: int, size: int, _consts: Type[Enum] = None) -> bytes: ''' Simple parse wrapper for a signed integer. ''' @@ -180,7 +183,7 @@ def _parse(fname_in: str, schema: Parser) -> bytes: return schema.parse(input_json) -def _write(output_bin: bytes, output_idx: int, output_dir: str | None): +def _write(output_bin: bytes, output_idx: int, output_dir: Optional[str]): output_fname = f'{output_idx:04}.bin' if output_dir: output_fname = pathlib.Path(output_dir) / output_fname @@ -199,14 +202,14 @@ def _process(fname_in: str, def json2bin(target: str, schema: Parser, - private_dir: str | None, - output_dir: str | None, + private_dir: Optional[str], + output_dir: Optional[str], index_func: FunctionType, glob_pattern: str='*.json', - narc_name: str | None = None, - narc_packer: str | None = None, + narc_name: Optional[str] = None, + narc_packer: Optional[str] = None, output_mode: OutputMode = OutputMode.MULTI_FILE, - skip_stems: list[str] = []): + skip_stems: Sequence[str] = []): private_dir = pathlib.Path(private_dir) output_dir = pathlib.Path(output_dir) diff --git a/tools/json2bin/pokemon_wotbl_data.py b/tools/json2bin/pokemon_wotbl_data.py index 9b6b84c4a4..c2585a181d 100644 --- a/tools/json2bin/pokemon_wotbl_data.py +++ b/tools/json2bin/pokemon_wotbl_data.py @@ -21,9 +21,9 @@ def parse_level_up_moves(table: dict, _size: int, _enum: None): for key, value in table.items(): level = int(key) level_moves = value - if type(level_moves) == str: + if isinstance(level_moves, str): out.extend(table_line(moves.Move[level_moves].value, level)) - elif type(level_moves) == list: + elif isinstance(level_moves, list): for move in level_moves: out.extend(table_line(moves.Move[move].value, level)) else: diff --git a/tools/json2bin/trainer_data.py b/tools/json2bin/trainer_data.py index d215177ae5..4db7208119 100644 --- a/tools/json2bin/trainer_data.py +++ b/tools/json2bin/trainer_data.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from collections.abc import Mapping, Sequence import pathlib, functools from consts import ( @@ -12,7 +13,7 @@ import json2bin as j2b -def derive_data_flags(party: list[dict], *args) -> bytes: +def derive_data_flags(party: Sequence[Mapping], *args) -> bytes: defined_moves = False defined_items = False for mon in party: @@ -25,7 +26,7 @@ def derive_data_flags(party: list[dict], *args) -> bytes: return (int(defined_moves) | (int(defined_items) << 1)).to_bytes(1, 'little') -def parse_trainer_items(item_list: list[str], *args) -> bytes: +def parse_trainer_items(item_list: Sequence[str], *args) -> bytes: item_bin = bytearray([]) for item_str in item_list: item_bin.extend(item.Item[item_str].value.to_bytes(2, 'little')) @@ -35,7 +36,7 @@ def parse_trainer_items(item_list: list[str], *args) -> bytes: return item_bin -def parse_poke_moves(move_list: list[str], *args) -> bytes: +def parse_poke_moves(move_list: Sequence[str], *args) -> bytes: move_bin = bytearray([]) for move_str in move_list: move_bin.extend(moves.Move[move_str].value.to_bytes(2, 'little')) @@ -64,7 +65,7 @@ def parse_party_mon(mon: dict, has_moves: bool, has_items: bool) -> bytes: # Parties are a complicated and variable structure, so just process them wholly # independently -def parse_party_mons(party_list: list[dict], *args) -> bytes: +def parse_party_mons(party_list: Sequence[Mapping], *args) -> bytes: if len(party_list) == 0: # special case, pads to 2 words instead of 1 word return (0).to_bytes(8, 'little')