Skip to content

Commit b3d0fd0

Browse files
authored
Additional doc clarifications and formatting improvements
This adds extra detail to some docstrings, and brings formatting in line with Sphinx conventions.
1 parent 3fae659 commit b3d0fd0

File tree

12 files changed

+145
-92
lines changed

12 files changed

+145
-92
lines changed

docs/source/api.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ Core Package
77

88
.. automodule:: mss
99

10-
Factory Helpers
11-
===============
12-
13-
.. automodule:: mss.factory
14-
1510
Screenshot Objects
1611
==================
1712

docs/source/developers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You will need `pytest <https://pypi.org/project/pytest/>`_::
2929
How to Test?
3030
------------
3131

32-
Launch the test suit::
32+
Launch the test suite::
3333

3434
$ python -m pytest
3535

src/mss/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
class MSSBase(metaclass=ABCMeta):
4646
"""This class will be overloaded by a system specific one."""
4747

48-
__slots__ = {"_monitors", "cls_image", "compression_level", "with_cursor", "_closed"}
48+
__slots__ = {"_closed", "_monitors", "cls_image", "compression_level", "with_cursor"}
4949

5050
def __init__(
5151
self,

src/mss/factory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ def mss(**kwargs: Any) -> MSSBase:
1717
1818
It then proxies its arguments to the class for
1919
instantiation.
20+
21+
.. seealso::
22+
- :class:`mss.darwin.MSS`
23+
- :class:`mss.linux.MSS`
24+
- :class:`mss.windows.MSS`
25+
- :func:`mss.linux.mss`
26+
- :class:`mss.linux.xshmgetimage.MSS`
27+
- :class:`mss.linux.xgetimage.MSS`
28+
- :class:`mss.linux.xlib.MSS`
2029
"""
2130
os_ = platform.system().lower()
2231

src/mss/linux/__init__.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
"""GNU/Linux backend dispatcher.
2-
3-
This module picks the appropriate X11 backend implementation based on the
4-
``backend`` option. Available values:
5-
6-
- ``"default"`` or ``"xshmgetimage"``: XCB-based backend using XShmGetImage
7-
with automatic fallback to XGetImage when MIT-SHM is unavailable (default)
8-
- ``"xgetimage"``: XCB-based backend using XGetImage
9-
- ``"xlib"``: legacy Xlib-based backend retained for environments without
10-
working XCB libraries
11-
12-
Keyword arguments are forwarded to the selected backend. The ``display``
13-
argument (e.g., ``":0.0"``) targets a specific X server when needed.
14-
15-
.. versionadded:: 10.2.0
16-
The ``backend`` selector and the upper-case :func:`MSS` alias.
17-
18-
The top-level :func:`mss` function proxies keyword arguments to the selected
19-
backend class and returns an :class:`mss.base.MSSBase` implementation.
20-
"""
1+
"""GNU/Linux backend dispatcher for X11 screenshot implementations."""
212

223
from typing import Any
234

@@ -28,15 +9,32 @@
289

2910

3011
def mss(backend: str = "default", **kwargs: Any) -> MSSBase:
31-
"""Return a backend-specific MSS implementation.
12+
"""Return a backend-specific MSS implementation for GNU/Linux.
13+
14+
Selects and instantiates the appropriate X11 backend based on the
15+
``backend`` parameter. Keyword arguments are forwarded to the selected
16+
backend class.
3217
33-
The ``backend`` flag selects the implementation:
18+
:param backend: Backend selector. Valid values:
3419
35-
- ``"default"``/``"xshmgetimage"`` (default): XCB backend using
36-
XShmGetImage with automatic fallback to XGetImage
37-
- ``"xgetimage"``: XCB backend using XGetImage
38-
- ``"xlib"``: traditional Xlib backend retained for environments without
39-
working XCB libraries
20+
- ``"default"`` or ``"xshmgetimage"`` (default): XCB-based backend
21+
using XShmGetImage with automatic fallback to XGetImage when MIT-SHM
22+
is unavailable
23+
- ``"xgetimage"``: XCB-based backend using XGetImage
24+
- ``"xlib"``: Legacy Xlib-based backend retained for environments
25+
without working XCB libraries
26+
27+
.. versionadded:: 10.2.0 Prior to this version, the
28+
:class:`mss.linux.xlib.MSS` implementation was the only available
29+
backend.
30+
31+
:param kwargs: Keyword arguments forwarded to the backend. Common options
32+
include ``display`` (e.g., ``":0.0"``) to target a specific X server.
33+
:returns: An MSS backend implementation.
34+
35+
.. versionadded:: 10.2.0 Prior to this version, this didn't exist:
36+
the :func:`mss.linux.MSS` was a class equivalent to the current
37+
:class:`mss.linux.xlib.MSS` implementation.
4038
"""
4139
backend = backend.lower()
4240
if backend == "xlib":
@@ -61,4 +59,9 @@ def mss(backend: str = "default", **kwargs: Any) -> MSSBase:
6159

6260
# Alias in upper-case for backward compatibility. This is a supported name in the docs.
6361
def MSS(*args, **kwargs) -> MSSBase: # type: ignore[no-untyped-def] # noqa: N802, ANN002, ANN003
62+
"""Alias for :func:`mss.linux.mss.mss` for backward compatibility.
63+
64+
.. versionchanged:: 10.2.0 Prior to this version, this was a class.
65+
.. deprecated:: 10.2.0 Use :func:`mss.linux.mss` instead.
66+
"""
6467
return mss(*args, **kwargs)

src/mss/linux/base.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@
2323
class MSSXCBBase(MSSBase):
2424
"""Base class for XCB-based screenshot implementations.
2525
26-
This class provides common XCB initialization and monitor detection logic
27-
that can be shared across different XCB screenshot methods (XGetImage,
28-
XShmGetImage, XComposite, etc.).
26+
Provides common XCB initialization and monitor detection logic that can be
27+
shared across different XCB screenshot methods (``XGetImage``,
28+
``XShmGetImage``, ``XComposite``, etc.).
2929
"""
3030

3131
def __init__(self, /, **kwargs: Any) -> None: # noqa: PLR0912
3232
"""Initialize an XCB connection and validate the display configuration.
3333
34-
Args:
35-
**kwargs: Keyword arguments, including optional 'display' for X11 display string.
36-
37-
Raises:
38-
ScreenShotError: If the display configuration is not supported.
34+
:param kwargs: Optional keyword arguments. Recognized key ``display``
35+
specifies an X11 display string (bytes) to connect to.
3936
"""
4037
super().__init__(**kwargs)
4138

@@ -127,7 +124,12 @@ def _close_impl(self) -> None:
127124
self.conn = None
128125

129126
def _monitors_impl(self) -> None:
130-
"""Get positions of monitors. It will populate self._monitors."""
127+
"""Populate monitor geometry information.
128+
129+
Detects and appends monitor rectangles to ``self._monitors``. The first
130+
entry represents the entire X11 root screen; subsequent entries, when
131+
available, represent individual monitors reported by XRandR.
132+
"""
131133
if self.conn is None:
132134
msg = "Cannot identify monitors while the connection is closed"
133135
raise ScreenShotError(msg)
@@ -187,6 +189,11 @@ def _monitors_impl(self) -> None:
187189
# style is.
188190

189191
def _cursor_impl_check_xfixes(self) -> bool:
192+
"""Check XFixes availability and version.
193+
194+
:returns: ``True`` if the server provides XFixes with a compatible
195+
version, otherwise ``False``.
196+
"""
190197
if self.conn is None:
191198
msg = "Cannot take screenshot while the connection is closed"
192199
raise ScreenShotError(msg)
@@ -201,7 +208,12 @@ def _cursor_impl_check_xfixes(self) -> bool:
201208
return (reply.major_version, reply.minor_version) >= (2, 0)
202209

203210
def _cursor_impl(self) -> ScreenShot:
204-
"""Retrieve all cursor data. Pixels have to be RGBx."""
211+
"""Capture the current cursor image.
212+
213+
Pixels are returned in BGRA ordering.
214+
215+
:returns: A screenshot object containing the cursor image and region.
216+
"""
205217

206218
if self.conn is None:
207219
msg = "Cannot take screenshot while the connection is closed"
@@ -229,10 +241,14 @@ def _cursor_impl(self) -> ScreenShot:
229241
return self.cls_image(data, region)
230242

231243
def _grab_impl_xgetimage(self, monitor: Monitor, /) -> ScreenShot:
232-
"""Retrieve all pixels from a monitor using GetImage.
244+
"""Retrieve pixels from a monitor using ``GetImage``.
245+
246+
Used by the XGetImage backend and by the XShmGetImage backend in
247+
fallback mode.
233248
234-
This is used by the XGetImage backend, and also the XShmGetImage
235-
backend in fallback mode.
249+
:param monitor: Monitor rectangle specifying ``left``, ``top``,
250+
``width``, and ``height`` to capture.
251+
:returns: A screenshot object containing the captured region.
236252
"""
237253

238254
if self.conn is None:

src/mss/linux/xgetimage.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
33
This backend issues XCB ``GetImage`` requests and supports the RandR and
44
XFixes extensions when available for monitor enumeration and cursor capture.
5+
6+
This backend will work on any X connection, but is slower than the xshmgetimage
7+
backend.
8+
9+
.. versionadded:: 10.2.0
510
"""
611

712
from mss.models import Monitor
@@ -11,11 +16,7 @@
1116

1217

1318
class MSS(MSSXCBBase):
14-
"""XCB backend using XGetImage requests on GNU/Linux.
15-
16-
Uses RandR (for monitor enumeration) and XFixes (for cursor capture) when
17-
available.
18-
"""
19+
"""XCB backend using XGetImage requests on GNU/Linux."""
1920

2021
def _grab_impl(self, monitor: Monitor) -> ScreenShot:
2122
"""Retrieve all pixels from a monitor. Pixels have to be RGBX."""

src/mss/linux/xlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This backend talks to X11 via Xlib and the Xrandr extension, and is retained
44
as a fallback when XCB backends are unavailable. Cursor capture uses XFixes
55
when available.
6+
7+
.. versionadded:: 10.2.0 Prior to this version, this was available as
8+
``mss.linux.MSS``.
69
"""
710

811
from __future__ import annotations
@@ -415,15 +418,18 @@ def __init__(self, /, **kwargs: Any) -> None:
415418
if not _X11:
416419
msg = "No X11 library found."
417420
raise ScreenShotError(msg)
421+
#: :meta private:
418422
self.xlib = cdll.LoadLibrary(_X11)
419423

420424
if not _XRANDR:
421425
msg = "No Xrandr extension found."
422426
raise ScreenShotError(msg)
427+
#: :meta private:
423428
self.xrandr = cdll.LoadLibrary(_XRANDR)
424429

425430
if self.with_cursor:
426431
if _XFIXES:
432+
#: :meta private:
427433
self.xfixes = cdll.LoadLibrary(_XFIXES)
428434
else:
429435
self.with_cursor = False

src/mss/linux/xshmgetimage.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""XCB backend using MIT-SHM XShmGetImage with automatic fallback.
22
3+
This is the fastest Linux backend available, and will work in most common
4+
cases. However, it will not work over remote X connections, such as over ssh.
5+
36
This implementation prefers shared-memory captures for performance and will
47
fall back to XGetImage when the MIT-SHM extension is unavailable or fails at
58
runtime. The fallback reason is exposed via ``shm_fallback_reason`` to aid
69
debugging.
10+
11+
.. versionadded:: 10.2.0
712
"""
813

914
from __future__ import annotations
@@ -33,12 +38,7 @@ class ShmStatus(enum.Enum):
3338

3439

3540
class MSS(MSSXCBBase):
36-
"""XCB backend using XShmGetImage with an automatic XGetImage fallback.
37-
38-
The ``shm_status`` attribute tracks whether shared memory is available,
39-
and ``shm_fallback_reason`` records why a fallback occurred when MIT-SHM
40-
cannot be used.
41-
"""
41+
"""XCB backend using XShmGetImage with an automatic XGetImage fallback."""
4242

4343
def __init__(self, /, **kwargs: Any) -> None:
4444
super().__init__(**kwargs)
@@ -52,7 +52,11 @@ def __init__(self, /, **kwargs: Any) -> None:
5252
# isn't available. The factory in linux/__init__.py could then catch that and switch to XGetImage.
5353
# The conditions under which the attach will succeed but the xcb_shm_get_image will fail are extremely
5454
# rare, and I haven't yet found any that also will work with xcb_get_image.
55+
#: Whether we can use the MIT-SHM extensions for this connection.
56+
#: This will not be ``AVAILABLE`` until at least one capture has succeeded.
57+
#: It may be set to ``UNAVAILABLE`` sooner.
5558
self.shm_status: ShmStatus = self._setup_shm()
59+
#: If MIT-SHM is unavailable, the reason why (for debugging purposes).
5660
self.shm_fallback_reason: str | None = None
5761

5862
def _shm_report_issue(self, msg: str, *args: Any) -> None:

src/mss/models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
"""This is part of the MSS Python's module.
2-
Source: https://github.com/BoboTiG/python-mss.
3-
"""
1+
# This is part of the MSS Python's module.
2+
# Source: https://github.com/BoboTiG/python-mss.
43

54
from typing import Any, NamedTuple
65

0 commit comments

Comments
 (0)