Skip to content

Commit

Permalink
black + ruff pass
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 28, 2024
1 parent ed626d1 commit d2c689d
Show file tree
Hide file tree
Showing 32 changed files with 349 additions and 115 deletions.
24 changes: 17 additions & 7 deletions moderngl_window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from moderngl_window.conf import settings
from moderngl_window.context.base import BaseWindow, WindowConfig
from moderngl_window.timers.clock import Timer
from moderngl_window.utils.keymaps import (AZERTY, QWERTY, KeyMap, # noqa
KeyMapFactory)
from moderngl_window.utils.keymaps import AZERTY, QWERTY, KeyMap, KeyMapFactory # noqa
from moderngl_window.utils.module_loading import import_string

__version__ = "3.0.0"
Expand Down Expand Up @@ -74,7 +73,9 @@ class ContextRefs:
CONTEXT: Optional[moderngl.Context] = None


def activate_context(window: Optional[BaseWindow] = None, ctx: Optional[moderngl.Context] = None) -> None:
def activate_context(
window: Optional[BaseWindow] = None, ctx: Optional[moderngl.Context] = None
) -> None:
"""
Register the active window and context.
If only a window is supplied the context is taken from the window.
Expand Down Expand Up @@ -122,9 +123,12 @@ def get_window_cls(window: str = "") -> type[BaseWindow]:
logger.info("Attempting to load window class: %s", window)
win = import_string(window)

assert issubclass(win, BaseWindow), f"{win} is not derived from moderngl_window.context.base.BaseWindow"
assert issubclass(
win, BaseWindow
), f"{win} is not derived from moderngl_window.context.base.BaseWindow"
return win


def get_local_window_cls(window: Optional[str] = None) -> type[BaseWindow]:
"""
Attempt to obtain a window class in the moderngl_window package
Expand Down Expand Up @@ -172,15 +176,19 @@ def create_window_from_settings() -> BaseWindow:
window_cls = import_string(settings.WINDOW["class"])
window = window_cls(**settings.WINDOW)

assert isinstance(window, BaseWindow), f"{type(window)} is not derived from moderngl_window.context.base.BaseWindow"
assert isinstance(
window, BaseWindow
), f"{type(window)} is not derived from moderngl_window.context.base.BaseWindow"
activate_context(window=window)
return window


# --- The simple window config system ---


def run_window_config(config_cls: type[WindowConfig], timer: Optional[Timer] = None, args: Any = None) -> None:
def run_window_config(
config_cls: type[WindowConfig], timer: Optional[Timer] = None, args: Any = None
) -> None:
"""
Run an WindowConfig entering a blocking main loop
Expand Down Expand Up @@ -322,7 +330,9 @@ def create_parser() -> argparse.ArgumentParser:
return parser


def parse_args(args: Optional[Any] = None, parser: Optional[argparse.ArgumentParser] = None) -> argparse.Namespace:
def parse_args(
args: Optional[Any] = None, parser: Optional[argparse.ArgumentParser] = None
) -> argparse.Namespace:
"""Parse arguments from sys.argv
Passing in your own argparser can be user to extend the parser.
Expand Down
6 changes: 4 additions & 2 deletions moderngl_window/capture/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def _remove_file(self) -> None:
if os.path.exists(self._filename):
os.remove(self._filename)

def start_capture(self, filename: Optional[str] = None, framerate: Union[int, float] = 60) -> None:
def start_capture(
self, filename: Optional[str] = None, framerate: Union[int, float] = 60
) -> None:
"""
Start the capturing process
Expand Down Expand Up @@ -132,7 +134,7 @@ def save(self) -> None:
"""
if not self._recording:
return

if self._source is None:
return

Expand Down
85 changes: 64 additions & 21 deletions moderngl_window/context/base/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from moderngl_window.context.base import BaseKeys, KeyModifiers
from moderngl_window.geometry.attributes import AttributeNames
from moderngl_window.loaders.texture.icon import IconLoader
from moderngl_window.meta import (DataDescription, ProgramDescription,
SceneDescription, TextureDescription)
from moderngl_window.meta import (
DataDescription,
ProgramDescription,
SceneDescription,
TextureDescription,
)
from moderngl_window.scene import Scene
from moderngl_window.timers.base import BaseTimer

