#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import subprocess
import tempfile

#TODO: Cahge scrot backend to ImageMagic
#      Edit the image
# https://wiki.archlinux.org/index.php/Taking_a_Screenshot
SCREENSHOT_SOUND = '/usr/share/sounds/scru_shot.wav'


class ScrotNotFound(Exception):
    """scrot must be installed"""


def grab(filename, select, sound, quality, delay, optipng):
    """Grab the screen as binary file"""
    if not filename:
        # Temporary file
        f = tempfile.NamedTemporaryFile(suffix='.png',
            prefix='screenshot_scrot_')
        filename = f.name
    grab_filename(filename, select, sound, quality, delay, optipng)
    # Open the temp screenshot
    return open(filename, 'rb')


def grab_filename(filename, select, sound, quality, delay, optipng):
    """Grab the screen as image file"""
    # Wrap of scrot command
    cmd = ['scrot', filename]
    opng = ['optipng', '-preserve', '-quiet', filename]
    #mogri = ['mogrify' '-depth', '8', filename]
    if select:
        cmd.append('-s')
        cmd.append('-b')    #show window decoration (border)
    if quality:
        cmd.append('-q%d' % quality)
    if delay:
        # delay and show regresive count
        cmd.append('-d%d' % delay)
        cmd.append('-c')
    try:
        subprocess.call(cmd)
        if sound:
            play_screenshot_sound()
        if optipng:
            subprocess.Popen(opng)
    except Exception, e:
        raise ScrotNotFound
        print e


def play_screenshot_sound():
    """"Play a sound of a camera shot"""
    try:
        # Player for alsa
        subprocess.call(['aplay', '-q', SCREENSHOT_SOUND])
    except OSError:
        # Player for oss
        subprocess.call(['ossplay', '-q', SCREENSHOT_SOUND])
    else:
        pass

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import subprocess
import tempfile

#TODO: Cahge scrot backend to ImageMagic
#      Edit the image
# https://wiki.archlinux.org/index.php/Taking_a_Screenshot
SCREENSHOT_SOUND = '/usr/share/sounds/scru_shot.wav'


class ScrotNotFound(Exception):
    """scrot must be installed"""


def grab(filename, select, sound, quality, delay, optipng):
    """Grab the screen as binary file"""
    if not filename:
        # Temporary file
        f = tempfile.NamedTemporaryFile(suffix='.png',
            prefix='screenshot_scrot_')
        filename = f.name
    grab_filename(filename, select, sound, quality, delay, optipng)
    # Open the temp screenshot
    return open(filename, 'rb')


def grab_filename(filename, select, sound, quality, delay, optipng):
    """Grab the screen as image file"""
    # Wrap of scrot command
    cmd = ['scrot', filename]
    opng = ['optipng', '-preserve', '-quiet', filename]
    #mogri = ['mogrify' '-depth', '8', filename]
    if select:
        cmd.append('-s')
        cmd.append('-b')    #show window decoration (border)
    if quality:
        cmd.append('-q%d' % quality)
    if delay:
        # delay and show regresive count
        cmd.append('-d%d' % delay)
        cmd.append('-c')
    try:
        subprocess.call(cmd)
        if sound:
            play_screenshot_sound()
        if optipng:
            subprocess.Popen(opng)
    except Exception, e:
        raise ScrotNotFound
        print e


def play_screenshot_sound():
    """"Play a sound of a camera shot"""
    try:
        # Player for alsa
        subprocess.call(['aplay', '-q', SCREENSHOT_SOUND])
    except OSError:
        # Player for oss
        subprocess.call(['ossplay', '-q', SCREENSHOT_SOUND])
    else:
        pass

#!/usr/bin/env python2 # -*- coding: utf-8 -*- #TODO: Cahge scrot backend to ImageMagic #      Edit the image # https://wiki.archlinux.org/index.php/Taking_a_Screenshot scrot must be installed Grab the screen as binary file # Temporary file # Open the temp screenshot Grab the screen as image file # Wrap of scrot command #mogri = ['mogrify' '-depth', '8', filename] #show window decoration (border) # delay and show regresive count "Play a sound of a camera shot # Player for alsa # Player for oss