Beispiel #1
0
def _encode_process_brief(process, elem):
    """ Insert a brief process description into an XML element passed as the
    second argument.
    The brief process description is shared by both the Capabilities and
    ProcessDescriptions XML encoders.
    """
    identifier = getattr(process, 'identifier', type(process).__name__)
    title = getattr(process, 'title', identifier)
    abstract = getattr(process, 'description', process.__doc__)
    version = getattr(process, "version", "1.0.0")
    metadata = getattr(process, "metadata", {})
    profiles = getattr(process, "profiles", [])
    wsdl = getattr(process, "wsdl", None)

    elem.append(OWS("Identifier", identifier))
    elem.append(OWS("Title", title))
    elem.attrib[ns_wps("processVersion")] = version
    if abstract:
        elem.append(OWS("Abstract", abstract))
    elem.extend(_encode_metadata(k, metadata[k]) for k in metadata)
    elem.extend(WPS("Profile", profile) for profile in profiles)
    if wsdl:
        elem.append(WPS("WSDL", **{ns_xlink("href"): wsdl}))

    return elem
Beispiel #2
0
def _encode_literal(prm, is_input):
    """ Encode process description LiteralData (parameter definition) element.
    """
    dtype = prm.dtype
    elem = NIL("LiteralData" if is_input else "LiteralOutput")

    elem.append(
        OWS(
            "DataType", dtype.name, **{
                ns_ows("reference"):
                "http://www.w3.org/TR/xmlschema-2/#%s" % dtype.name,
            }))

    if prm.uoms:
        elem.append(
            NIL("UOMs", NIL("Default", OWS("UOM", prm.uoms[0])),
                NIL("Supported", *[OWS("UOM", u) for u in prm.uoms])))

    if is_input:
        elem.append(_encode_allowed_value(prm.allowed_values))

        if prm.default is not None:
            elem.append(NIL("DefaultValue", str(prm.default)))

    return elem
Beispiel #3
0
def _encode_param_common(prm, title_required=True):
    """ Encode the Identifier, Title and Abstract sub-elements common to all
    input and output parameters.
    """
    elist = [OWS("Identifier", prm.identifier)]
    if prm.title or title_required:
        elist.append(OWS("Title", prm.title or prm.identifier))
    if prm.abstract:
        elist.append(OWS("Abstract", prm.abstract))
    return elist
Beispiel #4
0
def _encode_bbox(data, prm):
    """ Encode Data/BoundingBoxData element. """
    try:
        lower, upper, crs = prm.encode_xml(data)
    except (ValueError, TypeError) as exc:
        raise InvalidOutputValueError(prm.identifier, exc)

    return WPS(
        "BoundingBoxData",
        OWS("LowerCorner", lower),
        OWS("UpperCorner", upper),
        crs=crs,
        #dimension="%d"%prm.dimension,
    )
Beispiel #5
0
 def encode_failed(self, exception):
     """ Encode ProcessFailed execute response."""
     # NOTE: Some exceptions such as the urllib2.HTTPError have also
     # the 'code' attribute and the duck typing does not work very well.
     # Therefore we need match the exception base type.
     if isinstance(exception, OWS10Exception):
         code = exception.code
         locator = exception.locator
     else:
         code = "NoApplicableCode"
         locator = type(exception).__name__
     message = str(exception)
     exc_attr = {"exceptionCode": str(code)}
     if locator:
         exc_attr["locator"] = str(locator)
     exc_elem = OWS("Exception", OWS("ExceptionText", message), **exc_attr)
     status = WPS("ProcessFailed", WPS("ExceptionReport", exc_elem))
     return self._encode_common(status)
Beispiel #6
0
def _encode_operations_metadata(conf):
    """ Encode OperationsMetadata XML element. """
    component = ServiceComponent(env)
    versions = ("1.0.0", )
    get_handlers = component.query_service_handlers(service="WPS",
                                                    versions=versions,
                                                    method="GET")
    post_handlers = component.query_service_handlers(service="WPS",
                                                     versions=versions,
                                                     method="POST")
    all_handlers = sorted(set(get_handlers + post_handlers),
                          key=lambda h: h.request)
    url = conf.http_service_url
    return OWS(
        "OperationsMetadata", *[
            OWS("Operation",
                OWS(
                    "DCP",
                    OWS(
                        "HTTP",
                        OWS("Get", **{ns_xlink("href"): url}),
                        OWS("Post", **{ns_xlink("href"): url}),
                    )),
                name=handler.request) for handler in all_handlers
        ])
