Exemplo n.º 1
0
def describe_frameworks(serv, args, blueprint_name):
    """
    Function used to answer to DescribeFrameworks requests

    :param serv:    Service object
    :param args:    parameters of the TJS request
    :return:        request response
    """

    exceptions = []

    arg_version = args.get("version")
    # TODO: handle language parameter
    arg_language = args.get("language")
    arg_framework_uri = args.get("frameworkuri")

    # TODO: handle language parameter
    arg_language = serv.languages[0]

    if arg_framework_uri:
        framework_uris = [item.strip() for item in arg_framework_uri.split(",")]
        frameworks = [serv.get_framework_with_uri(uri) for uri in framework_uris]
    else:
        frameworks = list(serv.get_frameworks())

    # Get the jinja template corresponding to the TJS specifications version
    if arg_version in ("1.0",):
        template_name = blueprint_name + "/tjs_100_describeframeworks.xml"
    else:
        # TJS exception
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "This version of the TJS specifications is not supported by this TJS implementation: {}. "
                "Supported version numbers are: {}.".format(
                    arg_version, ", ".join(SUPPORTED_VERSIONS)
                ),
                "locator": "version={}".format(arg_version),
            }
        )

        raise OwsCommonException(exceptions=exceptions)

    response_content = render_template(
        template_name,
        service=serv,
        frameworks=frameworks,
        tjs_version=arg_version,
        language=arg_language,
        onetjs_version=current_app.version,
    )
    response_content = utils.prettify_xml(
        xml_string=response_content, minify=not current_app.debug
    )
    response = make_response(response_content)
    response.headers["Content-Type"] = "application/xml"

    return response
Exemplo n.º 2
0
def handle_tjs_exception(error):

    if error.tjs_version in ("1.0",):
        template_name = "ows_common_110_exception.xml"
    else:
        template_name = "ows_common_110_exception.xml"

    response_content = render_template(template_name, exceptions=error.exceptions)
    response_content = utils.prettify_xml(
        xml_string=response_content, minify=not current_app.debug
    )
    response = make_response(response_content)
    response.headers["Content-Type"] = "application/xml"
    response.status_code = error.status_code
    return response
