Skip to content

Commit

Permalink
Update json2bin for python3.8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lhearachel committed Jan 27, 2024
1 parent 8f8326c commit 30f15cf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
5 changes: 3 additions & 2 deletions tools/json2bin/evo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from collections.abc import Sequence
import pathlib


Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
37 changes: 20 additions & 17 deletions tools/json2bin/json2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -150,22 +153,22 @@ 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.
'''
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.
'''
Expand All @@ -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
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tools/json2bin/pokemon_wotbl_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions tools/json2bin/trainer_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
from collections.abc import Mapping, Sequence
import pathlib, functools

from consts import (
Expand All @@ -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:
Expand All @@ -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'))
Expand All @@ -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'))
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit 30f15cf

Please sign in to comment.