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
3030from struct import pack
3131from 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+
591611def 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 ("\n Screen shot of the monitor 1" )
615- for filename in mss .save (output = 'monitor-%d.png' , screen = 1 ):
633+ print ("\n Screenshot of the monitor 1" )
634+ for filename in screenshotter .save (output = 'monitor-%d.png' , screen = 1 ):
616635 print (filename )
617636
618- print ("\n A shot to grab them all" )
619- for filename in mss .save (output = 'full-screenshot.png' , screen = - 1 ):
637+ print ("\n A screenshot to grab them all" )
638+ for filename in screenshotter .save (output = 'full-screenshot.png' , screen = - 1 ):
620639 print (filename )
621640
622- print ("\n Screen 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 ("\n Screenshot 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