Skip to content

Commit

Permalink
refactor: formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuramaruzxc committed Mar 14, 2024
1 parent 1359c23 commit 76c5d09
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 flake8-import-order black mypy wheel coverage
pip install flake8 flake8-import-order black isort mypy wheel coverage
pip install -r requirements.txt
sudo apt-get install dmz-cursor-theme
- name: Format with black
run: black .
- name: Auto Formatting
run: |
black .
isort --profile black .
- name: Lint with flake8
run: flake8 .
- name: Typecheck with mypy
Expand Down
5 changes: 2 additions & 3 deletions cursorgen/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys
from multiprocessing import cpu_count

import argparse
import os
import sys
import traceback
from multiprocessing import cpu_count
from multiprocessing.pool import ThreadPool
from threading import Lock
from typing import BinaryIO
Expand Down
12 changes: 6 additions & 6 deletions cursorgen/parser/ani.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, blob: bytes) -> None:
self.frames = self._parse(self.RIFF_HEADER.size)

def _unpack(self, struct_cls: struct.Struct, offset: int) -> Tuple[Any, ...]:
return struct_cls.unpack(self.blob[offset : offset + struct_cls.size])
return struct_cls.unpack(self.blob[offset: offset + struct_cls.size])

def _read_chunk(
self, offset: int, expected: Iterable[bytes]
Expand Down Expand Up @@ -78,7 +78,7 @@ def _parse(self, offset: int) -> List[CursorFrame]:
planes,
display_rate,
flags,
) = self.ANIH_HEADER.unpack(self.blob[offset : offset + self.ANIH_HEADER.size])
) = self.ANIH_HEADER.unpack(self.blob[offset: offset + self.ANIH_HEADER.size])

if size != self.ANIH_HEADER.size:
raise ValueError(
Expand All @@ -100,7 +100,7 @@ def _parse(self, offset: int) -> List[CursorFrame]:
)
if name == self.LIST_CHUNK:
list_end = offset + size
if self.blob[offset : offset + 4] != self.FRAME_TYPE:
if self.blob[offset: offset + 4] != self.FRAME_TYPE:
raise ValueError(
f"Unexpected RIFF list type: {self.blob[offset:offset + 4]!r}, expected {self.FRAME_TYPE!r}"
)
Expand All @@ -111,7 +111,7 @@ def _parse(self, offset: int) -> List[CursorFrame]:
offset, expected=[self.ICON_CHUNK]
)
frames.append(
CURParser(self.blob[offset : offset + size]).frames[0]
CURParser(self.blob[offset: offset + size]).frames[0]
)
offset += size
if offset & 1:
Expand All @@ -125,7 +125,7 @@ def _parse(self, offset: int) -> List[CursorFrame]:
order = [
i
for i, in self.UNSIGNED.iter_unpack(
self.blob[offset : offset + size]
self.blob[offset: offset + size]
)
]
if len(order) != step_count:
Expand All @@ -137,7 +137,7 @@ def _parse(self, offset: int) -> List[CursorFrame]:
delays = [
i
for i, in self.UNSIGNED.iter_unpack(
self.blob[offset : offset + size]
self.blob[offset: offset + size]
)
]
if len(delays) != step_count:
Expand Down
21 changes: 11 additions & 10 deletions cursorgen/parser/bmp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import PIL.Image as ImageType
import struct
from typing import Any, Dict, List, Tuple

import PIL.Image as ImageType
from PIL import Image
from typing import Dict, Tuple, Any, List

from cursorgen.parser.base import BaseParser

Expand Down Expand Up @@ -30,7 +31,7 @@ def __init__(self, blob: bytes) -> None:
self.frame = self._parse()

def _unpack(self, struct_cls: struct.Struct, offset: int) -> Tuple[Any, ...]:
return struct_cls.unpack(self.blob[offset : offset + struct_cls.size])
return struct_cls.unpack(self.blob[offset: offset + struct_cls.size])

