Skip to content

Commit a766225

Browse files
author
BoboTiG
committed
Fix BoboTiG#5: add a shortcut to take automatically use the proper MSS class
1 parent dfa257d commit a766225

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

doc/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ devel 201y/mm/dd
66
- Python 2.6 to 3.5 ready
77
- code purgation and review, more debug informations
88
- delete --debug argument
9+
- fix #5: add a shortcut to take automatically use the proper MSS class
910
- MSS: few optimizations into save_img()
1011
- MSSMac: remove rotation from informations returned by enum_display_monitors()
1112
- MSSLinux: fix object has no attribute 'display' into __del__

mss.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
in supporting documentation or portions thereof, including
2626
modifications, that you make.
2727
'''
28-
__all__ = ['MSSLinux', 'MSSMac', 'MSSWindows', 'ScreenshotError']
28+
__all__ = ['mss', 'MSSLinux', 'MSSMac', 'MSSWindows', 'ScreenshotError']
2929

3030
from struct import pack
3131
from platform import system
@@ -588,12 +588,29 @@ def get_pixels(self, monitor):
588588
return self.image
589589

590590

591+
def mss(*args, **kwargs):
592+
''' Factory returning a proper MSS class instance.
593+
594+
It detects the plateform we are running on
595+
and choose the most adapted mss_class to take
596+
screenshots.
597+
598+
It then proxies its arguments to the class for
599+
instantiation.
600+
'''
601+
602+
mss_class = {
603+
'Darwin': MSSMac,
604+
'Linux': MSSLinux,
605+
'Windows': MSSWindows
606+
}[system()]
607+
608+
return mss_class(*args, **kwargs)
609+
610+
591611
def main():
592612
''' Usage example. '''
593613

594-
systems = {'Darwin': MSSMac, 'Linux': MSSLinux, 'Windows': MSSWindows}
595-
mss = systems[system()]()
596-
597614
def on_exists(fname):
598615
''' Callback example when we try to overwrite an existing
599616
screenshot.
@@ -607,22 +624,24 @@ def on_exists(fname):
607624
return True
608625

609626
try:
610-
print('One screen shot per monitor')
611-
for filename in mss.save():
627+
screenshotter = mss()
628+
629+
print('One screenshot per monitor')
630+
for filename in screenshotter.save():
612631
print(filename)
613632

614-
print("\nScreen shot of the monitor 1")
615-
for filename in mss.save(output='monitor-%d.png', screen=1):
633+
print("\nScreenshot of the monitor 1")
634+
for filename in screenshotter.save(output='monitor-%d.png', screen=1):
616635
print(filename)
617636

618-
print("\nA shot to grab them all")
619-
for filename in mss.save(output='full-screenshot.png', screen=-1):
637+
print("\nA screenshot to grab them all")
638+
for filename in screenshotter.save(output='full-screenshot.png', screen=-1):
620639
print(filename)
621640

622-
print("\nScreen shot of the monitor 1, with callback")
623-
for filename in mss.save(output='mon-%d.png',
624-
screen=1,
625-
callback=on_exists):
641+
print("\nScreenshot of the monitor 1, with callback")
642+
for filename in screenshotter.save(output='mon-%d.png',
643+
screen=1,
644+
callback=on_exists):
626645
print(filename)
627646
except ScreenshotError as ex:
628647
print(ex)

0 commit comments

Comments
 (0)