Ejemplo n.º 1
0
def check_status(url=None, response=None, sleep_secs=2, verify=False):
    """
    Run owslib.wps check_status with additional exception handling.

    :param verify: Flag to enable SSL verification. Default: False
    :return: OWSLib.wps.WPSExecution object.
    """
    execution = WPSExecution()
    if response:
        LOGGER.debug("using response document ...")
        xml = response
    elif url:
        LOGGER.debug('using status_location url ...')
        xml = requests.get(url, verify=verify).content
    else:
        raise Exception(
            "you need to provide a status-location url or response object.")
    # TODO: see owslib: https://github.com/geopython/OWSLib/pull/477
    execution.checkStatus(response=xml, sleepSecs=sleep_secs)
    if execution.response is None:
        raise Exception("check_status failed!")
    # TODO: workaround for owslib type change of reponse
    if not isinstance(execution.response, etree._Element):
        execution.response = etree.fromstring(execution.response)
    return execution
Ejemplo n.º 2
0
def check_status(url=None, response=None, sleep_secs=2, verify=False):
    """
    Run owslib.wps check_status with additional exception handling.

    :param verify: Flag to enable SSL verification. Default: False
    :return: OWSLib.wps.WPSExecution object.
    """
    execution = WPSExecution()
    if response:
        logger.debug("using response document ...")
        xml = response
    elif url:
        logger.debug('using status_location url ...')
        xml = requests.get(url, verify=verify).content
    else:
        raise Exception("you need to provide a status-location url or response object.")
    if type(xml) is unicode:
        xml = xml.encode('utf8', errors='ignore')
    execution.checkStatus(response=xml, sleepSecs=sleep_secs)
    if execution.response is None:
        raise Exception("check_status failed!")
    # TODO: workaround for owslib type change of reponse
    if not isinstance(execution.response, etree._Element):
        execution.response = etree.fromstring(execution.response)
    return execution
Ejemplo n.º 3
0
def check_wps_status(
        location=None,  # type: Optional[str]
        response=None,  # type: Optional[XML]
        sleep_secs=2,  # type: int
        verify=True,  # type: bool
        settings=None,  # type: Optional[AnySettingsContainer]
):  # type: (...) -> WPSExecution
    """
    Run :func:`owslib.wps.WPSExecution.checkStatus` with additional exception handling.

    :param location: job URL or file path where to look for job status.
    :param response: WPS response document of job status.
    :param sleep_secs: number of seconds to sleep before returning control to the caller.
    :param verify: Flag to enable SSL verification.
    :param settings: Application settings to retrieve any additional request parameters as applicable.
    :return: OWSLib.wps.WPSExecution object.
    """
    def _retry_file():
        LOGGER.warning(
            "Failed retrieving WPS status-location, attempting with local file."
        )
        out_path = get_wps_local_status_location(location, settings)
        if not out_path:
            raise HTTPNotFound(
                "Could not find file resource from [{}].".format(location))
        LOGGER.info("Resolved WPS status-location using local file reference.")
        return open(out_path, "r").read()

    execution = WPSExecution()
    if response:
        LOGGER.debug("Retrieving WPS status from XML response document...")
        xml = response
    elif location:
        xml_resp = HTTPNotFound()
        try:
            LOGGER.debug("Attempt to retrieve WPS status-location from URL...")
            xml_resp = request_extra("get",
                                     location,
                                     verify=verify,
                                     settings=settings)
            xml = xml_resp.content
        except Exception as ex:
            LOGGER.debug("Got exception during get status: [%r]", ex)
            xml = _retry_file()
        if xml_resp.status_code == HTTPNotFound.code:
            LOGGER.debug("Got not-found during get status: [%r]", xml)
            xml = _retry_file()
    else:
        raise Exception(
            "Missing status-location URL/file reference or response with XML object."
        )
    if isinstance(xml, str):
        xml = xml.encode("utf8", errors="ignore")
    execution.checkStatus(response=xml, sleepSecs=sleep_secs)
    if execution.response is None:
        raise Exception("Missing response, cannot check status.")
    if not isinstance(execution.response, lxml.etree._Element):  # noqa
        execution.response = lxml.etree.fromstring(execution.response)
    return execution
Ejemplo n.º 4
0
def check_status(url=None, response=None, sleep_secs=2, verify=False):
    """
    Run owslib.wps check_status with additional exception handling.

    :param verify: Flag to enable SSL verification. Default: False
    :return: OWSLib.wps.WPSExecution object.
    """
    execution = WPSExecution()
    if response:
        LOGGER.debug("using response document ...")
        xml = response
    elif url:
        LOGGER.debug('using status_location url ...')
        xml = requests.get(url, verify=verify).content
    else:
        raise Exception("you need to provide a status-location url or response object.")
    # TODO: see owslib: https://github.com/geopython/OWSLib/pull/477
    execution.checkStatus(response=xml, sleepSecs=sleep_secs)
    if execution.response is None:
        raise Exception("check_status failed!")
    # TODO: workaround for owslib type change of reponse
    if not isinstance(execution.response, etree._Element):
        execution.response = etree.fromstring(execution.response)
    return execution
Ejemplo n.º 5
0
def check_wps_status(
        location=None,  # type: Optional[str]
        response=None,  # type: Optional[xml_util.XML]
        sleep_secs=2,  # type: int
        verify=True,  # type: bool
        settings=None,  # type: Optional[AnySettingsContainer]
):  # type: (...) -> WPSExecution
    """
    Run :func:`owslib.wps.WPSExecution.checkStatus` with additional exception handling.

    :param location: job URL or file path where to look for job status.
    :param response: WPS response document of job status.
    :param sleep_secs: number of seconds to sleep before returning control to the caller.
    :param verify: flag to enable SSL verification.
    :param settings: application settings to retrieve any additional request parameters as applicable.
    :returns: OWSLib.wps.WPSExecution object.
    """
    def _retry_file():
        # type: () -> str
        LOGGER.warning(
            "Failed retrieving WPS status-location, attempting with local file."
        )
        out_path = get_wps_local_status_location(location, settings)
        if not out_path:
            raise HTTPNotFound(
                f"Could not find file resource from [{location}].")
        LOGGER.info("Resolved WPS status-location using local file reference.")
        with open(out_path, mode="r", encoding="utf-8") as f:
            return f.read()

    execution = WPSExecution()
    if response:
        LOGGER.debug("Retrieving WPS status from XML response document...")
        xml_data = response
    elif location:
        xml_resp = HTTPNotFound()
        xml_data = None
        try:
            LOGGER.debug(
                "Attempt to retrieve WPS status-location from URL [%s]...",
                location)
            xml_resp = request_extra("get",
                                     location,
                                     verify=verify,
                                     settings=settings)
            xml_data = xml_resp.content
        except Exception as ex:
            LOGGER.debug(
                "Got exception during get status: [%r]. Will retry with local reference.",
                ex)
        if xml_resp.status_code != HTTPOk.code:
            LOGGER.debug(
                "WPS XML status not found: [%r]. Retrying with local reference.",
                xml_data)
            xml_data = _retry_file()
    else:
        raise Exception(
            "Missing status-location URL/file reference or response with XML object."
        )
    if isinstance(xml_data, str):
        xml_data = xml_data.encode("utf8", errors="ignore")
    execution.checkStatus(response=xml_data, sleepSecs=sleep_secs)
    if execution.response is None:
        raise Exception("Missing response, cannot check status.")
    if not isinstance(execution.response, xml_util.XML):
        execution.response = xml_util.fromstring(execution.response)
    return execution