Exemplo n.º 3
0
def get_data(serv, args, blueprint_name):
    """
    Function used to answer to GetData requests

    :param serv:    Service object
    :param args:    parameters of the TJS request
    :return:        request response
    """

    exceptions = []

    arg_version = args.get("version")
    # TODO: handle language parameter
    arg_language = args.get("language")
    arg_framework_uri = args.get("frameworkuri")
    arg_dataset_uri = args.get("dataseturi")
    arg_attributes = args.get("attributes")
    # TODO: handle linkagekeys parameter
    arg_linkage_keys = args.get("linkagekeys")
    # TODO: handle xsl parameter
    arg_xsl = args.get("xsl")
    # TODO: handle aid parameter
    arg_aid = args.get("aid")

    # Get the jinja template corresponding to the TJS specifications version
    if arg_version in ("1.0",):
        template_name = blueprint_name + "/tjs_100_getdata.xml"
    else:
        # TJS exception
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "This version of the TJS specifications is not supported by this TJS implementation: {}. "
                "Supported version numbers are: {}.".format(
                    arg_version, ", ".join(SUPPORTED_VERSIONS)
                ),
                "locator": "version={}".format(arg_version),
            }
        )

        raise OwsCommonException(exceptions=exceptions)

    # TODO: handle language parameter
    arg_language = serv.languages[0]

    # Missing frameworkuri parameter
    if not arg_framework_uri:
        exceptions.append(
            {
                "code": "MissingParameterValue",
                "text": "Oh là là ! "
                "The 'frameworkuri' parameter is mandatory for GetData operation. "
                "This TJS server cannot make a guess for this parameter.",
                "locator": "frameworkuri",
            }
        )

    # Missing dataseturi parameter
    if not arg_dataset_uri:
        exceptions.append(
            {
                "code": "MissingParameterValue",
                "text": "Oh là là ! "
                "The 'dataseturi' parameter is mandatory for GetData operation. "
                "This TJS server cannot make a guess for this parameter.",
                "locator": "dataseturi",
            }
        )

    if not (arg_framework_uri and arg_dataset_uri):
        raise OwsCommonException(exceptions=exceptions)

    # Retrieve the Framework record
    frwk = serv.get_framework_with_uri(arg_framework_uri)
    if frwk is None:
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "The parameter value for 'frameworkuri' is not valid: {}.".format(
                    arg_framework_uri
                ),
                "locator": "frameworkuri={}".format(arg_framework_uri),
            }
        )
        raise OwsCommonException(exceptions=exceptions)

    # Retrieve the Dataset record
    dtst = serv.get_dataset_with_uri(arg_dataset_uri)
    if dtst is None:
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "The parameter value for 'dataseturi' is not valid: {}.".format(
                    arg_dataset_uri
                ),
                "locator": "dataseturi={}".format(arg_dataset_uri),
            }
        )
        raise OwsCommonException(exceptions=exceptions)

    # Retrieve the DatasetAttribute records
    dtst_attributes = []
    if arg_attributes:
        attributes_names = [
            attribute_name.strip() for attribute_name in arg_attributes.split(",")
        ]
        for attribute_name in attributes_names:
            attribute = dtst.get_attribute_with_name(attribute_name)
            if attribute is None:
                exceptions.append(
                    {
                        "code": "InvalidParameterValue",
                        "text": "Oh là là ! "
                        "The requested attribute is not valid: {}.".format(
                            attribute_name
                        ),
                        "locator": "attributes={}".format(arg_attributes),
                    }
                )
            else:
                dtst_attributes.append(attribute)
        if len(attributes_names) != len(dtst_attributes):
            raise OwsCommonException(exceptions=exceptions)
    else:
        dtst_attributes = dtst.ds_attributes

    # TODO: handle the complete set of parameters declared in the TJS specification

    # Create a pandas data frame from the dataset datasource
    try:
        data = dtst.get_data(attributes=dtst_attributes, framework=frwk)
    except ValueError as e:
        current_app.logger.error(e.message)
        data = None

    # TODO: handle correctly empty pd_dataframe (None for example)
    response_content = render_template(
        template_name,
        service=serv,
        tjs_version=arg_version,
        language=arg_language,
        framework=frwk,
        dataset=dtst,
        attributes=dtst_attributes,
        data=data,
        onetjs_version=current_app.version,
    )
    response_content = utils.prettify_xml(
        xml_string=response_content, minify=not current_app.debug
    )
    response = make_response(response_content)
    response.headers["Content-Type"] = "application/xml"

    # if dtst.cached:
    #     max_age = dtst.cache_max_age or 3600
    #     response.cache_control.max_age = max_age
    #     response.cache_control.public = True

    return response
