Skip to content

Commit

Permalink
Python Upgrade: 3.9 - 3.12 (#18)
Browse files Browse the repository at this point in the history
* Python 3.12 Upgrade

* pyupgrade: py>=3.9
  • Loading branch information
theCapypara authored Oct 3, 2023
1 parent d694c91 commit c57ba52
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 37 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
steps:
- uses: actions/checkout@v4
- uses: theCapypara/mypy-check@rust-support
Expand All @@ -33,7 +33,7 @@ jobs:
strategy:
max-parallel: 5
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down Expand Up @@ -87,17 +87,17 @@ jobs:
- name: Note version
run: |
echo "PACKAGE_VERSION=$(python3 -- ./setup.py --version)" >> $GITHUB_ENV
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.11
- name: Upgrade pip
run: |
python -m pip install --upgrade pip
- name: Build Python wheels
uses: RalfG/[email protected]_x86_64
with:
python-versions: 'cp38-cp38 cp39-cp39 cp310-cp310 cp311-cp311'
python-versions: 'cp39-cp39 cp310-cp310 cp311-cp311 cp312-cp312'
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand All @@ -113,10 +113,10 @@ jobs:
uses: actions/download-artifact@v3
with:
name: wheels
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.11
- name: Upgrade pip
run: |
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def get_resources(file_exts):
'Development Status :: 4 - Beta',
'Programming Language :: Python',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
],
)
4 changes: 2 additions & 2 deletions skytemple_dtef/dungeon_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _dma_type_extra_name(self):


