Skip to content

Commit aa43a68

Browse files
author
BoboTiG
committed
Export ScreenshotError exception
1 parent bab8ae6 commit aa43a68

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

mss.py

Lines changed: 16 additions & 21 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']
28+
__all__ = ['MSSLinux', 'MSSMac', 'MSSWindows', 'ScreenshotError']
2929

3030
from struct import pack
3131
from platform import system
@@ -104,24 +104,21 @@ class BITMAPINFOHEADER(Structure):
104104
class BITMAPINFO(Structure):
105105
_fields_ = [('bmiHeader', BITMAPINFOHEADER), ('bmiColors', DWORD * 3)]
106106
else:
107-
raise ScreenshotError('System "{}" not implemented.'.format(system()))
107+
raise ScreenshotError('MSS: system "{}" not implemented.'.format(system()))
108108

109109

110110
# ----------------------------------------------------------------------
111111
# --- [ C'est parti mon kiki ! ] ---------------------------------------
112112
# ----------------------------------------------------------------------
113113
class MSS(object):
114-
''' This class will be overloaded by a system specific one.
115-
It checkes if there is a class available for the current system.
116-
Raise an exception if no one found.
117-
'''
114+
''' This class will be overloaded by a system specific one. '''
118115

119116
DEBUG = False
120117

121118
def __init__(self, debug=False):
122119
''' Global vars and class overload. '''
123120

124-
self.DEBUG = debug in [True, 'on' 'yes', 'oui', 1]
121+
self.DEBUG = debug in [True, 1, 'on' 'yes', 'oui']
125122
self.debug('__init__', 'DEBUG', self.DEBUG)
126123
self.init()
127124

@@ -311,7 +308,7 @@ def get_pixels(self, monitor):
311308
image = CGWindowListCreateImage(rect, kCGWindowListOptionOnScreenOnly,
312309
kCGNullWindowID, kCGWindowImageDefault)
313310
if not image:
314-
raise ScreenshotError('CGWindowListCreateImage() failed.')
311+
raise ScreenshotError('MSS: CGWindowListCreateImage() failed.')
315312
return image
316313

317314
def save_img(self, data, width, height, output):
@@ -320,20 +317,16 @@ def save_img(self, data, width, height, output):
320317
self.debug('MSSMac: save_img')
321318

322319
url = NSURL.fileURLWithPath_(output)
323-
dest = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, None)
324-
if not dest:
325-
msg = 'Error while trying to create the image destination ' \
326-
'to "{}".'.format(output)
327-
raise ScreenshotError(msg)
328-
CGImageDestinationAddImage(dest, data, None)
329-
if not CGImageDestinationFinalize(dest):
330-
msg = 'Impossible to write data to file "{}".'.format(output)
331-
raise ScreenshotError(msg)
320+
if CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, None):
321+
CGImageDestinationAddImage(dest, data, None)
322+
if CGImageDestinationFinalize(dest):
323+
return
324+
raise ScreenshotError('MSS: error writing to file "{}".'.format(output))
332325

333326

334327
class MSSLinux(MSS):
335328
''' Mutli-screen shot implementation for GNU/Linux.
336-
It uses intensively the Xlib.
329+
It uses intensively the Xlib and Xrandr.
337330
'''
338331

339332
def __del__(self):
@@ -351,13 +344,13 @@ def init(self):
351344

352345
x11 = find_library('X11')
353346
if not x11:
354-
raise ScreenshotError('No X11 library found.')
347+
raise ScreenshotError('MSS: no X11 library found.')
355348
self.xlib = cdll.LoadLibrary(x11)
356349
self.debug('init', 'xlib', self.xlib)
357350

358351
xrandr = find_library('Xrandr')
359352
if not xrandr:
360-
raise ScreenshotError('No Xrandr library found.')
353+
raise ScreenshotError('MSS: no Xrandr library found.')
361354
self.xrandr = cdll.LoadLibrary(xrandr)
362355
self.debug('init', 'xrandr', self.xrandr)
363356

@@ -372,7 +365,7 @@ def init(self):
372365
else:
373366
disp = environ['DISPLAY']
374367
except KeyError:
375-
err = '$DISPLAY not set. Stopping to prevent segfault.'
368+
err = 'MSS: $DISPLAY not set. Stopping to prevent segfault.'
376369
raise ScreenshotError(err)
377370
self.debug('init', '$DISPLAY', disp)
378371

@@ -469,6 +462,8 @@ def enum_display_monitors(self, screen=0):
469462

470463
def get_pixels(self, monitor):
471464
''' Retrieve all pixels from a monitor. Pixels have to be RGB.
465+
466+
@TODO: this function takes the most time. Need better solution.
472467
'''
473468

474469
self.debug('get_pixels')

0 commit comments

Comments
 (0)