more linting

This commit is contained in:
fdev31 2023-07-30 19:12:21 +02:00
parent b219b02081
commit a0f3ca2a83
8 changed files with 50 additions and 34 deletions

View file

@ -1,6 +1,7 @@
#!/bin/env python #!/bin/env python
" Pyprland - an Hyprland companion app " " Pyprland - an Hyprland companion app "
import asyncio import asyncio
from typing import cast
import json import json
import sys import sys
import os import os
@ -23,7 +24,7 @@ class Pyprland:
event_reader: asyncio.StreamReader event_reader: asyncio.StreamReader
stopped = False stopped = False
name = "builtin" name = "builtin"
config: dict[str, dict] = None config: None | dict[str, dict] = None
def __init__(self): def __init__(self):
self.plugins: dict[str, Plugin] = {} self.plugins: dict[str, Plugin] = {}
@ -33,6 +34,7 @@ class Pyprland:
"""Loads the configuration """Loads the configuration
if `init` is true, also initializes the plugins""" if `init` is true, also initializes the plugins"""
assert isinstance(self.config, dict)
try: try:
with open(os.path.expanduser(CONFIG_FILE), encoding="utf-8") as f: with open(os.path.expanduser(CONFIG_FILE), encoding="utf-8") as f:
self.config = json.loads(f.read()) self.config = json.loads(f.read())
@ -42,7 +44,9 @@ class Pyprland:
) )
raise PyprError() from e raise PyprError() from e
for name in self.config["pyprland"]["plugins"]: assert self.config
for name in cast(dict, self.config["pyprland"]["plugins"]):
if name not in self.plugins: if name not in self.plugins:
modname = name if "." in name else f"pyprland.plugins.{name}" modname = name if "." in name else f"pyprland.plugins.{name}"
try: try:

View file