class RestTileMapping:
def __init__(self, x: int, y: int, mappings: List[RestTileMappingEntry], file_name: str):
def __init__(self, x: int, y: int, mappings: list[RestTileMappingEntry], file_name: str):
self.x = x
self.y = y
self.mappings = mappings
Expand All @@ -110,7 +110,7 @@ def __init__(self, x: int, y: int, mappings: List[RestTileMappingEntry], file_na

class DungeonXml:
@classmethod
def generate(cls, dpla: DplaProtocol, dungeon_tile_dimensions: int, rest_tile_mappings: List[RestTileMapping]) -> Element:
def generate(cls, dpla: DplaProtocol, dungeon_tile_dimensions: int, rest_tile_mappings: list[RestTileMapping]) -> Element:
dungeon_tileset = Element(DUNGEON_TILESET, {DIMENSIONS: str(dungeon_tile_dimensions)})
dungeon_tileset.append(Comment(" Dungeon Tile Exchange Format (DTEF) - SkyTemple PMD Explorers of Sky Export.\n"
" This XML file contains additional metadata for the tileset.\n"
Expand Down
10 changes: 5 additions & 5 deletions skytemple_dtef/explorers_dtef.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def paste(fimg, chunk_index, x, y):
)

# Process tiles
self._variation_map: List[List[int]] = [[], [], []]
self._variation_map: list[list[int]] = [[], [], []]
self._coord_map = {}
# Pre-fill variation maps
for ti, the_type in enumerate((DmaType.WALL, DmaType.WATER, DmaType.FLOOR)):
Expand All @@ -85,9 +85,9 @@ def paste(fimg, chunk_index, x, y):
self._variation_map[iv].append(variation)

# Non standard tiles
self.rest_mappings: List[RestTileMapping] = []
self._tiles_to_draw_on_more: List[int] = []
self._rest_mappings_idxes: Dict[int, int] = {} # dpc -> rest_mappings index
self.rest_mappings: list[RestTileMapping] = []
self._tiles_to_draw_on_more: list[int] = []
self._rest_mappings_idxes: dict[int, int] = {} # dpc -> rest_mappings index

# Process all normal rule tiles (47-set and check 256-set extended)
for ti, the_type in enumerate((DmaType.WALL, DmaType.WATER, DmaType.FLOOR)):
Expand Down Expand Up @@ -129,7 +129,7 @@ def paste(fimg, chunk_index, x, y):
def get_xml(self) -> ElementTree.Element:
return DungeonXml.generate(self.dpla, TW, self.rest_mappings)

def get_tiles(self) -> List[Image.Image]:
def get_tiles(self) -> list[Image.Image]:
return [self.var0, self.var1, self.var2, self.rest]

@staticmethod
Expand Down
22 changes: 11 additions & 11 deletions skytemple_dtef/explorers_dtef_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ def __init__(self, dma: DmaProtocol, dpc: DpcProtocol, dpci: DpciProtocol, dpl:
self.dpla = dpla

self._dirname: Optional[str] = None
self._tileset_file_map: Dict[str, Image.Image] = {}
self._tileset_chunk_map: Dict[str, Dict[Tuple[int, int], int]] = {}
self._tileset_file_map: dict[str, Image.Image] = {}
self._tileset_chunk_map: dict[str, dict[tuple[int, int], int]] = {}
self._xml: Optional[Element] = None

# The individual
self._chunks: List[Image.Image] = [Image.new('P', (CHUNK_DIM, CHUNK_DIM))]
self._chunks: list[Image.Image] = [Image.new('P', (CHUNK_DIM, CHUNK_DIM))]
self._palette: Optional[bytes] = None
self._dpla__colors: List[List[int]] = []
self._dpla__durations_per_frame_for_colors: List[int] = []
self._dpla__colors: list[list[int]] = []
self._dpla__durations_per_frame_for_colors: list[int] = []
self._dma__original_chunk_mappings = dma.chunk_mappings
self.dma.chunk_mappings = [0 for _ in range(0, len(dma.chunk_mappings))]

Expand All @@ -90,14 +90,14 @@ def do_import(self, dirname: str, fn_xml: str, fn_var0: str, fn_var1: str, fn_va
"but only {CHUNK_DIM}px are supported.")))

var_map = get_rule_variations(REMAP_RULES)
ts: List[str] = [os.path.basename(fn_var0), os.path.basename(fn_var1), os.path.basename(fn_var2)]
ts: list[str] = [os.path.basename(fn_var0), os.path.basename(fn_var1), os.path.basename(fn_var2)]
for i, fn in enumerate(ts):
self._import_tileset(fn, var_map, DmaType.WALL, 0, 0, TILESHEET_WIDTH, TILESHEET_HEIGHT, i, ts[i-1] if i > 0 else None)
self._import_tileset(fn, var_map, DmaType.WATER, TILESHEET_WIDTH, 0, TILESHEET_WIDTH, TILESHEET_HEIGHT, i, ts[i-1] if i > 0 else None)
self._import_tileset(fn, var_map, DmaType.FLOOR, TILESHEET_WIDTH * 2, 0, TILESHEET_WIDTH, TILESHEET_HEIGHT, i, ts[i-1] if i > 0 else None)

ani0: List[List[int]] = [[] for __ in range(0, 16)]
ani1: List[List[int]] = [[] for __ in range(0, 16)]
ani0: list[list[int]] = [[] for __ in range(0, 16)]
ani1: list[list[int]] = [[] for __ in range(0, 16)]
dur0 = [6 for __ in range(0, 16)]
dur1 = [6 for __ in range(0, 16)]
for child in self._xml:
Expand Down Expand Up @@ -144,7 +144,7 @@ def _open_tileset(self, fn):
'The palettes of the images do not match. First image read that didn\'t match: '
'"{basename}"')))

def _import_tileset(self, fn: str, rule_map: Dict[int, Set[int]], typ: int, bx, by, w, h, var_id, prev_fn: Optional[str]):
def _import_tileset(self, fn: str, rule_map: dict[int, set[int]], typ: int, bx, by, w, h, var_id, prev_fn: Optional[str]):
assert fn in self._tileset_file_map, f(_("Logic error: Tileset file {fn} was not loaded."))
assert fn in self._tileset_chunk_map, f(_("Logic error: Tileset file {fn} was not loaded."))
tileset = self._tileset_file_map[fn]
Expand Down Expand Up @@ -268,7 +268,7 @@ def _merge_chunks(self):
return new_img