def _extract(self) -> Dict:
"""Gets bitmap parameters.
Expand Down Expand Up @@ -68,9 +69,9 @@ def _extract(self) -> Dict:
if palette_size < 0:
palette_size = 0

Palette = self.blob[size : size + palette_size]
XORData = self.blob[size + palette_size : size + palette_size + XOR_size]
ANDData = self.blob[size + palette_size + XOR_size : len(self.blob)]
Palette = self.blob[size: size + palette_size]
XORData = self.blob[size + palette_size: size + palette_size + XOR_size]
ANDData = self.blob[size + palette_size + XOR_size: len(self.blob)]

parameters = {
"size": size,
Expand Down Expand Up @@ -117,7 +118,7 @@ def _parse(self) -> ImageType:
image_data = []
for i in range(0, len(self.parameters["xor"]), 2):
data = int.from_bytes(
self.parameters["xor"][i : i + 2], byteorder="little"
self.parameters["xor"][i: i + 2], byteorder="little"
)
b = (data & 0x7C00) >> 10
g = (data & 0x3E0) >> 5
Expand Down Expand Up @@ -174,11 +175,11 @@ def _parse(self) -> ImageType:
if self.parameters["palette"] and self.parameters["bpp"] <= 8:
image = image.convert("P")
palette_int = [
self.parameters["palette"][i : i + 3]
self.parameters["palette"][i: i + 3]
for i in range(0, self.parameters["size_pal"], 4)
]
rsv = [
self.parameters["palette"][i + 3 : i + 4]
self.parameters["palette"][i + 3: i + 4]
for i in range(0, self.parameters["size_pal"], 4)
]

Expand Down Expand Up @@ -238,7 +239,7 @@ def is_png(blob: bytes) -> bool:
def is_gray(self):
"""Determines whether an image is grayscale (from palette)."""
chunks = [
self.parameters["palette"][i : i + 3]
self.parameters["palette"][i: i + 3]
for i in range(0, self.parameters["size_pal"], 4)
]
if all(elem == block[0] for block in chunks for elem in block):
Expand Down
7 changes: 4 additions & 3 deletions cursorgen/parser/cur.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import struct
from PIL import Image
from typing import List

from PIL import Image

from cursorgen.parser.base import BaseParser
from cursorgen.parser.bmp import BMPParser
from cursorgen.utils.cursor import CursorFrame, CursorImage
Expand Down Expand Up @@ -35,10 +36,10 @@ def _parse(self) -> [list[tuple[int, int]]]:
for i in range(image_count):
width, height, palette, reserved, hx, hy, size, file_offset = (
self.ICON_DIR_ENTRY.unpack(
self.blob[offset : offset + self.ICON_DIR_ENTRY.size]
self.blob[offset: offset + self.ICON_DIR_ENTRY.size]
)
)
self.image_data.append(self.blob[file_offset : file_offset + size])
self.image_data.append(self.blob[file_offset: file_offset + size])
hotspots.append((hx, hy))

offset += self.ICON_DIR_ENTRY.size
Expand Down
8 changes: 4 additions & 4 deletions cursorgen/parser/xcursor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import struct
from collections import defaultdict
from typing import Any, Dict, List, Tuple, cast

import struct
from PIL import Image
from typing import Any, Dict, List, Tuple, cast

from cursorgen.parser.base import BaseParser
from cursorgen.utils.cursor import CursorFrame, CursorImage
Expand All @@ -25,7 +25,7 @@ def __init__(self, blob: bytes) -> None:
self.frames = self._parse()

def _unpack(self, struct_cls: struct.Struct, offset: int) -> Tuple[Any, ...]:
return struct_cls.unpack(self.blob[offset : offset + struct_cls.size])
return struct_cls.unpack(self.blob[offset: offset + struct_cls.size])

def _parse(self) -> List[CursorFrame]:
magic, header_size, version, toc_size = self._unpack(self.FILE_HEADER, 0)
Expand Down Expand Up @@ -89,7 +89,7 @@ def _parse(self) -> List[CursorFrame]:

image_start = position + self.IMAGE_HEADER.size
image_size = width * height * 4
blob = self.blob[image_start : image_start + image_size]
blob = self.blob[image_start: image_start + image_size]
if len(blob) != image_size:
raise ValueError(
f"Invalid image at {image_start}: expected {image_size} bytes, got {len(blob)} bytes"
Expand Down
3 changes: 2 additions & 1 deletion cursorgen/utils/cursor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from PIL import Image
from typing import Iterator, List, Tuple

from PIL import Image


class CursorImage:
image: Image.Image
Expand Down
4 changes: 2 additions & 2 deletions cursorgen/writer/x11.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from itertools import chain

from PIL import Image
from operator import itemgetter
from typing import List, Optional

from PIL import Image

from cursorgen.parser import XCursorParser
from cursorgen.utils.cursor import CursorFrame

Expand Down

0 comments on commit 76c5d09

Please sign in to comment.