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

Skip to content

Commit 048de3d

Browse files
authored
MSS: Add the 'mss' entry point
1 parent 0057532 commit 048de3d

File tree

7 files changed

+122
-23
lines changed

7 files changed

+122
-23
lines changed

CHANGELOG

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ dev 2017/xx/xx
66
- add the 'Say Thanks' button
77
- doc: several fixes (fix #22)
88
- tests: a lot of tests added for better coverage
9+
- MSS: add the 'mss' entry point
910
- MSS: add more way of customization to the output argument of save()
1011
- MSS: possibility to use custom class to handle screen shot data
11-
- Mac: handle screenshot's width not divisible by 16 (fix #14, #19, #21)
12-
- Mac: properly support all display scaling and resolutions (fix #23)
13-
- Mac: fix memory leak (fix #24)
12+
- Mac: properly support all display scaling and resolutions (fix #14, #19, #21, #23)
13+
- Mac: fix memory leaks (fix #24)
1414
- Linux: handle bad display value
1515
- Windows: Take into account zoom factor for high-DPI displays (fix #20)
1616

docs/source/usage.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ A more specific example to only target GNU/Linux:
4747

4848
.. literalinclude:: examples/linux_display_keyword.py
4949
:lines: 9-
50+
51+
52+
Command line
53+
============
54+
55+
Since 3.1.0, an entry point was added and so you can use ``mss`` via the CLI:
56+
57+
mss --help
58+
59+
Or via direct call from Python:
60+
61+
python -m mss --help

mss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
in supporting documentation or portions thereof, including
2626
modifications, that you make.
2727
"""
28-
__all__ = ['ScreenShotError', 'mss']
28+
__all__ = ('ScreenShotError', 'mss')

mss/__main__.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,70 @@
66

77
from __future__ import print_function
88

9+
import os.path
10+
import sys
11+
from argparse import ArgumentParser
12+
13+
from . import __version__
914
from .exception import ScreenShotError
1015
from .factory import mss
16+
from .tools import to_png
1117

1218

13-
def main():
14-
# type: () -> int
19+
def main(args=None):
20+
# type: (Optional[List[str]]) -> int
1521
""" Main logic. """
1622

23+
cli_args = ArgumentParser()
24+
cli_args.add_argument('-c', '--coordinates', default='', type=str,
25+
help='the part of the screen to capture:'
26+
' top, left, width, height')
27+
cli_args.add_argument('-m', '--monitor', default=0, type=int,
28+
help='the monitor to screen shot')
29+
cli_args.add_argument('-o', '--output', default='monitor-{mon}.png',
30+
help='the output file name')
31+
cli_args.add_argument('-q', '--quiet', default=False, action='store_true',
32+
help='do not print created files')
33+
cli_args.add_argument('-v', '--version', action='version',
34+
version=__version__)
35+
36+
options = cli_args.parse_args(args)
37+
kwargs = {
38+
'mon': options.monitor,
39+
'output': options.output,
40+
}
41+
if options.coordinates:
42+
try:
43+
top, left, width, height = map(int, options.coordinates.split(','))
44+
except ValueError:
45+
print('Coordinates syntax: top, left, width, height')
46+
return 2
47+
48+
kwargs['mon'] = {
49+
'top': top,
50+
'left': left,
51+
'width': width,
52+
'height': height,
53+
}
54+
if options.output == 'monitor-{mon}.png':
55+
kwargs['output'] = 'sct-{top}x{left}_{width}x{height}.png'
56+
1757
try:
1858
with mss() as sct:
19-
for file_name in sct.save():
20-
print(file_name)
59+
if options.coordinates:
60+
output = kwargs['output'].format(**kwargs['mon'])
61+
sct_img = sct.grab(kwargs['mon'])
62+
to_png(sct_img.rgb, sct_img.size, output)
63+
if not options.quiet:
64+
print(os.path.realpath(output))
65+
else:
66+
for file_name in sct.save(**kwargs):
67+
if not options.quiet:
68+
print(os.path.realpath(file_name))
2169
return 0
2270
except ScreenShotError:
2371
return 1
2472

2573

2674
if __name__ == '__main__':
27-
exit(main())
75+
exit(main(sys.argv[1:]))

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
'classifiers': classifiers,
5454
'platforms': ['Darwin', 'Linux', 'Windows'],
5555
'packages': ['mss'],
56+
'entry_points': {'console_scripts': ['mss=mss.__main__:main']},
5657
'license': 'MIT',
5758
}
5859

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def purge_files():
1919
""" Remove all generated files from previous runs. """
2020

2121
for fname in glob.glob('*.png'):
22-
print('Deleting {0} ...'.format(fname))
22+
print('Deleting {0!r} ...'.format(fname))
2323
os.unlink(fname)
2424

2525
for fname in glob.glob('*.png.old'):
26-
print('Deleting {0} ...'.format(fname))
26+
print('Deleting {0!r} ...'.format(fname))
2727
os.unlink(fname)
2828

2929

tests/test_implementation.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,56 @@ def test_factory(monkeypatch):
8282
assert error == 'System not (yet?) implemented.'
8383

8484

85-
def test_python_call(monkeypatch):
86-
import mss.__main__
87-
mss.__main__.main()
88-
89-
def raise_():
90-
raise ScreenShotError()
91-
92-
pytest.skip('Not working for now.')
93-
monkeypatch.setattr(mss.mss, '__init__', raise_)
94-
with pytest.raises(ScreenShotError):
95-
mss.mss()
96-
monkeypatch.undo()
85+
def test_entry_point(capsys, sct):
86+
from mss.__main__ import main
87+
from datetime import datetime
88+
89+
for opt in ('-m', '--monitor'):
90+
main([opt, '1'])
91+
out, _ = capsys.readouterr()
92+
assert out.endswith('monitor-1.png\n')
93+
assert os.path.isfile('monitor-1.png')
94+
os.remove('monitor-1.png')
95+
96+
for opt in zip(('-m 1', '--monitor=1'), ('-q', '--quiet')):
97+
main(opt)
98+
out, _ = capsys.readouterr()
99+
assert not out
100+
assert os.path.isfile('monitor-1.png')
101+
os.remove('monitor-1.png')
102+
103+
fmt = 'sct-{width}x{height}.png'
104+
for opt in ('-o', '--out'):
105+
main([opt, fmt])
106+
filename = fmt.format(**sct.monitors[1])
107+
out, _ = capsys.readouterr()
108+
assert out.endswith(filename + '\n')
109+
assert os.path.isfile(filename)
110+
os.remove(filename)
111+
112+
fmt = 'sct_{mon}-{date:%Y-%m-%d}.png'
113+
for opt in ('-o', '--out'):
114+
main([opt, fmt])
115+
filename = fmt.format(mon=1, date=datetime.now())
116+
out, _ = capsys.readouterr()
117+
assert out.endswith(filename + '\n')
118+
assert os.path.isfile(filename)
119+
os.remove(filename)
120+
121+
coordinates = '2,12,40,67'
122+
for opt in ('-c', '--coordinates'):
123+
main([opt, coordinates])
124+
filename = 'sct-2x12_40x67.png'
125+
out, _ = capsys.readouterr()
126+
assert out.endswith(filename + '\n')
127+
assert os.path.isfile(filename)
128+
os.remove(filename)
129+
130+
coordinates = '2,12,40'
131+
for opt in ('-c', '--coordinates'):
132+
main([opt, coordinates])
133+
out, _ = capsys.readouterr()
134+
assert out == 'Coordinates syntax: top, left, width, height\n'
97135

98136

99137
def test_grab_with_tuple(sct):

0 commit comments

Comments
 (0)