def _prepare_import_animation(self, child):
colors: List[List[int]] = [[] for __ in range(0, 16)]
colors: list[list[int]] = [[] for __ in range(0, 16)]
color_animations = []
# If we have an old XML with duration on animation
if ANIMATION__DURATION in child.attrib:
Expand Down Expand Up @@ -299,5 +299,5 @@ def _finalize(self):
self.dpla.durations_per_frame_for_colors = self._dpla__durations_per_frame_for_colors

@staticmethod
def _convert_hex_str_color_to_tuple(h: str) -> Tuple[int, ...]:
def _convert_hex_str_color_to_tuple(h: str) -> tuple[int, ...]:
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
7 changes: 4 additions & 3 deletions skytemple_dtef/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#
# You should have received a copy of the GNU General Public License
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from typing import Dict, Set, Iterable, Optional
from typing import Dict, Set, Optional
from collections.abc import Iterable

from skytemple_files.graphics.dma.protocol import DmaNeighbor
from skytemple_files.common.i18n_util import _, f
Expand Down Expand Up @@ -86,14 +87,14 @@
]


def get_rule_variations(input_rules: Iterable[Optional[int]]) -> Dict[int, Set[int]]:
def get_rule_variations(input_rules: Iterable[Optional[int]]) -> dict[int, set[int]]:
"""
Returns all 256-set rules which encode the same tile in a reduced rule-set of 47 rules
(including the rule passed in). If the rule passed in is None, an empty list is returned.
Rules are ORed numbers created form DmaNeighbors. See REMAP_RULES for the 47 rule-set.
The returned value is a dict, where each key is one of the input rules, and the values ALL matching 256-rules.
"""
rules: Dict[int, Set[int]] = {x: set() for x in input_rules if x is not None}
rules: dict[int, set[int]] = {x: set() for x in input_rules if x is not None}
for rule in range(0, 256):
orig_rule = rule
if rule & DmaNeighbor.NORTH_WEST == DmaNeighbor.NORTH_WEST:
Expand Down
13 changes: 7 additions & 6 deletions skytemple_dtef/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
from math import floor

from typing import Tuple, Iterable, List, Optional, Dict
from typing import Tuple, List, Optional, Dict
from collections.abc import Iterable
from xml.etree.ElementTree import Element

from PIL import Image
Expand All @@ -26,15 +27,15 @@


class ColorAnimInfo:
def __init__(self, index: int, duration: int, frame_colors_hex: List[Tuple[int, int, int]]):
def __init__(self, index: int, duration: int, frame_colors_hex: list[tuple[int, int, int]]):
self.index = index
self.duration = duration
self.frame_color_tuples = frame_colors_hex


def apply_extended_animations(
xml: Element, var0: Image.Image, var1: Image.Image, var2: Image.Image, rest: Image.Image
) -> Iterable[Tuple[str, Image.Image]]:
) -> Iterable[tuple[str, Image.Image]]:
"""
Generates images for each frame of palette animation, yields filenames and images for the frames.
Since the animations can have different speeds, the total number of frames will be the LCM of the durations
Expand Down Expand Up @@ -95,14 +96,14 @@ def xml_filter_tags(xml: Element, tag_list) -> Element:
return new_ele


def convert_hex_str_color_to_tuple(h: str) -> Optional[Tuple[int, int, int]]:
def convert_hex_str_color_to_tuple(h: str) -> Optional[tuple[int, int, int]]:
if h is None:
return None
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) # type: ignore


def _build_color_groups(xml: Element) -> List[List[ColorAnimInfo]]:
colors_grouped: Dict[Tuple[int, int], List[ColorAnimInfo]] = {}
def _build_color_groups(xml: Element) -> list[list[ColorAnimInfo]]:
colors_grouped: dict[tuple[int, int], list[ColorAnimInfo]] = {}
for node in xml:
if node.tag == ANIMATION:
ci_base = 16 * (int(node.attrib[ANIMATION__PALETTE]))
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tox]
envlist = py38,py39,py310,py311
envlist = py39,py310,py311,py312
skipsdist = {env:TOXBUILD:false}

[gh-actions]
python =
3.8: py38, mypy
3.9: py39
3.10: py310
3.11: py311
3.12: py312

[testenv]
commands =
Expand Down

0 comments on commit c57ba52

Please sign in to comment.