Skip to content

Commit a25dab0

Browse files
authored
feat: tweak ruff rules (#306)
1 parent 139ee83 commit a25dab0

File tree

16 files changed

+144
-110
lines changed

16 files changed

+144
-110
lines changed

CHANGES.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Technical Changes
22

3+
## 10.0.0 (2024-11-xx)
4+
5+
### base.py
6+
- Added `OPAQUE`
7+
8+
### darwin.py
9+
- Added `MAC_VERSION_CATALINA`
10+
11+
### linux.py
12+
- Added `BITS_PER_PIXELS_32`
13+
- Added `SUPPORTED_BITS_PER_PIXELS`
14+
315
## 9.0.0 (2023-04-18)
416

517
### linux.py

docs/source/examples/callback.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
Screenshot of the monitor 1, with callback.
55
"""
66

7-
import os
8-
import os.path
7+
from pathlib import Path
98

109
import mss
1110

1211

1312
def on_exists(fname: str) -> None:
1413
"""Callback example when we try to overwrite an existing screenshot."""
15-
if os.path.isfile(fname):
16-
newfile = f"{fname}.old"
17-
print(f"{fname} -> {newfile}")
18-
os.rename(fname, newfile)
14+
file = Path(fname)
15+
if file.is_file():
16+
newfile = file.with_name(f"{file.name}.old")
17+
print(f"{fname}{newfile}")
18+
file.rename(newfile)
1919

2020

2121
with mss.mss() as sct:

pyproject.toml

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,34 @@ line-length = 120
152152
indent-width = 4
153153
target-version = "py39"
154154

155-
[tool.ruff.lint]
156-
extend-select = ["ALL"]
157-
ignore = [
158-
"ANN101",
159-
"ANN401",
160-
"C90",
161-
"COM812",
162-
"D", # TODO
163-
"ERA",
164-
"FBT",
165-
"INP001",
166-
"ISC001",
167-
"PTH",
168-
"PL",
169-
"S",
170-
"SLF",
171-
"T201",
172-
]
173-
fixable = ["ALL"]
174-
175155
[tool.ruff.format]
176156
quote-style = "double"
177157
indent-style = "space"
178158
skip-magic-trailing-comma = false
179159
line-ending = "auto"
160+
161+
[tool.ruff.lint]
162+
fixable = ["ALL"]
163+
extend-select = ["ALL"]
164+
ignore = [
165+
"ANN401", # typing.Any
166+
"C90", # complexity
167+
"COM812", # conflict
168+
"D", # TODO
169+
"ISC001", # conflict
170+
"T201", # `print()`
171+
]
172+
173+
[tool.ruff.lint.per-file-ignores]
174+
"docs/source/*" = [
175+
"ERA001", # commented code
176+
"INP001", # file `xxx` is part of an implicit namespace package
177+
]
178+
"src/tests/*" = [
179+
"FBT001", # boolean-typed positional argument in function definition
180+
"PLR2004", # magic value used in comparison
181+
"S101", # use of `assert` detected
182+
"S602", # `subprocess` call with `shell=True`
183+
"S603", # `subprocess` call
184+
"SLF001", # private member accessed
185+
]

src/mss/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
lock = Lock()
3030

31+
OPAQUE = 255
32+
3133

3234
class MSSBase(metaclass=ABCMeta):
3335
"""This class will be overloaded by a system specific one."""
@@ -200,7 +202,6 @@ def shot(self, /, **kwargs: Any) -> str:
200202
@staticmethod
201203
def _merge(screenshot: ScreenShot, cursor: ScreenShot, /) -> ScreenShot:
202204
"""Create composite image by blending screenshot and mouse cursor."""
203-
204205
(cx, cy), (cw, ch) = cursor.pos, cursor.size
205206
(x, y), (w, h) = screenshot.pos, screenshot.size
206207

@@ -234,7 +235,7 @@ def _merge(screenshot: ScreenShot, cursor: ScreenShot, /) -> ScreenShot:
234235
if not alpha:
235236
continue
236237

237-
if alpha == 255:
238+
if alpha == OPAQUE:
238239
screen_raw[spos : spos + 3] = cursor_raw[cpos : cpos + 3]
239240
else:
240241
alpha2 = alpha / 255

src/mss/darwin.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
__all__ = ("MSS",)
2222

23+
MAC_VERSION_CATALINA = 10.16
24+
2325

2426
def cgfloat() -> type[c_double | c_float]:
2527
"""Get the appropriate value for a float."""
@@ -59,7 +61,7 @@ def __repr__(self) -> str:
5961
#
6062
# Note: keep it sorted by cfunction.
6163
CFUNCTIONS: CFunctions = {
62-
# cfunction: (attr, argtypes, restype)
64+
# Syntax: cfunction: (attr, argtypes, restype)
6365
"CGDataProviderCopyData": ("core", [c_void_p], c_void_p),
6466
"CGDisplayBounds": ("core", [c_uint32], CGRect),
6567
"CGDisplayRotation": ("core", [c_uint32], c_float),
@@ -98,7 +100,7 @@ def __init__(self, /, **kwargs: Any) -> None:
98100
def _init_library(self) -> None:
99101
"""Load the CoreGraphics library."""
100102
version = float(".".join(mac_ver()[0].split(".")[:2]))
101-
if version < 10.16:
103+
if version < MAC_VERSION_CATALINA:
102104
coregraphics = ctypes.util.find_library("CoreGraphics")
103105
else:
104106
# macOS Big Sur and newer
@@ -136,9 +138,13 @@ def _monitors_impl(self) -> None:
136138
rect = core.CGDisplayBounds(display)
137139
rect = core.CGRectStandardize(rect)
138140
width, height = rect.size.width, rect.size.height
141+
142+
# 0.0: normal
143+
# 90.0: right
144+
# -90.0: left
139145
if core.CGDisplayRotation(display) in {90.0, -90.0}:
140-
# {0.0: "normal", 90.0: "right", -90.0: "left"}
141146
width, height = height, width
147+
142148
self._monitors.append(
143149
{
144150
"left": int_(rect.origin.x),
@@ -161,7 +167,6 @@ def _monitors_impl(self) -> None:
161167

162168
def _grab_impl(self, monitor: Monitor, /) -> ScreenShot:
163169
"""Retrieve all pixels from a monitor. Pixels have to be RGB."""
164-
165170
core = self.core
166171
rect = CGRect((monitor["left"], monitor["top"]), (monitor["width"], monitor["height"]))
167172

src/mss/factory.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def mss(**kwargs: Any) -> MSSBase:
1919
It then proxies its arguments to the class for
2020
instantiation.
2121
"""
22-
2322
os_ = platform.system().lower()
2423

2524
if os_ == "darwin":

src/mss/linux.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242

4343
PLAINMASK = 0x00FFFFFF
4444
ZPIXMAP = 2
45+
BITS_PER_PIXELS_32 = 32
46+
SUPPORTED_BITS_PER_PIXELS = {
47+
BITS_PER_PIXELS_32,
48+
}
4549

4650

4751
class Display(Structure):
@@ -233,7 +237,7 @@ def _validate(retval: int, func: Any, args: tuple[Any, Any], /) -> tuple[Any, An
233237
#
234238
# Note: keep it sorted by cfunction.
235239
CFUNCTIONS: CFunctions = {
236-
# cfunction: (attr, argtypes, restype)
240+
# Syntax: cfunction: (attr, argtypes, restype)
237241
"XCloseDisplay": ("xlib", [POINTER(Display)], c_void_p),
238242
"XDefaultRootWindow": ("xlib", [POINTER(Display)], POINTER(XWindowAttributes)),
239243
"XDestroyImage": ("xlib", [POINTER(XImage)], c_void_p),
@@ -433,7 +437,7 @@ def _grab_impl(self, monitor: Monitor, /) -> ScreenShot:
433437

434438
try:
435439
bits_per_pixel = ximage.contents.bits_per_pixel
436-
if bits_per_pixel != 32:
440+
if bits_per_pixel not in SUPPORTED_BITS_PER_PIXELS:
437441
msg = f"[XImage] bits per pixel value not (yet?) implemented: {bits_per_pixel}."
438442
raise ScreenShotError(msg)
439443

src/mss/tools.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
import os
88
import struct
99
import zlib
10+
from typing import TYPE_CHECKING
1011

12+
if TYPE_CHECKING:
13+
from pathlib import Path
1114

12-
def to_png(data: bytes, size: tuple[int, int], /, *, level: int = 6, output: str | None = None) -> bytes | None:
15+
16+
def to_png(data: bytes, size: tuple[int, int], /, *, level: int = 6, output: Path | str | None = None) -> bytes | None:
1317
"""Dump data to a PNG file. If `output` is `None`, create no file but return
1418
the whole PNG data.
1519
@@ -18,7 +22,6 @@ def to_png(data: bytes, size: tuple[int, int], /, *, level: int = 6, output: str
1822
:param int level: PNG compression level.
1923
:param str output: Output file name.
2024
"""
21-
2225
pack = struct.pack
2326
crc32 = zlib.crc32
2427

@@ -49,7 +52,7 @@ def to_png(data: bytes, size: tuple[int, int], /, *, level: int = 6, output: str
4952
# Returns raw bytes of the whole PNG data
5053
return magic + b"".join(ihdr + idat + iend)
5154

52-
with open(output, "wb") as fileh:
55+
with open(output, "wb") as fileh: # noqa: PTH123
5356
fileh.write(magic)
5457
fileh.write(b"".join(ihdr))
5558
fileh.write(b"".join(idat))

src/mss/windows.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class BITMAPINFO(Structure):
7474
#
7575
# Note: keep it sorted by cfunction.
7676
CFUNCTIONS: CFunctions = {
77-
# cfunction: (attr, argtypes, restype)
77+
# Syntax: cfunction: (attr, argtypes, restype)
7878
"BitBlt": ("gdi32", [HDC, INT, INT, INT, INT, HDC, INT, INT, DWORD], BOOL),
7979
"CreateCompatibleBitmap": ("gdi32", [HDC, INT, INT], HBITMAP),
8080
"CreateCompatibleDC": ("gdi32", [HDC], HDC),
@@ -179,7 +179,6 @@ def _callback(_monitor: int, _data: HDC, rect: LPRECT, _dc: LPARAM) -> int:
179179
"""Callback for monitorenumproc() function, it will return
180180
a RECT with appropriate values.
181181
"""
182-
183182
rct = rect.contents
184183
self._monitors.append(
185184
{

src/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)