Beispiel #7
0
def _encode_allowed_value(avobj):
    """ Encode process description LiteralData allowed values definition.
    Based on the type of the allowed value definition either AnyValue,
    ValuesReference, or AllowedValues element is returned.
    """
    enum, ranges, elist = None, [], []

    if isinstance(avobj, AllowedAny):
        return OWS("AnyValue")
    elif isinstance(avobj, AllowedByReference):
        return WPS(
            "ValuesReference", **{
                ns_ows("reference"): avobj.url,
                "valuesForm": avobj.url,
            })
    elif isinstance(avobj, AllowedEnum):
        enum = avobj
    elif isinstance(avobj, AllowedRange):
        ranges = [avobj]
    elif isinstance(avobj, AllowedRangeCollection):
        enum, ranges = avobj.enum, avobj.ranges
    else:
        raise TypeError("Invalid allowed value object! OBJ=%r" % avobj)

    dtype = avobj.dtype
    ddtype = dtype.get_diff_dtype()

    if enum is not None:
        elist.extend(OWS("Value", dtype.encode(v)) for v in enum.values)
    for range_ in ranges:
        attr, elms = {}, []
        if range_.closure != 'closed':
            attr = {ns_ows("rangeClosure"): range_.closure}
        if range_.minval is not None:
            elms.append(OWS("MinimumValue", dtype.encode(range_.minval)))
        if range_.maxval is not None:
            elms.append(OWS("MaximumValue", dtype.encode(range_.maxval)))
        if range_.spacing is not None:
            elms.append(OWS("Spacing", ddtype.encode(range_.spacing)))
        elist.append(OWS("Range", *elms, **attr))

    return OWS("AllowedValues", *elist)
Beispiel #8
0
    def encode_capabilities(processes):
        """ Encode Capabilities XML document. """
        conf = CapabilitiesConfigReader(get_eoxserver_config())

        # Avoid duplicate process offerings ...
        process_set = set()
        process_offerings = []
        for process in processes:
            process_identifier = (getattr(process, 'identifier', None)
                                  or type(process).__name__)
            if process_identifier not in process_set:
                process_offerings.append(encode_process_brief(process))
                process_set.add(process_identifier)

        return WPS(
            "Capabilities",
            OWS(
                "ServiceIdentification",
                OWS("Title", conf.title),
                OWS("Abstract", conf.abstract),
                OWS("Keywords", *(OWS("Keyword", kw) for kw in conf.keywords)),
                OWS("ServiceType", "WPS"),
                OWS("ServiceTypeVersion", "1.0.0"),
                OWS("Fees", conf.fees),
                OWS("AccessConstraints", conf.access_constraints),
            ),
            OWS(
                "ServiceProvider", OWS("ProviderName", conf.provider_name),
                OWS("ProviderSite", **{ns_xlink("href"): conf.provider_site}),
                OWS(
                    "ServiceContact",
                    OWS("IndividualName", conf.individual_name),
                    OWS("PositionName", conf.position_name),
                    OWS(
                        "ContactInfo",
                        OWS("Phone", OWS("Voice", conf.phone_voice),
                            OWS("Facsimile", conf.phone_facsimile)),
                        OWS(
                            "Address", OWS("DeliveryPoint",
                                           conf.delivery_point),
                            OWS("City", conf.city),
                            OWS("AdministrativeArea",
                                conf.administrative_area),
                            OWS("PostalCode", conf.postal_code),
                            OWS("Country", conf.country),
                            OWS("ElectronicMailAddress",
                                conf.electronic_mail_address))))),
            _encode_operations_metadata(conf),
            WPS("ProcessOfferings", *process_offerings),
            WPS("Languages", WPS("Default", OWS("Language", "en-US")),
                WPS("Supported", OWS("Language", "en-US"))),
            # TODO: WPS("WSDL")
            **{
                "service": "WPS",
                "version": "1.0.0",
                ns_xml("lang"): "en-US",
                "updateSequence": conf.update_sequence,
            })
Beispiel #9
0
def _encode_metadata(title, href):
    """ Encode one Metadata element. """
    return OWS("Metadata", **{ns_xlink("title"): title, ns_xlink("href"): href})