<s> from __future__ import print_function <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> from java . awt import Toolkit , Robot , Rectangle <EOL> from javax . imageio import ImageIO <EOL> from java . io import File <EOL> elif sys . platform == '<STR_LIT>' : <EOL> import clr <EOL> clr . AddReference ( '<STR_LIT>' ) <EOL> clr . AddReference ( '<STR_LIT>' ) <EOL> from System . Drawing import Bitmap , Graphics , Imaging <EOL> from System . Windows . Forms import Screen <EOL> else : <EOL> try : <EOL> import wx <EOL> except ImportError : <EOL> wx = None <EOL> try : <EOL> from gtk import gdk <EOL> except ImportError : <EOL> gdk = None <EOL> try : <EOL> from PIL import ImageGrab <EOL> except ImportError : <EOL> ImageGrab = None <EOL> from robot . api import logger <EOL> from robot . libraries . BuiltIn import BuiltIn <EOL> from robot . version import get_version <EOL> from robot . utils import abspath , get_error_message , get_link_path , py2to3 <EOL> class Screenshot ( object ) : <EOL> """<STR_LIT>""" <EOL> ROBOT_LIBRARY_SCOPE = '<STR_LIT>' <EOL> ROBOT_LIBRARY_VERSION = get_version ( ) <EOL> def __init__ ( self , screenshot_directory = None , screenshot_module = None ) : <EOL> """<STR_LIT>""" <EOL> self . _given_screenshot_dir = self . _norm_path ( screenshot_directory ) <EOL> self . _screenshot_taker = ScreenshotTaker ( screenshot_module ) <EOL> def _norm_path ( self , path ) : <EOL> if not path : <EOL> return path <EOL> return os . path . normpath ( path . replace ( '<STR_LIT:/>' , os . sep ) ) <EOL> @ property <EOL> def _screenshot_dir ( self ) : <EOL> return self . _given_screenshot_dir or self . _log_dir <EOL> @ property <EOL> def _log_dir ( self ) : <EOL> variables = BuiltIn ( ) . get_variables ( ) <EOL> outdir = variables [ '<STR_LIT>' ] <EOL> log = variables [ '<STR_LIT>' ] <EOL> log = os . path . dirname ( log ) if log != '<STR_LIT>' else '<STR_LIT:.>' <EOL> return self . _norm_path ( os . path . join ( outdir , log ) ) <EOL> def set_screenshot_directory ( self , path ) : <EOL> """<STR_LIT>""" <EOL> path = self . _norm_path ( path ) <EOL> if not os . path . isdir ( path ) : <EOL> raise RuntimeError ( "<STR_LIT>" % path ) <EOL> old = self . _screenshot_dir <EOL> self . _given_screenshot_dir = path <EOL> return old <EOL> def take_screenshot ( self , name = "<STR_LIT>" , width = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> path = self . _save_screenshot ( name ) <EOL> self . _embed_screenshot ( path , width ) <EOL> return path <EOL> def take_screenshot_without_embedding ( self , name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> path = self . _save_screenshot ( name ) <EOL> self . _link_screenshot ( path ) <EOL> return path <EOL> def _save_screenshot ( self , basename , directory = None ) : <EOL> path = self . _get_screenshot_path ( basename , directory ) <EOL> return self . _screenshot_to_file ( path ) <EOL> def _screenshot_to_file ( self , path ) : <EOL> path = self . _validate_screenshot_path ( path ) <EOL> logger . debug ( '<STR_LIT>' <EOL> % self . _screenshot_taker . module ) <EOL> try : <EOL> self . _screenshot_taker ( path ) <EOL> except : <EOL> logger . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % get_error_message ( ) ) <EOL> return path <EOL> def _validate_screenshot_path ( self , path ) : <EOL> path = abspath ( self . _norm_path ( path ) ) <EOL> if not os . path . exists ( os . path . dirname ( path ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % os . path . dirname ( path ) ) <EOL> return path <EOL> def _get_screenshot_path ( self , basename , directory ) : <EOL> directory = self . _norm_path ( directory ) if directory else self . _screenshot_dir <EOL> if basename . lower ( ) . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> return os . path . join ( directory , basename ) <EOL> index = <NUM_LIT:0> <EOL> while True : <EOL> index += <NUM_LIT:1> <EOL> path = os . path . join ( directory , "<STR_LIT>" % ( basename , index ) ) <EOL> if not os . path . exists ( path ) : <EOL> return path <EOL> def _embed_screenshot ( self , path , width ) : <EOL> link = get_link_path ( path , self . _log_dir ) <EOL> logger . info ( '<STR_LIT>' <EOL> % ( link , link , width ) , html = True ) <EOL> def _link_screenshot ( self , path ) : <EOL> link = get_link_path ( path , self . _log_dir ) <EOL> logger . info ( "<STR_LIT>" <EOL> % ( link , path ) , html = True ) <EOL> @ py2to3 <EOL> class ScreenshotTaker ( object ) : <EOL> def __init__ ( self , module_name = None ) : <EOL> self . _screenshot = self . _get_screenshot_taker ( module_name ) <EOL> self . module = self . _screenshot . __name__ . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] <EOL> self . _wx_app_reference = None <EOL> def __call__ ( self , path ) : <EOL> self . _screenshot ( path ) <EOL> def __nonzero__ ( self ) : <EOL> return self . module != '<STR_LIT>' <EOL> def test ( self , path = None ) : <EOL> if not self : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> print ( "<STR_LIT>" % self . module ) <EOL> if not path : <EOL> print ( "<STR_LIT>" ) <EOL> return True <EOL> print ( "<STR_LIT>" % path ) <EOL> try : <EOL> self ( path ) <EOL> except : <EOL> print ( "<STR_LIT>" % get_error_message ( ) ) <EOL> return False <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> return True <EOL> def _get_screenshot_taker ( self , module_name = None ) : <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return self . _java_screenshot <EOL> if sys . platform == '<STR_LIT>' : <EOL> return self . _cli_screenshot <EOL> if sys . platform == '<STR_LIT>' : <EOL> return self . _osx_screenshot <EOL> if module_name : <EOL> return self . _get_named_screenshot_taker ( module_name . lower ( ) ) <EOL> return self . _get_default_screenshot_taker ( ) <EOL> def _get_named_screenshot_taker ( self , name ) : <EOL> screenshot_takers = { '<STR_LIT>' : ( wx , self . _wx_screenshot ) , <EOL> '<STR_LIT>' : ( gdk , self . _gtk_screenshot ) , <EOL> '<STR_LIT>' : ( ImageGrab , self . _pil_screenshot ) , <EOL> '<STR_LIT>' : ( self . _scrot , self . _scrot_screenshot ) } <EOL> if name not in screenshot_takers : <EOL> raise RuntimeError ( "<STR_LIT>" % name ) <EOL> supported , screenshot_taker = screenshot_takers [ name ] <EOL> if not supported : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> % name ) <EOL> return screenshot_taker <EOL> def _get_default_screenshot_taker ( self ) : <EOL> for module , screenshot_taker in [ ( wx , self . _wx_screenshot ) , <EOL> ( gdk , self . _gtk_screenshot ) , <EOL> ( ImageGrab , self . _pil_screenshot ) , <EOL> ( self . _scrot , self . _scrot_screenshot ) , <EOL> ( True , self . _no_screenshot ) ] : <EOL> if module : <EOL> return screenshot_taker <EOL> def _java_screenshot ( self , path ) : <EOL> size = Toolkit . getDefaultToolkit ( ) . getScreenSize ( ) <EOL> rectangle = Rectangle ( <NUM_LIT:0> , <NUM_LIT:0> , size . width , size . height ) <EOL> image = Robot ( ) . createScreenCapture ( rectangle ) <EOL> ImageIO . write ( image , '<STR_LIT>' , File ( path ) ) <EOL> def _cli_screenshot ( self , path ) : <EOL> bmp = Bitmap ( Screen . PrimaryScreen . Bounds . Width , <EOL> Screen . PrimaryScreen . Bounds . Height ) <EOL> graphics = Graphics . FromImage ( bmp ) <EOL> try : <EOL> graphics . CopyFromScreen ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , bmp . Size ) <EOL> finally : <EOL> graphics . Dispose ( ) <EOL> bmp . Save ( path , Imaging . ImageFormat . Jpeg ) <EOL> def _osx_screenshot ( self , path ) : <EOL> if self . _call ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , path ) != <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _call ( self , * command ) : <EOL> try : <EOL> return subprocess . call ( command , stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) <EOL> except OSError : <EOL> return - <NUM_LIT:1> <EOL> @ property <EOL> def _scrot ( self ) : <EOL> return os . sep == '<STR_LIT:/>' and self . _call ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT:0> <EOL> def _scrot_screenshot ( self , path ) : <EOL> if not path . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % os . path . splitext ( path ) [ <NUM_LIT:1> ] ) <EOL> if self . _call ( '<STR_LIT>' , '<STR_LIT>' , path ) != <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _wx_screenshot ( self , path ) : <EOL> if not self . _wx_app_reference : <EOL> self . _wx_app_reference = wx . App ( False ) <EOL> context = wx . ScreenDC ( ) <EOL> width , height = context . GetSize ( ) <EOL> bitmap = wx . EmptyBitmap ( width , height , - <NUM_LIT:1> ) <EOL> memory = wx . MemoryDC ( ) <EOL> memory . SelectObject ( bitmap ) <EOL> memory . Blit ( <NUM_LIT:0> , <NUM_LIT:0> , width , height , context , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> memory . SelectObject ( wx . NullBitmap ) <EOL> bitmap . SaveFile ( path , wx . BITMAP_TYPE_JPEG ) <EOL> def _gtk_screenshot ( self , path ) : <EOL> window = gdk . get_default_root_window ( ) <EOL> if not window : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> width , height = window . get_size ( ) <EOL> pb = gdk . Pixbuf ( gdk . COLORSPACE_RGB , False , <NUM_LIT:8> , width , height ) <EOL> pb = pb . get_from_drawable ( window , window . get_colormap ( ) , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , width , height ) <EOL> if not pb : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> pb . save ( path , '<STR_LIT>' ) <EOL> def _pil_screenshot ( self , path ) : <EOL> ImageGrab . grab ( ) . save ( path , '<STR_LIT>' ) <EOL> def _no_screenshot ( self , path ) : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) not in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> sys . exit ( "<STR_LIT>" <EOL> % os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> path = sys . argv [ <NUM_LIT:1> ] if sys . argv [ <NUM_LIT:1> ] != '<STR_LIT:test>' else None <EOL> module = sys . argv [ <NUM_LIT:2> ] if len ( sys . argv ) > <NUM_LIT:2> else None <EOL> ScreenshotTaker ( module ) . test ( path ) </s>