@ -1,22 +1,22 @@
""" expose Brings every client window to screen for selection """ expose Brings every client window to screen for selection
toggle_minimized allows having an "expose" like selection of minimized windows toggle_minimized allows having an "expose" like selection of minimized windows
""" """
from typing import Any from typing import Any, cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON, hyprctl from ..ipc import hyprctlJSON, hyprctl
class Extension(Plugin): # pylint: disable=missing-class-docstring class Extension(Plugin): # pylint: disable=missing-class-docstring
exposed = False exposed: list[dict] = []
async def run_toggle_minimized(self, special_workspace="minimized"): async def run_toggle_minimized(self, special_workspace="minimized"):
"""[name] Toggles switching the focused window to the special workspace "name" (default: minimized)""" """[name] Toggles switching the focused window to the special workspace "name" (default: minimized)"""
aw: dict[str, Any] = await hyprctlJSON("activewindow") aw = cast(dict, await hyprctlJSON("activewindow"))
wid = aw["workspace"]["id"] wid = aw["workspace"]["id"]
assert isinstance(wid, int) assert isinstance(wid, int)
if wid < 1: # special workspace: unminimize if wid < 1: # special workspace: unminimize
wrk = await hyprctlJSON("activeworkspace") wrk = cast(dict, await hyprctlJSON("activeworkspace"))
await hyprctl(f"togglespecialworkspace {special_workspace}") await hyprctl(f"togglespecialworkspace {special_workspace}")
await hyprctl(f"movetoworkspacesilent {wrk['id']},address:{aw['address']}") await hyprctl(f"movetoworkspacesilent {wrk['id']},address:{aw['address']}")
await hyprctl(f"focuswindow address:{aw['address']}") await hyprctl(f"focuswindow address:{aw['address']}")
@ -36,7 +36,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
"""Expose every client on the active workspace. """Expose every client on the active workspace.
If expose is active restores everything and move to the focused window""" If expose is active restores everything and move to the focused window"""
if self.exposed: if self.exposed:
aw: dict[str, Any] = await hyprctlJSON("activewindow") aw: dict[str, Any] = cast(dict, await hyprctlJSON("activewindow"))
focused_addr = aw["address"] focused_addr = aw["address"]
for client in self.exposed_clients: for client in self.exposed_clients:
await hyprctl( await hyprctl(
@ -44,9 +44,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
) )
await hyprctl("togglespecialworkspace exposed") await hyprctl("togglespecialworkspace exposed")
await hyprctl(f"focuswindow address:{focused_addr}") await hyprctl(f"focuswindow address:{focused_addr}")
self.exposed = False self.exposed = []
else: else:
self.exposed = await hyprctlJSON("clients") self.exposed = cast(list, await hyprctlJSON("clients"))
for client in self.exposed_clients: for client in self.exposed_clients:
await hyprctl( await hyprctl(
f"movetoworkspacesilent special:exposed,address:{client['address']}" f"movetoworkspacesilent special:exposed,address:{client['address']}"

View file

@ -1,5 +1,5 @@
" Moves unreachable client windows to the currently focused workspace" " Moves unreachable client windows to the currently focused workspace"
from typing import Any from typing import Any, cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON, hyprctl from ..ipc import hyprctlJSON, hyprctl
@ -23,8 +23,8 @@ def contains(monitor, window):
class Extension(Plugin): # pylint: disable=missing-class-docstring class Extension(Plugin): # pylint: disable=missing-class-docstring
async def run_attract_lost(self): async def run_attract_lost(self):
"""Brings lost floating windows to the current workspace""" """Brings lost floating windows to the current workspace"""
monitors: list[dict[str, Any]] = await hyprctlJSON("monitors") monitors = cast(list, await hyprctlJSON("monitors"))
windows = await hyprctlJSON("clients") windows = cast(list, await hyprctlJSON("clients"))
lost = [ lost = [
win win
for win in windows for win in windows

View file

@ -1,6 +1,6 @@
" The monitors plugin " " The monitors plugin "
import subprocess import subprocess
from typing import Any from typing import Any, cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON from ..ipc import hyprctlJSON
@ -40,7 +40,7 @@ def configure_monitors(monitors, screenid: str, pos_x: int, pos_y: int) -> None:
class Extension(Plugin): # pylint: disable=missing-class-docstring class Extension(Plugin): # pylint: disable=missing-class-docstring
async def load_config(self, config) -> None: async def load_config(self, config) -> None:
await super().load_config(config) await super().load_config(config)
monitors = await hyprctlJSON("monitors") monitors = cast(list[dict], await hyprctlJSON("monitors"))
for monitor in monitors: for monitor in monitors:
await self.event_monitoradded( await self.event_monitoradded(
monitor["name"], no_default=True, monitors=monitors monitor["name"], no_default=True, monitors=monitors
@ -53,7 +53,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
monitor_name = monitor_name.strip() monitor_name = monitor_name.strip()
if not monitors: if not monitors:
monitors = await hyprctlJSON("monitors") monitors = cast(list, await hyprctlJSON("monitors"))
assert monitors assert monitors
@ -85,18 +85,20 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
ref = mon_by_name[other_mon_description] ref = mon_by_name[other_mon_description]
if ref: if ref:
place = placement.lower() place = placement.lower()
x: int = 0
y: int = 0
if place == "topof": if place == "topof":
x: int = ref["x"] x = ref["x"]
y: int = ref["y"] - newmon["height"] y = ref["y"] - newmon["height"]
elif place == "bottomof": elif place == "bottomof":
x: int = ref["x"] x = ref["x"]
y: int = ref["y"] + ref["height"] y = ref["y"] + ref["height"]
elif place == "leftof": elif place == "leftof":
x: int = ref["x"] - newmon["width"] x = ref["x"] - newmon["width"]
y: int = ref["y"] y = ref["y"]
else: # rightof else: # rightof
x: int = ref["x"] + ref["width"] x = ref["x"] + ref["width"]
y: int = ref["y"] y = ref["y"]
configure_monitors(monitors, monitor_name, x, y) configure_monitors(monitors, monitor_name, x, y)
return True return True

View file

@ -2,7 +2,7 @@
import os import os
import asyncio import asyncio
import subprocess import subprocess
from typing import Any from typing import Any, cast
from ..ipc import ( from ..ipc import (
hyprctl, hyprctl,
@ -158,10 +158,10 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
*(die_in_piece(scratch) for scratch in self.scratches.values()) *(die_in_piece(scratch) for scratch in self.scratches.values())
) )
async def load_config(self, config) -> None: async def load_config(self, config: dict[str, Any]) -> None:
"config loader" "config loader"
config: dict[str, dict[str, Any]] = config["scratchpads"] my_config: dict[str, dict[str, Any]] = config["scratchpads"]
scratches = {k: Scratch(k, v) for k, v in config.items()} scratches = {k: Scratch(k, v) for k, v in my_config.items()}
new_scratches = set() new_scratches = set()
@ -347,7 +347,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
uid = uid.strip() uid = uid.strip()
item = self.scratches.get(uid) item = self.scratches.get(uid)
self.focused_window_tracking[uid] = await hyprctlJSON("activewindow") self.focused_window_tracking[uid] = cast(
dict[str, Any], await hyprctlJSON("activewindow")
)
if not item: if not item:
self.log.warning("%s is not configured", uid) self.log.warning("%s is not configured", uid)

View file

@ -1,4 +1,5 @@
" shift workspaces across monitors " " shift workspaces across monitors "
from typing import cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON, hyprctl from ..ipc import hyprctlJSON, hyprctl
@ -8,7 +9,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
monitors: list[str] = [] monitors: list[str] = []
async def init(self): async def init(self):
self.monitors = [mon["name"] for mon in await hyprctlJSON("monitors")] self.monitors: list[str] = [
mon["name"] for mon in cast(list[dict], await hyprctlJSON("monitors"))
]
async def run_shift_monitors(self, arg: str): async def run_shift_monitors(self, arg: str):
"""Swaps monitors' workspaces in the given direction""" """Swaps monitors' workspaces in the given direction"""
@ -16,7 +19,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
if direction > 0: if direction > 0:
mon_list = self.monitors[:-1] mon_list = self.monitors[:-1]
else: else:
mon_list = reversed(self.monitors[1:]) mon_list = list(reversed(self.monitors[1:]))
for i, mon in enumerate(mon_list): for i, mon in enumerate(mon_list):
await hyprctl(f"swapactiveworkspaces {mon} {self.monitors[i+direction]}") await hyprctl(f"swapactiveworkspaces {mon} {self.monitors[i+direction]}")

View file

@ -1,5 +1,5 @@
" Toggle monitors on or off " " Toggle monitors on or off "
from typing import Any from typing import Any, cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON, hyprctl from ..ipc import hyprctlJSON, hyprctl
@ -8,7 +8,7 @@ from ..ipc import hyprctlJSON, hyprctl
class Extension(Plugin): # pylint: disable=missing-class-docstring class Extension(Plugin): # pylint: disable=missing-class-docstring
async def run_toggle_dpms(self): async def run_toggle_dpms(self):
"""toggles dpms on/off for every monitor""" """toggles dpms on/off for every monitor"""
monitors: list[dict[str, Any]] = await hyprctlJSON("monitors") monitors = cast(list[dict[str, Any]], await hyprctlJSON("monitors"))
powered_off = any(m["dpmsStatus"] for m in monitors) powered_off = any(m["dpmsStatus"] for m in monitors)
if not powered_off: if not powered_off:
await hyprctl("dpms on") await hyprctl("dpms on")

View file

@ -1,4 +1,5 @@
""" Force workspaces to follow the focus / mouse """ """ Force workspaces to follow the focus / mouse """
from typing import cast
from .interface import Plugin from .interface import Plugin
from ..ipc import hyprctlJSON, hyprctl from ..ipc import hyprctlJSON, hyprctl
@ -19,10 +20,14 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring
# move every free workspace to the currently focused desktop # move every free workspace to the currently focused desktop
busy_workspaces = set( busy_workspaces = set(
mon["activeWorkspace"]["id"] mon["activeWorkspace"]["id"]
for mon in await hyprctlJSON("monitors") for mon in cast(list[dict], await hyprctlJSON("monitors"))
if mon["name"] != monitor_id if mon["name"] != monitor_id
) )
workspaces = [w["id"] for w in await hyprctlJSON("workspaces") if w["id"] > 0] workspaces = [
w["id"]
for w in cast(list[dict], await hyprctlJSON("workspaces"))
if w["id"] > 0
]
batch: list[str | list[str]] = [] batch: list[str | list[str]] = []
for n in workspaces: for n in workspaces: