Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d01cb0

Browse files
Mickaël SchoentgenBoboTiG
authored andcommitted
MSS: renamed again MSSMixin to MSSBase, now derived from abc.ABCMeta
- `MSSMixin` was really a base class, not a mixin. - Correctly implemented abstract attributes.
1 parent a3056ea commit 9d01cb0

File tree

10 files changed

+52
-56
lines changed

10 files changed

+52
-56
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ History:
44

55
5.0.1 2020/xx/xx
66
- produce wheels for Python 3 only
7+
- MSS: renamed again MSSMixin to MSSBase, now derived from abc.ABCMeta
78
- tools: force write of file when saving a PNG file
89
- tests: fix tests on macOS with Retina display
910

CHANGES.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
5.0.0 (2019-xx-xx)
1+
5.0.1 (2020-xx-xx)
2+
==================
3+
4+
base.py
5+
-------
6+
- Renamed back ``MSSMixin`` class to ``MSSBase``
7+
- ``MSSBase`` is now derived from ``abc.ABCMeta``
8+
- ``MSSBase.monitor`` is now an abstract property
9+
- ``MSSBase.grab()`` is now an abstract method
10+
11+
12+
5.0.0 (2019-12-31)
213
==================
314

415
darwin.py

docs/source/api.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ GNU/Linux
5959
:rtype: :class:`~mss.base.ScreenShot`
6060
:raises ScreenShotError: When color depth is not 32 (rare).
6161

62-
See :meth:`~mss.base.MSSMixin.grab()` for details.
62+
See :meth:`~mss.base.MSSBase.grab()` for details.
6363

6464
.. function:: error_handler(display, event)
6565

@@ -82,7 +82,7 @@ Methods
8282

8383
.. module:: mss.base
8484

85-
.. class:: MSSMixin
85+
.. class:: MSSBase
8686

8787
The parent's class for every OS implementation.
8888

@@ -96,9 +96,9 @@ Methods
9696

9797
:param dict monitor: region's coordinates.
9898
:rtype: :class:`ScreenShot`
99-
:raises NotImplementedError: Subclasses need to implement this.
10099

101100
Retrieve screen pixels for a given *region*.
101+
Subclasses need to implement this.
102102

103103
.. note::
104104

@@ -194,7 +194,7 @@ Methods
194194
Properties
195195
==========
196196

197-
.. class:: mss.base.MSSMixin
197+
.. class:: mss.base.MSSBase
198198

199199
.. attribute:: monitors
200200

@@ -215,6 +215,8 @@ Properties
215215
- ``width``: the width
216216
- ``height``: the height
217217

218+
Subclasses need to implement this.
219+
218220
:rtype: list[dict[str, int]]
219221

220222
.. class:: mss.base.ScreenShot

mss/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Source: https://github.com/BoboTiG/python-mss
44
"""
55

6+
from abc import ABCMeta, abstractmethod
67
from datetime import datetime
78
from typing import TYPE_CHECKING
89

@@ -16,7 +17,7 @@
1617
from .models import Monitor, Monitors # noqa
1718

1819

19-
class MSSMixin:
20+
class MSSBase(metaclass=ABCMeta):
2021
""" This class will be overloaded by a system specific one. """
2122

2223
__slots__ = {"_monitors", "cls_image", "compression_level"}
@@ -27,7 +28,7 @@ def __init__(self):
2728
self._monitors = [] # type: Monitors
2829

2930
def __enter__(self):
30-
# type: () -> MSSMixin
31+
# type: () -> MSSBase
3132
""" For the cool call `with MSS() as mss:`. """
3233

3334
return self
@@ -41,6 +42,7 @@ def close(self):
4142
# type: () -> None
4243
""" Clean-up. """
4344

45+
@abstractmethod
4446
def grab(self, monitor):
4547
# type: (Monitor) -> ScreenShot
4648
"""
@@ -51,9 +53,8 @@ def grab(self, monitor):
5153
:return :class:`ScreenShot <ScreenShot>`.
5254
"""
5355

54-
raise NotImplementedError("Subclasses need to implement this!")
55-
5656
@property
57+
@abstractmethod
5758
def monitors(self):
5859
# type: () -> Monitors
5960
"""

mss/darwin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
from typing import TYPE_CHECKING
1010

11-
from .base import MSSMixin
11+
from .base import MSSBase
1212
from .exception import ScreenShotError
1313
from .screenshot import Size
1414

@@ -57,7 +57,7 @@ def __repr__(self):
5757
return "{}<{} {}>".format(type(self).__name__, self.origin, self.size)
5858

5959

60-
class MSS(MSSMixin):
60+
class MSS(MSSBase):
6161
"""
6262
Multiple ScreenShots implementation for macOS.
6363
It uses intensively the CoreGraphics library.
@@ -175,7 +175,7 @@ def monitors(self):
175175
def grab(self, monitor):
176176
# type: (Monitor) -> ScreenShot
177177
"""
178-
See :meth:`MSSMixin.grab <mss.base.MSSMixin.grab>` for full details.
178+
See :meth:`MSSBase.grab <mss.base.MSSBase.grab>` for full details.
179179
"""
180180

181181
# pylint: disable=too-many-locals

mss/factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
if TYPE_CHECKING:
1313
from typing import Any # noqa
1414

15-
from .base import MSSMixin # noqa
15+
from .base import MSSBase # noqa
1616

1717

1818
def mss(**kwargs):
19-
# type: (Any) -> MSSMixin
19+
# type: (Any) -> MSSBase
2020
""" Factory returning a proper MSS class instance.
2121
2222
It detects the plateform we are running on

mss/linux.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from types import SimpleNamespace
1010
from typing import TYPE_CHECKING
1111

12-
from .base import MSSMixin
12+
from .base import MSSBase
1313
from .exception import ScreenShotError
1414

1515
if TYPE_CHECKING:
@@ -175,7 +175,7 @@ def validate(retval, func, args):
175175
raise ScreenShotError(err, details=details)
176176

177177

178-
class MSS(MSSMixin):
178+
class MSS(MSSBase):
179179
"""
180180
Multiple ScreenShots implementation for GNU/Linux.
181181
It uses intensively the Xlib and its Xrandr extension.

mss/tests/test_gnu_linux.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import mss
1111
import pytest
12-
from mss.base import MSSMixin
12+
from mss.base import MSSBase
1313
from mss.exception import ScreenShotError
1414

1515

@@ -32,7 +32,7 @@ def test_factory_systems(monkeypatch):
3232
# GNU/Linux
3333
monkeypatch.setattr(platform, "system", lambda: "LINUX")
3434
with mss.mss() as sct:
35-
assert isinstance(sct, MSSMixin)
35+
assert isinstance(sct, MSSBase)
3636
monkeypatch.undo()
3737

3838
# macOS

mss/tests/test_implementation.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,43 @@
66
import os
77
import os.path
88
import platform
9-
import sys
109

10+
import pytest
1111
import mss
1212
import mss.tools
13-
from mss.base import MSSMixin
13+
from mss.base import MSSBase
1414
from mss.exception import ScreenShotError
1515
from mss.screenshot import ScreenShot
1616

17-
import pytest
18-
1917

20-
PY3 = sys.version[0] > "2"
21-
22-
23-
class MSS0(MSSMixin):
18+
class MSS0(MSSBase):
2419
""" Nothing implemented. """
2520

2621
pass
2722

2823

29-
class MSS1(MSSMixin):
30-
""" Emulate no monitors. """
24+
class MSS1(MSSBase):
25+
""" Only `grab()` implemented. """
3126

32-
@property
33-
def monitors(self):
34-
return []
27+
def grab(self, monitor):
28+
pass
3529

3630

37-
class MSS2(MSSMixin):
38-
""" Emulate one monitor. """
31+
class MSS2(MSSBase):
32+
""" Only `monitor` implemented. """
3933

4034
@property
4135
def monitors(self):
42-
return [{"top": 0, "left": 0, "width": 10, "height": 10}]
43-
36+
return []
4437

45-
def test_incomplete_class():
46-
# `monitors` property not implemented
47-
with pytest.raises(NotImplementedError):
48-
for filename in MSS0().save():
49-
assert os.path.isfile(filename)
5038

51-
# `monitors` property is empty
52-
with pytest.raises(ScreenShotError):
53-
for filename in MSS1().save():
54-
assert os.path.isfile(filename)
39+
@pytest.mark.parametrize("cls", [MSS0, MSS1, MSS2])
40+
def test_incomplete_class(cls):
41+
with pytest.raises(TypeError):
42+
cls()
5543

56-
# `grab()` not implemented
57-
sct = MSS2()
58-
with pytest.raises(NotImplementedError):
59-
sct.grab(sct.monitors[0])
6044

61-
# Bad monitor
45+
def test_bad_monitor(sct):
6246
with pytest.raises(ScreenShotError):
6347
sct.grab(sct.shot(mon=222))
6448

@@ -79,18 +63,15 @@ def test_repr(sct, pixel_ratio):
7963
def test_factory(monkeypatch):
8064
# Current system
8165
with mss.mss() as sct:
82-
assert isinstance(sct, MSSMixin)
66+
assert isinstance(sct, MSSBase)
8367

8468
# Unknown
8569
monkeypatch.setattr(platform, "system", lambda: "Chuck Norris")
8670
with pytest.raises(ScreenShotError) as exc:
8771
mss.mss()
8872
monkeypatch.undo()
8973

90-
if not PY3:
91-
error = exc.value[0]
92-
else:
93-
error = exc.value.args[0]
74+
error = exc.value.args[0]
9475
assert error == "System 'chuck norris' not (yet?) implemented."
9576

9677

mss/windows.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from typing import TYPE_CHECKING
2424

25-
from .base import MSSMixin
25+
from .base import MSSBase
2626
from .exception import ScreenShotError
2727

2828
if TYPE_CHECKING:
@@ -65,7 +65,7 @@ class BITMAPINFO(ctypes.Structure):
6565
_fields_ = [("bmiHeader", BITMAPINFOHEADER), ("bmiColors", DWORD * 3)]
6666

6767

68-
class MSS(MSSMixin):
68+
class MSS(MSSBase):
6969
""" Multiple ScreenShots implementation for Microsoft Windows. """
7070

7171
__slots__ = {"_bbox", "_bmi", "_data", "gdi32", "monitorenumproc", "user32"}

0 commit comments

Comments
 (0)