Exemplo n.º 4
0
def describe_data(serv, args, blueprint_name):
    """
    Function used to answer to DescribeData requests

    :param serv:    Service object
    :param args:    parameters of the TJS request
    :return:        request response
    """

    exceptions = []

    arg_version = args.get("version")
    # TODO: handle language parameter
    arg_language = args.get("language")
    arg_framework_uri = args.get("frameworkuri")
    arg_dataset_uri = args.get("dataseturi")
    arg_attributes = args.get("attributes")

    # TODO: handle language parameter
    arg_language = serv.languages[0]

    framework_uri = None
    if arg_framework_uri:
        framework_uris = [item.strip() for item in arg_framework_uri.split(",")]
        if len(framework_uris) > 1:
            # TJS exception
            exceptions.append(
                {
                    "code": "InvalidParameterValue",
                    "text": "Oh là là ! "
                    "The frameworkuri parameter of the DescribeData operation can only contain one uri. ",
                    "locator": "frameworkuri={}".format(arg_framework_uri),
                }
            )
        framework_uri = framework_uris[0]
    else:
        exceptions.append(
            {
                "code": "MissingParameterValue",
                "text": "Oh là là ! " "The 'frameworkuri' parameter must be present.",
                "locator": "frameworkuri",
            }
        )

    dataset_uri = None
    if arg_dataset_uri:
        dataset_uris = [item.strip() for item in arg_dataset_uri.split(",")]
        if len(dataset_uris) > 1:
            # TJS exception
            exceptions.append(
                {
                    "code": "InvalidParameterValue",
                    "text": "Oh là là ! "
                    "The dataseturi parameter of the DescribeData operation can only contain one uri. ",
                    "locator": "dataseturi={}".format(arg_dataset_uri),
                }
            )
        dataset_uri = dataset_uris[0]
    else:
        exceptions.append(
            {
                "code": "MissingParameterValue",
                "text": "Oh là là ! " "The 'dataseturi' parameter must be present.",
                "locator": "dataseturi",
            }
        )

    # Check if the frameworkuri and dataseturi values are compatible
    dtst = serv.get_dataset_with_uri(dataset_uri)
    frwk = serv.get_framework_with_uri(framework_uri)
    if frwk not in dtst.get_frameworks():
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "The specified framework is not available for the specified dataseturi.",
                "locator": "frameworkuri",
            }
        )

    # Retrieve the DatasetAttribute records
    dtst_attributes = []
    if arg_attributes:
        attributes_names = [
            attribute_name.strip() for attribute_name in arg_attributes.split(",")
        ]
        for attribute_name in attributes_names:
            attribute = dtst.get_attribute_with_name(attribute_name)
            if attribute is None:
                exceptions.append(
                    {
                        "code": "InvalidParameterValue",
                        "text": "Oh là là ! "
                        "The requested attribute is not valid: {}.".format(
                            attribute_name
                        ),
                        "locator": "attributes={}".format(arg_attributes),
                    }
                )
            else:
                dtst_attributes.append(attribute)
    else:
        dtst_attributes = dtst.ds_attributes

    # Get the jinja template corresponding to the TJS specifications version
    if arg_version in ("1.0",):
        template_name = blueprint_name + "/tjs_100_describedata.xml"
    else:
        # TJS exception
        exceptions.append(
            {
                "code": "InvalidParameterValue",
                "text": "Oh là là ! "
                "This version of the TJS specifications is not supported by this TJS implementation: {}. "
                "Supported version numbers are: {}.".format(
                    arg_version, ", ".join(SUPPORTED_VERSIONS)
                ),
                "locator": "version={}".format(arg_version),
            }
        )

    if exceptions:
        raise OwsCommonException(exceptions=exceptions)

    response_content = render_template(
        template_name,
        service=serv,
        tjs_version=arg_version,
        language=arg_language,
        framework=frwk,
        dataset=dtst,
        attributes=dtst_attributes,
        onetjs_version=current_app.version,
    )
    response_content = utils.prettify_xml(
        xml_string=response_content, minify=not current_app.debug
    )
    response = make_response(response_content)
    response.headers["Content-Type"] = "application/xml"

    return response
Exemplo n.º 5
0
def get_capabilities(serv, args, blueprint_name):
    """
    Function used to answer to GetCapabilities requests

    :param serv:    Service object
    :param args:    parameters of the TJS request
    :return:        request response
    """

    exceptions = []

    arg_accept_versions = args.get("acceptversions")
    # TODO: handle language parameter
    arg_language = args.get("language")

    tjs_version = None

    if arg_accept_versions:
        accepted_and_supported_versions = []

        for vrs in arg_accept_versions.split(","):
            try:
                strict_vrs = Version(vrs)
                for supported_vrs in SUPPORTED_VERSIONS:
                    if strict_vrs == supported_vrs:
                        accepted_and_supported_versions.append(strict_vrs)
            except ValueError as e:
                exceptions.append(
                    {
                        "code": "VersionNegotiationFailed",
                        "text": "Oh là là ! " "{}".format(e.message),
                        "locator": "acceptversions",
                    }
                )

        if accepted_and_supported_versions:
            tjs_version = str(accepted_and_supported_versions[0])
            print(tjs_version)
        else:
            exceptions.append(
                {
                    "code": "VersionNegotiationFailed",
                    "text": "Oh là là ! "
                    "The 'acceptversions' does not include any version supported by this server."
                    "Supported versions are: {}".format(
                        ",".join([str(vrs) for vrs in SUPPORTED_VERSIONS])
                    ),
                    "locator": "acceptversions",
                }
            )
    else:
        tjs_version = str(SUPPORTED_VERSIONS[0])

    if tjs_version == "1.0":
        template_name = blueprint_name + "/tjs_100_getcapabilities.xml"

        # TODO: handle language parameter
        arg_language = serv.languages[0]

        response_content = render_template(
            template_name,
            service=serv,
            tjs_version=tjs_version,
            onetjs_version=current_app.version,
        )
        response_content = utils.prettify_xml(
            xml_string=response_content, minify=not current_app.debug
        )
        response = make_response(response_content)
        response.headers["Content-Type"] = "application/xml"

        return response

    raise OwsCommonException(exceptions=exceptions)