'Save a Screenshot of a saved map
@requires:
PhantomJS http://phantomjs.org
Selenium https://pypi.python.org/pypi/selenium'

@staticmethod
def get_screenshot(config_id, temp=True, height=None, width=None):

    map_id = 'default_map'
    from webdriver import WebDriver
    from selenium.common.exceptions import TimeoutException, WebDriverException
    from selenium.webdriver.support.ui import WebDriverWait
    request = current.request
    cachepath = os.path.join(request.folder, 'static', 'cache', 'jpg')
    if (not os.path.exists(cachepath)):
        try:
            os.mkdir(cachepath)
        except OSError as os_error:
            error = ('GIS:    JPEG    files    cannot    be    saved:    %s    %s' % (cachepath, os_error))
            current.log.error(error)
            current.session.error = error
            redirect(URL(c='gis', f='index', vars={'config': config_id}))
    cwd = os.getcwd()
    os.chdir(cachepath)
    driver = WebDriver(service_args=['--proxy-type=none'])
    os.chdir(cwd)
    settings = current.deployment_settings
    if (height is None):
        height = settings.get_gis_map_height()
    if (width is None):
        width = settings.get_gis_map_width()
    driver.set_window_size((width + 5), (height + 20))
    base_url = ('%s/%s' % (settings.get_base_public_url(), request.application))
    driver.get(base_url)
    response = current.response
    session_id = response.session_id
    if (not current.auth.override):
        driver.add_cookie({'name': response.session_id_name, 'value': session_id, 'path': '/'})
        current.session._unlock(response)
    url = ('%s/gis/map_viewing_client?print=1&config=%s' % (base_url, config_id))
    driver.get(url)
    def map_loaded(driver):
        test = ("return    S3.gis.maps['%s'].s3.loaded" % map_id)
        try:
            result = driver.execute_script(test)
        except WebDriverException as e:
            result = False
        return result
    try:
        WebDriverWait(driver, 100).until(map_loaded)
    except TimeoutException as e:
        driver.quit()
        current.log.error(('Timeout:    %s' % e))
        return None
    if temp:
        filename = ('%s.jpg' % session_id)
    else:
        filename = ('config_%s.jpg' % config_id)
    script = ("\nvar    page    =    this;\npage.clipRect    =    {top:    10,\n                                                                    left:    5,\n                                                                    width:    %(width)s,\n                                                                    height:    %(height)s\n                                                                    };\npage.render('%(filename)s',    {format:    'jpeg',    quality:    '100'});" % dict(width=width, height=height, filename=filename))
    try:
        result = driver.execute_phantomjs(script)
    except WebDriverException as e:
        driver.quit()
        current.log.error(('WebDriver    crashed:    %s' % e))
        return None
    driver.quit()
    if temp:
        ctable = current.s3db.gis_config
        the_set = current.db((ctable.id == config_id))
        config = the_set.select(ctable.temp, limitby=(0, 1)).first()
        try:
            if config.temp:
                the_set.delete()
        except:
            pass
    return filename
