Skip to content

Commit 1ae7899

Browse files
Merge pull request #8 from danila-schelkov/lint-workflow
Lint workflow
2 parents 112cbc2 + e9c2d95 commit 1ae7899

File tree

17 files changed

+242
-180
lines changed

17 files changed

+242
-180
lines changed

.flake8

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[flake8]
22
max-line-length = 88
3-
ignore = E203
3+
ignore = E203, W503
4+
exclude =
5+
updates
6+
venv

.github/workflows/lint.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Lint
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v3
11+
- name: Set up Python 3.10
12+
uses: actions/setup-python@v2
13+
with:
14+
python-version: "3.10"
15+
16+
- name: Install requirements
17+
run: |
18+
pip install pipx
19+
pipx install cookiecutter
20+
pipx runpip cookiecutter install -r requirements.txt
21+
22+
- name: Linting code by flake8
23+
run: |
24+
pipx run flake8 --show-source --statistics
25+
26+
- name: Check types by pyright
27+
run: |
28+
pipx run pyright

.pre-commit-config.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.0.1
4+
hooks:
5+
- id: end-of-file-fixer
6+
- repo: https://github.com/PyCQA/isort
7+
rev: 5.12.0
8+
hooks:
9+
- id: isort
10+
args: ['--profile','black']
211
- repo: https://github.com/psf/black
312
rev: 22.12.0
413
hooks:
@@ -7,4 +16,4 @@ repos:
716
- repo: https://github.com/PyCQA/flake8
817
rev: 6.0.0
918
hooks:
10-
- id: flake8
19+
- id: flake8

system/bytestream.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import io
2-
from typing import Literal
2+
from typing import Literal, Optional, Union
33

44

55
class Reader(io.BytesIO):
66
def __init__(
7-
self, buffer: bytes = b"", endian: Literal["little", "big"] = "little"
7+
self,
8+
buffer: bytes = b"",
9+
endian: Union[Literal["little"], Literal["big"]] = "little",
810
):
911
super().__init__(buffer)
1012

@@ -68,7 +70,7 @@ def write_uint32(self, integer: int):
6870
def write_int32(self, integer: int):
6971
self.write_int(integer, 4, True)
7072

71-
def write_string(self, string: str = None):
73+
def write_string(self, string: Optional["str"] = None):
7274
if string is None:
7375
self.write_byte(255)
7476
return

system/lib/features/cut_sprites.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ def cut_sprites(swf: SupercellSWF, export_folder: str):
1111
os.makedirs(f"{export_folder}/movie_clips", exist_ok=True)
1212

1313
# TODO: Too slow, fix it
14-
# movie_clips_skipped = 0
15-
# movie_clip_count = len(swf.movie_clips)
16-
# for movie_clip_index in range(movie_clip_count):
17-
# movie_clip = swf.movie_clips[movie_clip_index]
18-
#
19-
# rendered_movie_clip = movie_clip.render(swf)
20-
# if sum(rendered_movie_clip.size) >= 2:
21-
# rendered_movie_clip.save(f'{export_folder}/movie_clips/{movie_clip.export_name or movie_clip.id}.png')
22-
# else:
23-
# # For debug:
24-
# # logger.warning(f'MovieClip {movie_clip.id} cannot be rendered.')
25-
# movie_clips_skipped += 1
26-
#
27-
# Console.progress_bar(
28-
# 'Rendering movie clips (%d/%d). Skipped count: %d' %
29-
# (movie_clip_index + 1, movie_clip_count, movie_clips_skipped),
30-
# movie_clip_index,
31-
# movie_clip_count
32-
# )
14+
movie_clips_skipped = 0
15+
movie_clip_count = len(swf.movie_clips)
16+
for movie_clip_index in range(movie_clip_count):
17+
movie_clip = swf.movie_clips[movie_clip_index]
18+
19+
rendered_movie_clip = movie_clip.render(swf)
20+
if sum(rendered_movie_clip.size) >= 2:
21+
clip_name = movie_clip.export_name or movie_clip.id
22+
rendered_movie_clip.save(f"{export_folder}/movie_clips/{clip_name}.png")
23+
else:
24+
# For debug:
25+
# logger.warning(f'MovieClip {movie_clip.id} cannot be rendered.')
26+
movie_clips_skipped += 1
27+
28+
Console.progress_bar(
29+
"Rendering movie clips (%d/%d). Skipped count: %d"
30+
% (movie_clip_index + 1, movie_clip_count, movie_clips_skipped),
31+
movie_clip_index,
32+
movie_clip_count,
33+
)
3334

