Skip to content

Commit d7972f7

Browse files
pylint is now happy
1 parent edb1f4c commit d7972f7

File tree

4 files changed

+102
-42
lines changed

4 files changed

+102
-42
lines changed

display_server_interactions/linux.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/python3
22
# -*- coding: utf-8 -*-
33

4+
"""
5+
All X11 specific DSI functions
6+
"""
7+
48
# built-in modules
59
from typing import Optional
610
import logging
@@ -32,6 +36,8 @@
3236

3337
# Setup Xlib Structures
3438

39+
# pylint: disable=too-few-public-methods
40+
3541

3642
class Display(Structure):
3743
"""
@@ -168,6 +174,7 @@ class XErrorEvent(Structure):
168174
"""
169175

170176
def __repr__(self) -> str:
177+
# pylint: disable-next=line-too-long
171178
return f"XErrorEvent(type={self.type}, serial={self.serial}, error_code={self.error_code}, request_code={self.request_code}, minor_code={self.minor_code})"
172179

173180
_fields_ = [
@@ -187,6 +194,9 @@ def __repr__(self) -> str:
187194

188195
@ctypes.CFUNCTYPE(c_int, POINTER(Display), POINTER(XErrorEvent))
189196
def error_handler(_, event):
197+
"""
198+
A C function that handles X11 errors.
199+
"""
190200
logger.error("%s", event.contents)
191201
return 0
192202

@@ -294,6 +304,10 @@ class KeyMasks:
294304

295305

296306
class Xlib:
307+
"""
308+
A class that provides access to Xlib functions.
309+
"""
310+
297311
def __init__(self):
298312
# load libX11.so.6
299313
x11 = ctypes.util.find_library("X11")
@@ -374,7 +388,7 @@ def __getattribute__(self, __name: str):
374388
return self.xlib.__getattribute__(__name)
375389

376390

377-
def get_window_property(xlib: Xlib, window_xid: int, property: str, type: _SimpleCData):
391+
def get_window_property(xlib: Xlib, window_xid: int, property_name: str, return_type: _SimpleCData):
378392
"""
379393
https://tronche.com/gui/x/xlib/window-information/XGetWindowProperty.html
380394
"""
@@ -388,7 +402,7 @@ def get_window_property(xlib: Xlib, window_xid: int, property: str, type: _Simpl
388402
xlib.display,
389403
window_xid,
390404
xlib.XInternAtom(
391-
xlib.display, c_char_p(property.encode('utf-8')),
405+
xlib.display, c_char_p(property_name.encode('utf-8')),
392406
False
393407
),
394408
0,
@@ -405,7 +419,7 @@ def get_window_property(xlib: Xlib, window_xid: int, property: str, type: _Simpl
405419
if prop_return:
406420
data = cast(
407421
prop_return,
408-
POINTER(type)
422+
POINTER(return_type)
409423
).contents.value
410424
else:
411425
data = None
@@ -417,6 +431,10 @@ def get_window_property(xlib: Xlib, window_xid: int, property: str, type: _Simpl
417431

418432

419433
class Window(WindowBase):
434+
"""
435+
An class for interacting with a window on X11.
436+
"""
437+
420438
def __init__(self, xid: int, xlib: Xlib) -> None:
421439
self.xid = xid
422440
self.xlib = xlib
@@ -481,7 +499,7 @@ def get_image(self, geometry: Optional[Box] = None) -> Image:
481499

482500
return data
483501

484-
def send_chr(self, chr: chr) -> None:
502+
def send_chr(self, character: chr) -> None:
485503
"""Send a character to the window
486504
487505
Args:
@@ -492,10 +510,10 @@ def send_chr(self, chr: chr) -> None:
492510
key = XEvent(type=EventTypes.KeyPress).xkey # KeyPress
493511
key.keycode = self.xlib.XKeysymToKeycode(
494512
self.xlib.display,
495-
self.xlib.XStringToKeysym(c_char_p(chr.encode('utf-8')))
513+
self.xlib.XStringToKeysym(c_char_p(character.encode('utf-8')))
496514
) # https://github.com/python-xlib/python-xlib/blob/master/Xlib/keysymdef/latin1.py
497515
key.window = key.root = self.xid
498-
key.state = KeyMasks.ShiftMask if chr.isupper() else 0
516+
key.state = KeyMasks.ShiftMask if character.isupper() else 0
499517

500518
self.xlib.XSendEvent(
501519
self.xlib.display, # Display *display
@@ -508,14 +526,14 @@ def send_chr(self, chr: chr) -> None:
508526
# flush display or events will run delayed cus thai'r only called on the next update
509527
self.xlib.XFlush(self.xlib.display)
510528

511-
def send_str(self, str: str) -> None:
529+
def send_str(self, string: str) -> None:
512530
"""Send a string to the window
513531
514532
Args:
515533
str (str): The string to send
516534
"""
517-
for chr in str:
518-
self.send_chr(chr)
535+
for character in string:
536+
self.send_chr(character)
519537

520538
def warp_pointer(self, x: int, y: int, geometry: Optional[Box] = None) -> None:
521539
if geometry is None:
@@ -538,10 +556,12 @@ def warp_pointer(self, x: int, y: int, geometry: Optional[Box] = None) -> None:
538556
self.xlib.XFlush(self.xlib.display)
539557

540558
def send_mouse_click(self, x: int, y: int, button: MouseButtons = MouseButtons.LEFT) -> None:
559+
# pylint: disable=line-too-long
541560
"""
542561
Send a mouse click to the window at the given coordinates without moving the pointer.
543562
Some applications may not respond to the click so it is recommended to also move the pointer with `warp_pointer`.
544563
"""
564+
# pylint: enable=line-too-long
545565
event = XEvent(type=EventTypes.ButtonPress).xbutton
546566
event.window = event.root = self.xid
547567
event.button = button
@@ -622,20 +642,20 @@ def get_all_windows(xlib: Xlib) -> list:
622642
Get all window XIDs. By recursively getting all connected windows.
623643
"""
624644
final = get_connected_xids(xlib, xlib.root_window)
625-
next = final.copy()
645+
next_window = final.copy()
626646

627647
run = True
628648
while run:
629649
run = False
630650
next_temp = []
631-
for xid in next:
651+
for xid in next_window:
632652
xids = get_connected_xids(xlib, xid)
633653
if len(xids) > 0:
634654
run = True
635655
next_temp += xids
636656

637-
next = next_temp
638-
final += next
657+
next_window = next_temp
658+
final += next_window
639659

640660
final_windows = []
641661

@@ -646,6 +666,10 @@ def get_all_windows(xlib: Xlib) -> list:
646666

647667

648668
class DSI(DSIBase):
669+
"""
670+
Main DSI class
671+
"""
672+
649673
def __init__(self):
650674
self.xlib = Xlib()
651675

display_server_interactions/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
#!/usr/bin/python3
22
# -*- coding: utf-8 -*-
33

4+
"""
5+
This module provides a function to detect the current Linux display server.
6+
"""
7+
48
from os import getenv
59
from enum import Enum
610

711

812
class LinuxDisplayServer(Enum):
13+
"""
14+
An enumeration of Linux display servers.
15+
"""
916
WAYLAND = "wayland"
1017
X11 = "x11"
1118
UNKNOWN = "Unknown"
1219

1320

1421
def detect_linux_display_server() -> LinuxDisplayServer:
22+
"""
23+
Detects the current Linux display server.
24+
"""
1525
xdg_session_type = getenv("XDG_SESSION_TYPE")
1626
if xdg_session_type == "wayland":
1727
return LinuxDisplayServer.WAYLAND

display_server_interactions/window.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#!/usr/bin/python3
22
# -*- coding: utf-8 -*-
33

4+
"""
5+
This module provides a class for interacting with windows
6+
"""
7+
8+
# built-in modules
49
from abc import ABCMeta, abstractmethod
510
from typing import Optional
11+
12+
# local modules
613
from .image import Image
714
from .buttons import MouseButtons
815
from .box import Box
916

1017

1118
class WindowBase(metaclass=ABCMeta):
19+
"""
20+
An abstract base class that defines the interface for interacting with a window.
21+
"""
1222
@property
1323
@abstractmethod
1424
def name(self) -> str:
@@ -42,19 +52,21 @@ def geometry(self) -> Box:
4252

4353
@abstractmethod
4454
def get_image(self, geometry: Optional[Box] = None) -> Image:
55+
# pylint: disable=line-too-long
4556
"""
4657
Returns an Image of the window.
4758
With the geometry parameter you can specify a sub-region of the window that will be captured.
4859
"""
60+
# pylint: enable=line-too-long
4961

5062
@abstractmethod
51-
def send_chr(self, chr: chr) -> None:
63+
def send_chr(self, character: chr) -> None:
5264
"""
5365
send the keystroke of the given character to the window.
5466
"""
5567

5668
@abstractmethod
57-
def send_str(self, str: str) -> None:
69+
def send_str(self, string: str) -> None:
5870
"""
5971
Send keystrokes equivalent to the string you pass to the window.
6072
"""
@@ -75,7 +87,9 @@ def send_mouse_click(self, x: int, y: int, button: MouseButtons = MouseButtons.L
7587
"""
7688

7789
def __repr__(self) -> str:
90+
# pylint: disable=line-too-long
7891
name = self.name
7992
if name:
8093
return f'Window(name="{self.name}", pid={self.pid}, active={self.active}, geometry={self.geometry})'
8194
return f'Window(pid={self.pid}, active={self.active}, geometry={self.geometry})'
95+
# pylint: enable=line-too-long

0 commit comments

Comments
 (0)