from easyprocess import EasyProcess
from pyscreenshot.backendloader import BackendLoader
import Image
import logging
import tempfile

__version__ = '0.3.1'

log = logging.getLogger(__name__)
log.debug('version=' + __version__)


def grab(bbox=None, childprocess=False, backend=None):
    '''Copy the contents of the screen to PIL image memory.

    :param bbox: optional bounding box
    :param childprocess: pyscreenshot can cause an error,
            if it is used on more different virtual displays
            and back-end is not in different process.
            Some back-ends are always different processes: scrot, imagemagick
    :param backend: back-end can be forced if set (examples:scrot, wx,..),
                    otherwise back-end is automatic
    '''
    if childprocess:
        f = tempfile.NamedTemporaryFile(
            suffix='.png', prefix='pyscreenshot_childprocess_')
        filename = f.name

        params = ["'%s'" % (filename), 'childprocess=False']
        if backend:
            params += ["backend='%s'" % (backend)]
        params = ','.join(params)

        EasyProcess(['python',
                     '-c',
                     "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % params,
                     ]).check()
        im = Image.open(filename)
        if bbox:
            im = im.crop(bbox)
        return im
    else:
        if backend:
            BackendLoader().force(backend)
        return BackendLoader().selected().grab(bbox)


def grab_to_file(filename, childprocess=False, backend=None):
    '''Copy the contents of the screen to a file.

    :param filename: file for saving
    :param childprocess: see :py:func:`grab`
    :param backend: see :py:func:`grab`
    '''
    if childprocess:

        params = ["'%s'" % (filename), 'childprocess=False']
        if backend:
            params += ["backend='%s'" % (backend)]
        params = ','.join(params)

        EasyProcess(['python',
                     '-c',
                     "import pyscreenshot; pyscreenshot.grab_to_file(%s)" % params,
                     ]).check()
    else:
        if backend:
            BackendLoader().force(backend)
        return BackendLoader().selected().grab_to_file(filename)

@@ -125,16 +125,27 @@
 tempfile
+%0Aimport sys
 %0A%0A__vers
@@ -1107,32 +1107,49 @@
    EasyProcess(%5B
+sys.executable, #
 'python',%0A      
@@ -1966,16 +1966,33 @@
 rocess(%5B
+sys.executable, #
 'python'

Replace explicit 'python' reference with sys.executable - so it runs in the currently executing python