3435
shapes_count = len(swf.shapes)
3536
swf.xcod_writer.write_uint16(shapes_count)

system/lib/features/place_sprites.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import os
2+
from typing import List, Tuple
23

34
from PIL import Image, ImageDraw
45

56
from system.lib import Console
67
from system.lib.helper import get_sides, get_size
78
from system.lib.images import get_format_by_pixel_type
8-
from system.lib.xcod import parse_info, FileInfo
9+
from system.lib.xcod import FileInfo, parse_info
910
from system.localization import locale
1011

1112

1213
def place_sprites(
1314
xcod_path: str, folder: str, overwrite: bool = False
14-
) -> (list, FileInfo):
15+
) -> Tuple[List[Image.Image], FileInfo]:
1516
file_info, xcod = parse_info(xcod_path)
1617

1718
files_to_overwrite = os.listdir(f'{folder}{"/overwrite" if overwrite else ""}')
@@ -83,7 +84,7 @@ def place_sprites(
8384

8485
width, height = get_size(left, top, right, bottom)
8586

86-
bbox = left, top, right, bottom
87+
bbox = int(left), int(top), int(right), int(bottom)
8788

8889
tmp_region = Image.open(
8990
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'

system/lib/features/sc/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import os
22
import struct
3+
from typing import List, Optional
34

4-
from PIL import Image
55
from loguru import logger
6+
from PIL import Image
67

78
from system.bytestream import Writer
89
from system.lib.console import Console
910
from system.lib.features.files import write_sc
10-
from system.lib.images import get_pixel_size, split_image, save_texture
11+
from system.lib.images import get_pixel_size, save_texture, split_image
1112
from system.lib.xcod import FileInfo
1213
from system.localization import locale
1314

1415

1516
def compile_sc(
16-
_dir, file_info: FileInfo, sheets: list = None, output_folder: str = None
17+
_dir,
18+
file_info: FileInfo,
19+
sheets: Optional[List[Image.Image]] = None,
20+
output_folder: Optional[str] = None,
1721
):
1822
name = _dir.split("/")[-2]
1923

system/lib/helper.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import List, Tuple
1+
from typing import List, Tuple, Union
22

33
from system.lib.objects.point import Point
44

55

6-
def get_size(left: float, top: float, right: float, bottom: float) -> (int, int):
6+
def get_size(left: float, top: float, right: float, bottom: float) -> Tuple[int, int]:
77
"""Returns width and height of given rect.
88
99
:param left: left side of polygon
@@ -16,13 +16,14 @@ def get_size(left: float, top: float, right: float, bottom: float) -> (int, int)
1616

1717

1818
def get_sides(
19-
points: List[Tuple[float, float]] or List[Point]
20-
) -> (float, float, float, float):
19+
points: Union[List[Tuple[float, float]], List[Tuple[int, int]], List[Point]]
20+
) -> Tuple[float, float, float, float]:
2121
"""Calculates and returns rect sides.
2222
2323
:param points: polygon points
2424
:return: left, top, right, bottom
2525
"""
26+
2627
if len(points) > 0:
2728
point = points[0]
2829
if isinstance(point, Point):

system/lib/images.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
import struct
33

4+
import PIL.PyAccess
45
from PIL import Image
56

67
from system.bytestream import Reader
@@ -10,9 +11,10 @@
1011
CHUNK_SIZE = 32
1112

1213

13-
def load_image_from_buffer(img: Image) -> None:
14+
def load_image_from_buffer(img: Image.Image) -> None:
1415
width, height = img.size
15-
img_loaded = img.load()
16+
# noinspection PyTypeChecker
17+
img_loaded: PIL.PyAccess.PyAccess = img.load()
1618

1719
with open("pixel_buffer", "rb") as pixel_buffer:
1820
channels_count = int.from_bytes(pixel_buffer.read(1), "little")
@@ -22,12 +24,13 @@ def load_image_from_buffer(img: Image) -> None:
2224
img_loaded[x, y] = tuple(pixel_buffer.read(channels_count))
2325

2426

25-
def join_image(img: Image) -> None:
27+
def join_image(img: Image.Image) -> None:
2628
with open("pixel_buffer", "rb") as pixel_buffer:
2729
channels_count = int.from_bytes(pixel_buffer.read(1), "little")
2830

2931
width, height = img.size
30-
loaded_img = img.load()
32+
# noinspection PyTypeChecker
33+
loaded_img: PIL.PyAccess.PyAccess = img.load()
3134

3235
x_chunks_count = width // CHUNK_SIZE
3336
y_chunks_count = height // CHUNK_SIZE
@@ -51,13 +54,15 @@ def join_image(img: Image) -> None:
5154
Console.progress_bar(locale.join_pic, y_chunk, y_chunks_count + 1)
5255

5356

54-
def split_image(img: Image):
57+
def split_image(img: Image.Image):
5558
def add_pixel(pixel: tuple):
5659
loaded_image[pixel_index % width, int(pixel_index / width)] = pixel
5760

5861
width, height = img.size
59-
loaded_image = img.load()
60-
loaded_clone = img.copy().load()
62+
# noinspection PyTypeChecker
63+
loaded_image: PIL.PyAccess.PyAccess = img.load()
64+
# noinspection PyTypeChecker
65+
loaded_clone: PIL.PyAccess.PyAccess = img.copy().load()
6166

6267
x_chunks_count = width // CHUNK_SIZE
6368
y_chunks_count = height // CHUNK_SIZE
@@ -180,53 +185,54 @@ def read_pixel():
180185

181186

182187
def save_texture(sc, img, _type):
183-
write_pixel = None
184188
if _type in (0, 1):
185189

186190
def write_pixel(pixel):
187191
return struct.pack("4B", *pixel)
188192

189-
if _type == 2:
193+
elif _type == 2:
190194

191195
def write_pixel(pixel):
192196
r, g, b, a = pixel
193197
return struct.pack("<H", a >> 4 | b >> 4 << 4 | g >> 4 << 8 | r >> 4 << 12)
194198

195-
if _type == 3:
199+
elif _type == 3:
196200

197201
def write_pixel(pixel):
198202
r, g, b, a = pixel
199203
return struct.pack("<H", a >> 7 | b >> 3 << 1 | g >> 3 << 6 | r >> 3 << 11)
200204

201-
if _type == 4:
205+
elif _type == 4:
202206

203207
def write_pixel(pixel):
204208
r, g, b = pixel
205209
return struct.pack("<H", b >> 3 | g >> 2 << 5 | r >> 3 << 11)
206210

207-
if _type == 6:
211+
elif _type == 6:
208212

209213
def write_pixel(pixel):
210214
return struct.pack("2B", *pixel[::-1])
211215

212-
if _type == 10:
216+
elif _type == 10:
213217

214218
def write_pixel(pixel):
215219
return struct.pack("B", pixel)
216220

217-
if write_pixel is not None:
218-
width, height = img.size
221+
else:
222+
return
219223

220-
pix = img.getdata()
221-
point = -1
222-
for y in range(height):
223-
for x in range(width):
224-
sc.write(write_pixel(pix[y * width + x]))
224+
width, height = img.size
225225

226-
curr = Console.percent(y, height)
227-
if curr > point:
228-
Console.progress_bar(locale.writing_pic, y, height)
229-
point = curr
226+
pix = img.getdata()
227+
point = -1
228+
for y in range(height):
229+
for x in range(width):
230+
sc.write(write_pixel(pix[y * width + x]))
231+
232+
curr = Console.percent(y, height)
233+
if curr > point:
234+
Console.progress_bar(locale.writing_pic, y, height)
235+
point = curr
230236

231237

232238
def transform_image(image, scale_x, scale_y, angle, x, y):

system/lib/objects/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
from system.lib.objects.shape import Shape
21
from system.lib.objects.movie_clip import MovieClip
2+
from system.lib.objects.shape import Shape
33
from system.lib.objects.texture import SWFTexture
4+
5+
__all__ = ["Shape", "MovieClip", "SWFTexture"]

0 commit comments

Comments
 (0)