Expand Down Expand Up @@ -82,10 +86,10 @@ class BaseWindow:

def __init__(
self,
title: str="ModernGL",
title: str = "ModernGL",
gl_version: tuple[int, int] = (3, 3),
size: tuple[int, int] = (1280, 720),
resizable: bool= True,
resizable: bool = True,
visible: bool = True,
fullscreen: bool = False,
vsync: bool = True,
Expand Down Expand Up @@ -140,7 +144,9 @@ def __init__(
self._mouse_drag_event_func: Callable[[int, int, int, int], None] = dummy_func
self._mouse_scroll_event_func: Callable[[float, float], None] = dummy_func
self._unicode_char_entered_func: Callable[[str], None] = dummy_func
self._files_dropped_event_func: Callable[[int, int, list[Union[str, Path]]], None] = dummy_func
self._files_dropped_event_func: Callable[[int, int, list[Union[str, Path]]], None] = (
dummy_func
)
self._on_generic_event_func: Callable[[Event], None] = dummy_func

# Internal states
Expand Down Expand Up @@ -555,7 +561,9 @@ def files_dropped_event_func(self) -> Callable[[int, int, list[Union[str, Path]]

@files_dropped_event_func.setter
@require_callable
def files_dropped_event_func(self, func: Callable[[int, int, list[Union[str, Path]]], None]) -> None:
def files_dropped_event_func(
self, func: Callable[[int, int, list[Union[str, Path]]], None]
) -> None:
self._files_dropped_event_func = func

@property
Expand Down Expand Up @@ -585,7 +593,7 @@ def mouse_position_event_func(self) -> Callable[[int, int, int, int], None]:

@mouse_position_event_func.setter
@require_callable
def mouse_position_event_func(self, func:Callable[[int, int, int, int], None]) -> None:
def mouse_position_event_func(self, func: Callable[[int, int, int, int], None]) -> None:
self._mouse_position_event_func = func

@property
Expand Down Expand Up @@ -673,7 +681,9 @@ def _handle_mouse_button_state_change(self, button: int, pressed: bool) -> None:
else:
raise ValueError("Incompatible mouse button number: {}".format(button))

def convert_window_coordinates(self, x: int, y: int, x_flipped: bool = False, y_flipped: bool = False) -> tuple[int, int]:
def convert_window_coordinates(
self, x: int, y: int, x_flipped: bool = False, y_flipped: bool = False
) -> tuple[int, int]:
"""
Convert window coordinates to top-left coordinate space.
The default origin is the top left corner of the window.
Expand Down Expand Up @@ -716,7 +726,15 @@ def use(self) -> None:
"""Bind the window's framebuffer"""
self._ctx.screen.use()

def clear(self, red: float =0.0, green: float=0.0, blue: float=0.0, alpha: float=0.0, depth: float =1.0, viewport: Optional[tuple[int, int, int, int]]=None) -> None:
def clear(
self,
red: float = 0.0,
green: float = 0.0,
blue: float = 0.0,
alpha: float = 0.0,
depth: float = 1.0,
viewport: Optional[tuple[int, int, int, int]] = None,
) -> None:
"""
Binds and clears the default framebuffer
Expand Down Expand Up @@ -866,7 +884,9 @@ def _calc_mouse_delta(self, xpos: int, ypos: int) -> tuple[int, int]:
return dx, dy

@property
def on_generic_event_func(self) -> Union[Callable[[int, int, int, int], None], Callable[[Event], None]]:
def on_generic_event_func(
self,
) -> Union[Callable[[int, int, int, int], None], Callable[[Event], None]]:
"""
callable: Get or set the on_generic_event callable
used to funnel all non-processed events
Expand Down Expand Up @@ -1284,7 +1304,9 @@ def load_texture_2d(
**kwargs,
)
)
assert isinstance(texture, moderngl.Texture), f"There was an error when loading the texture, {type(texture)} is not a moderngl.Texture"
assert isinstance(
texture, moderngl.Texture
), f"There was an error when loading the texture, {type(texture)} is not a moderngl.Texture"
return texture

def load_texture_array(
Expand Down Expand Up @@ -1335,7 +1357,9 @@ def load_texture_array(
**kwargs,
)
)
assert isinstance(texture, moderngl.TextureArray), f"There was an error when loading the texture, {type(texture)} is not a moderngl.TextureArray"
assert isinstance(
texture, moderngl.TextureArray
), f"Error when loading the texture, {type(texture)} is not a moderngl.TextureArray"
return texture

def load_texture_cube(
Expand All @@ -1346,7 +1370,7 @@ def load_texture_cube(
neg_x: str = "",
neg_y: str = "",
neg_z: str = "",
flip : bool = False,
flip: bool = False,
flip_x: bool = False,
flip_y: bool = False,
mipmap: bool = False,
Expand Down Expand Up @@ -1398,7 +1422,9 @@ def load_texture_cube(
)
)

assert isinstance(texture, moderngl.TextureCube), f"There was an error when loading the texture, {type(texture)} is not a moderngl.TextureCube"
assert isinstance(
texture, moderngl.TextureCube
), f"Error when loading the texture. {type(texture)} is not a moderngl.TextureCube"
return texture

def load_program(
Expand Down Expand Up @@ -1447,7 +1473,9 @@ def load_program(
)
)

assert isinstance(prog, moderngl.Program), f"There was an error when loading the program, {type(prog)} is not a moderngl.Program"
assert isinstance(
prog, moderngl.Program
), f"There was an error when loading the program, {type(prog)} is not a moderngl.Program"
return prog

def load_compute_shader(
Expand All @@ -1466,7 +1494,9 @@ def load_compute_shader(
ProgramDescription(compute_shader=path, defines=defines, **kwargs)
)

assert isinstance(shader, moderngl.ComputeShader), f"There was an error when loading the shader, {type(shader)} is not a moderngl.ComputeShader"
assert isinstance(
shader, moderngl.ComputeShader
), f"Error loading compute shader. {type(shader)} is not a moderngl.ComputeShader"
return shader

def load_text(self, path: str, **kwargs: Any) -> str:
Expand All @@ -1490,7 +1520,9 @@ def load_text(self, path: str, **kwargs: Any) -> str:

text = resources.data.load(DataDescription(path=path, **kwargs))

assert isinstance(text, str), f"There was an error when loading the text, {type(text)} is not a string"
assert isinstance(
text, str
), f"There was an error when loading the text, {type(text)} is not a string"
return text

def load_json(self, path: str, **kwargs: Any) -> dict[str, Any]:
Expand All @@ -1514,7 +1546,9 @@ def load_json(self, path: str, **kwargs: Any) -> dict[str, Any]:

json = resources.data.load(DataDescription(path=path, **kwargs))

assert isinstance(json, dict), f"There was an error when loading the Texture, {type(json)} is not a dictionnary"
assert isinstance(
json, dict
), f"There was an error when loading the Texture, {type(json)} is not a dictionnary"
return json

def load_binary(self, path: str, **kwargs: Any) -> bytes:
Expand All @@ -1538,11 +1572,18 @@ def load_binary(self, path: str, **kwargs: Any) -> bytes:

binary = resources.data.load(DataDescription(path=path, kind="binary"))

assert isinstance(binary, bytes), f"There was an error when loading the binary, {type(binary)} is not a binary file"
assert isinstance(
binary, bytes
), f"There was an error when loading the binary, {type(binary)} is not a binary file"
return binary

def load_scene(
self, path: str, cache: bool = False, attr_names: type[AttributeNames] = AttributeNames, kind: Optional[str] = None, **kwargs: Any
self,
path: str,
cache: bool = False,
attr_names: type[AttributeNames] = AttributeNames,
kind: Optional[str] = None,
**kwargs: Any,
) -> Scene:
"""Loads a scene.
Expand All @@ -1569,7 +1610,9 @@ def load_scene(
)
)

assert isinstance(scene, Scene), f"There was an error when loading the scene, {type(scene)} is not a Scene"
assert isinstance(
scene, Scene
), f"There was an error when loading the scene, {type(scene)} is not a Scene"
return scene


Expand Down
8 changes: 6 additions & 2 deletions moderngl_window/context/glfw/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ def _set_icon(self, icon_path: Path) -> None:
image = Image.open(icon_path)
glfw.set_window_icon(self._window, 1, image)

def glfw_key_event_callback(self, window: Any, key: GLFW_key, scancode: int, action: GLFW_key, mods: GLFW_key) -> None:
def glfw_key_event_callback(
self, window: Any, key: GLFW_key, scancode: int, action: GLFW_key, mods: GLFW_key
) -> None:
"""Key event callback for glfw.
Translates and forwards keyboard event to :py:func:`keyboard_event`
Expand Down Expand Up @@ -311,7 +313,9 @@ def glfw_mouse_event_callback(self, window: Any, xpos: float, ypos: float) -> No
else:
self._mouse_position_event_func(xpos, ypos, dx, dy)

def glfw_mouse_button_callback(self, window: Any, button: GLFW_key, action: GLFW_key, mods: GLFW_key) -> None:
def glfw_mouse_button_callback(
self, window: Any, button: GLFW_key, action: GLFW_key, mods: GLFW_key
) -> None:
"""Handle mouse button events and forward them to the example
Args:
Expand Down
10 changes: 9 additions & 1 deletion moderngl_window/context/headless/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ def use(self) -> None:
assert self._fbo is not None, "No framebuffer defined, did you forget to call create_fbo()?"
self._fbo.use()

def clear(self, red: float = 0.0, green: float = 0.0, blue: float = 0.0, alpha: float = 0.0, depth: float = 1.0, viewport: Optional[tuple[int, int, int, int]] = None) -> None:
def clear(
self,
red: float = 0.0,
green: float = 0.0,
blue: float = 0.0,
alpha: float = 0.0,
depth: float = 1.0,
viewport: Optional[tuple[int, int, int, int]] = None,
) -> None:
"""
Binds and clears the default framebuffer
Expand Down
5 changes: 2 additions & 3 deletions moderngl_window/context/pyglet/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
pyglet.options["shadow_window"] = False

pyglet.options["debug_gl"] = False

from pathlib import Path
from typing import Any, Union
from pathlib import Path # noqa
from typing import Any, Union # noqa

from moderngl_window.context.base import BaseWindow # noqa: E402
from moderngl_window.context.pyglet.keys import Keys # noqa: E402
Expand Down
4 changes: 2 additions & 2 deletions moderngl_window/finders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from collections import namedtuple
from pathlib import Path
from typing import Any, Optional
from typing import Optional

from moderngl_window.conf import settings
from moderngl_window.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -72,7 +72,7 @@ def find(self, path: Path) -> Optional[Path]:

if abspath.exists():
logger.debug("found %s", abspath)
return Path(abspath) # Needed to please mypy, but is already be a path
return Path(abspath) # Needed to please mypy, but is already be a path

return None

Expand Down
6 changes: 5 additions & 1 deletion moderngl_window/geometry/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from moderngl_window.opengl.vao import VAO


def bbox(size: tuple[float, float, float] = (1.0, 1.0, 1.0), name: Optional[str] = None, attr_names: type[AttributeNames] = AttributeNames) -> VAO:
def bbox(
size: tuple[float, float, float] = (1.0, 1.0, 1.0),
name: Optional[str] = None,
attr_names: type[AttributeNames] = AttributeNames,
) -> VAO:
"""
Generates a bounding box with (0.0, 0.0, 0.0) as the center.
This is simply a box with ``LINE_STRIP`` as draw mode.
Expand Down
7 changes: 6 additions & 1 deletion moderngl_window/geometry/quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from moderngl_window.opengl.vao import VAO


def quad_fs(attr_names: type[AttributeNames] = AttributeNames, normals: bool = True, uvs: bool = True, name: Optional[str] = None) -> VAO:
def quad_fs(
attr_names: type[AttributeNames] = AttributeNames,
normals: bool = True,
uvs: bool = True,
name: Optional[str] = None,
) -> VAO:
"""
Creates a screen aligned quad using two triangles with normals and texture coordinates.
Expand Down
Loading

0 comments on commit d2c689d

Please sign in to comment.