weaver.formats

Module Contents

weaver.formats.FileModeSteamType[source]
weaver.formats.LOGGER[source]
class weaver.formats.AcceptLanguage[source]

Supported languages.

EN_CA = 'en-CA'[source]
FR_CA = 'fr-CA'[source]
EN_US = 'en-US'[source]
classmethod offers() List[str][source]

Languages offered by the application.

class weaver.formats.ContentType[source]

Supported Content-Type values.

Media-Type nomenclature:

<type> "/" [x- | <tree> "."] <subtype> ["+" suffix] *[";" parameter=value]
APP_DIR = 'application/directory'[source]
APP_CWL = 'application/cwl'[source]
APP_CWL_JSON = 'application/cwl+json'[source]
APP_CWL_YAML = 'application/cwl+yaml'[source]
APP_CWL_X = 'application/x-cwl'[source]
APP_FORM = 'application/x-www-form-urlencoded'[source]
APP_GEOJSON = 'application/geo+json'[source]
APP_GZIP = 'application/gzip'[source]
APP_HDF5 = 'application/x-hdf5'[source]
APP_JSON = 'application/json'[source]
APP_OAS_JSON = 'application/vnd.oai.openapi+json; version=3.0'[source]
APP_OGC_PKG_JSON = 'application/ogcapppkg+json'[source]
APP_OGC_PKG_YAML = 'application/ogcapppkg+yaml'[source]
APP_NETCDF = 'application/x-netcdf'[source]
APP_OCTET_STREAM = 'application/octet-stream'[source]
APP_PDF = 'application/pdf'[source]
APP_TAR = 'application/x-tar'[source]
APP_TAR_GZ = 'application/tar+gzip'[source]
APP_VDN_GEOJSON = 'application/vnd.geo+json'[source]
APP_XML = 'application/xml'[source]
APP_YAML = 'application/x-yaml'[source]
APP_ZIP = 'application/zip'[source]
IMAGE_GEOTIFF = 'image/tiff; subtype=geotiff'[source]
IMAGE_OGC_GEOTIFF = 'image/tiff; application=geotiff'[source]
IMAGE_JPEG = 'image/jpeg'[source]
IMAGE_GIF = 'image/gif'[source]
IMAGE_PNG = 'image/png'[source]
IMAGE_TIFF = 'image/tiff'[source]
MULTI_PART_FORM = 'multipart/form-data'[source]
TEXT_ENRICHED = 'text/enriched'[source]
TEXT_HTML = 'text/html'[source]
TEXT_PLAIN = 'text/plain'[source]
TEXT_RICHTEXT = 'text/richtext'[source]
TEXT_XML = 'text/xml'[source]
VIDEO_MPEG = 'video/mpeg'[source]
ANY_JSON[source]
ANY_CWL[source]
ANY_XML[source]
ANY = '*/*'[source]
class weaver.formats.ContentEncoding[source]

Supported Content-Encoding values.

Note

Value binary is kept for convenience and backward compatibility with older definitions. It will default to the same encoding strategy as if base64 was specified explicitly. Value binary is not part of RFC 4648, but remains a common occurrence that dates from when format: binary was the approach employed to represent binary (JSON-schema Draft-04 and prior) instead of what is now recommended using contentEncoding: base64 (JSON-schema Draft-07).

UTF_8 = 'UTF-8'[source]
BINARY = 'binary'[source]
BASE16 = 'base16'[source]
BASE32 = 'base32'[source]
BASE64 = 'base64'[source]
static is_text(encoding: Any) bool[source]

Indicates if the Content-Encoding value can be categorized as textual data.

static is_binary(encoding: Any) bool[source]

Indicates if the Content-Encoding value can be categorized as binary data.

static open_parameters(encoding: Any, mode: FileModeSteamType = 'r') Tuple[FileModeEncoding, typing_extensions.Literal[UTF-8, None]][source]

Obtains relevant mode and encoding parameters for open() using the specified Content-Encoding.

static encode(data: AnyStr, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[True] = True) bytes[source]
static encode(data: AnyStr, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[False] = False) str
static encode(data: DataStrT, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[None] = None) DataStrT

Encodes the data to the requested encoding and convert it to the string-like data type representation.

Parameters:
  • data – Data to encode.

  • encoding – Target encoding method.

  • binary – If unspecified, the string-like type will be the same as the input data. Otherwise, convert the encoded data to str or bytes accordingly.

Returns:

Encoded and converted data.

static decode(data: AnyStr, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[True] = True) bytes[source]
static decode(data: AnyStr, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[False] = False) str
static decode(data: DataStrT, encoding: ContentEncoding = BASE64, binary: typing_extensions.Literal[None] = None) DataStrT

Decodes the data from the specified encoding and convert it to the string-like data type representation.

Parameters:
  • data – Data to decode.

  • encoding – Expected source encoding.

  • binary – If unspecified, the string-like type will be the same as the input data. Otherwise, convert the decoded data to str or bytes accordingly.

Returns:

Decoded and converted data.

class weaver.formats.OutputFormat[source]

Renderer output formats for CLI, OpenAPI and HTTP response content generation.

JSON[source]
JSON_STR[source]
JSON_RAW[source]
YAML[source]
YML[source]
XML[source]
XML_STR[source]
XML_RAW[source]
TXT[source]
TEXT[source]
classmethod get(format_or_version: str | AnyOutputFormat | weaver.base.PropertyDataTypeT, default: AnyOutputFormat = JSON, allow_version: bool = True) AnyOutputFormat | weaver.base.PropertyDataTypeT[source]

Resolve the applicable output format.

Parameters:
  • format_or_version – Either a WPS version, a known value for a f/format query parameter, or an Accept header that can be mapped to one of the supported output formats.

  • default – Default output format if none could be resolved.

  • allow_version – Enable WPS version specifiers to infer the corresponding output representation.

Returns:

Resolved output format.

classmethod convert(data: weaver.typedefs.JSON, to: AnyOutputFormat | str, item_root: str = 'item') str | weaver.typedefs.JSON[source]

Converts the input data from JSON to another known format.

Parameters:
  • data – Input data to convert. Must be a literal JSON object, not a JSON-like string.

  • to – Target format representation. If the output format is not JSON, it is ALWAYS converted to the formatted string of the requested format to ensure the contents are properly represented as intended. In the case of JSON as target format or unknown format, the original object is returned directly.

  • item_root – When using XML representations, defines the top-most item name. Unused for other representations.

Returns:

Formatted output.

class weaver.formats.SchemaRole[source]

Constants container that provides similar functionalities to ExtendedEnum without explicit Enum membership.

JSON_SCHEMA = 'https://www.w3.org/2019/wot/json-schema'[source]
weaver.formats._CONTENT_TYPE_EXTENSION_OVERRIDES[source]
weaver.formats._CONTENT_TYPE_EXCLUDE[source]
weaver.formats._EXTENSION_CONTENT_TYPES_OVERRIDES[source]
weaver.formats._CONTENT_TYPE_EXTENSION_MAPPING: Dict[str, str][source]
weaver.formats._CONTENT_TYPE_FORMAT_MAPPING: Dict[str, pywps.inout.formats.Format][source]
weaver.formats._CONTENT_TYPE_EXT_PATTERN[source]
weaver.formats._CONTENT_TYPE_LOCALS_MISSING[source]
weaver.formats._CONTENT_TYPE_LOCALS_MISSING[source]
weaver.formats._EXTENSION_CONTENT_TYPES_MAPPING[source]
weaver.formats._CONTENT_TYPE_CHAR_TYPES = ['application', 'multipart', 'text'][source]
weaver.formats._CONTENT_TYPE_SYNONYM_MAPPING[source]
weaver.formats.IANA_NAMESPACE = 'iana'[source]
weaver.formats.IANA_NAMESPACE_URL = 'https://www.iana.org/assignments/media-types/'[source]
weaver.formats.IANA_NAMESPACE_DEFINITION[source]
weaver.formats.IANA_KNOWN_MEDIA_TYPES[source]
weaver.formats.IANA_MAPPING[source]
weaver.formats.EDAM_NAMESPACE = 'edam'[source]
weaver.formats.EDAM_NAMESPACE_URL = 'http://edamontology.org/'[source]
weaver.formats.EDAM_NAMESPACE_DEFINITION[source]
weaver.formats.EDAM_SCHEMA = 'http://edamontology.org/EDAM_1.24.owl'[source]
weaver.formats.EDAM_MAPPING[source]
weaver.formats.OPENGIS_NAMESPACE = 'opengis'[source]
weaver.formats.OPENGIS_NAMESPACE_URL = 'http://www.opengis.net/'[source]
weaver.formats.OPENGIS_NAMESPACE_DEFINITION[source]
weaver.formats.OPENGIS_MAPPING[source]
weaver.formats.OGC_NAMESPACE = 'ogc'[source]
weaver.formats.OGC_NAMESPACE_URL[source]
weaver.formats.OGC_NAMESPACE_DEFINITION[source]
weaver.formats.OGC_MAPPING[source]
weaver.formats.FORMAT_NAMESPACE_MAPPINGS[source]
weaver.formats.FORMAT_NAMESPACE_DEFINITIONS[source]
weaver.formats.FORMAT_NAMESPACE_PREFIXES[source]
weaver.formats.FORMAT_NAMESPACES[source]
weaver.formats.get_allowed_extensions() List[str][source]

Obtain the complete list of extensions that are permitted for processing by the application.

Note

This is employed for security reasons. Files can still be specified with another allowed extension, but it will not automatically inherit properties applicable to scripts and executables. If a specific file type is refused due to its extension, a PR can be submitted to add it explicitly.

weaver.formats.get_format(media_type: str, default: str | None = None) pywps.inout.formats.Format | None[source]

Obtains a Format with predefined extension and encoding details from known media-types.

weaver.formats.get_extension(media_type: str, dot: bool = True) str[source]

Retrieves the extension corresponding to media_type if explicitly defined, or by parsing it.

weaver.formats.get_content_type(extension: str, charset: str | None = None, default: str | None = None) str | None[source]

Retrieves the Content-Type corresponding to the specified extension if it can be matched.

Parameters:
  • extension – Extension for which to attempt finding a known Content-Type.

  • charset – Charset to apply to the Content-Type as needed if extension was matched.

  • default – Default Content-Type to return if no extension is matched.

Returns:

Matched or default Content-Type.

weaver.formats.add_content_type_charset(content_type: str | ContentType, charset: str | None) str[source]

Apply the specific charset to the content-type with some validation in case of conflicting definitions.

Parameters:
  • content_type – Desired Content-Type.

  • charset – Desired charset parameter.

Returns:

updated content-type with charset.

weaver.formats.get_cwl_file_format(media_type: str, make_reference: bool = False, must_exist: bool = True, allow_synonym: bool = True) Tuple[weaver.typedefs.JSON | None, str | None] | str | None[source]

Obtains the extended schema reference from the media-type identifier.

Obtains the corresponding IANA/EDAM/etc. format value to be applied under a CWL I/O File from the media_type (Content-Type header) using the first matched one.

Lookup procedure is as follows:

  • If make_reference=False:
    • If there is a match, returns tuple({<namespace-name: namespace-url>}, <format>) with:
      1. corresponding namespace mapping to be applied under $namespaces in the CWL.

      2. value of format adjusted according to the namespace to be applied to File in the CWL.

    • If there is no match but must_exist=False, returns a literal and non-existing definition as tuple({"iana": <iana-url>}, <format>).

    • If there is no match but must_exist=True AND allow_synonym=True, retry the call with the synonym if available, or move to next step. Skip this step if allow_synonym=False.

    • Otherwise, returns (None, None)

  • If make_reference=True:
    • If there is a match, returns the explicit format reference as <namespace-url>/<format>.

    • If there is no match but must_exist=False, returns the literal reference as <iana-url>/<format> (N.B.: literal non-official media-type reference will be returned even if an official synonym exists).

    • If there is no match but must_exist=True AND allow_synonym=True, retry the call with the synonym if available, or move to next step. Skip this step if allow_synonym=False.

    • Returns a single None as there is no match (directly or synonym).

Note:

In situations where must_exist=False is used and that the namespace and/or full format URL cannot be resolved to an existing reference, CWL will raise a validation error as it cannot confirm the format. You must therefore make sure that the returned reference (or a synonym format) really exists when using must_exist=False before providing it to the CWL I/O definition. Setting must_exist=False should be used only for literal string comparison or pre-processing steps to evaluate formats.

Parameters:
  • media_type – Some reference, namespace’d or literal (possibly extended) media-type string.

  • make_reference – Construct the full URL reference to the resolved media-type. Otherwise, return tuple details.

  • must_exist – Return result only if it can be resolved to an official media-type (or synonym if enabled), otherwise None. Non-official media-type can be enforced if disabled, in which case IANA namespace/URL is used as it preserves the original <type>/<subtype> format.

  • allow_synonym – Allow resolution of non-official media-type to an official media-type synonym if available. Types defined as synonym have semantically the same format validation/resolution for CWL. Requires must_exist=True, otherwise the non-official media-type is employed directly as result.

Returns:

Resolved media-type format for CWL usage, accordingly to specified arguments (see description details).

weaver.formats.map_cwl_media_type(cwl_format: str | None) str | None[source]

Obtains the Media-Type that corresponds to the specified CWL format.

Parameters:

cwl_format – Long form URL or namespaced variant of a CWL format referring to an ontology Media-Type.

Returns:

Resolved Media-Type.

weaver.formats.clean_media_type_format(media_type: str, suffix_subtype: bool = False, strip_parameters: bool = False) str | None[source]

Obtains a generic media-type identifier by cleaning up any additional parameters.

Removes any additional namespace key or URL from media_type so that it corresponds to the generic representation (e.g.: application/json) instead of the <namespace-name>:<format> mapping variant used in CWL->inputs/outputs->File->format or the complete URL reference.

Removes any leading temporary local file prefix inserted by CWL when resolving namespace mapping. This transforms file:///tmp/dir/path/package#application/json to plain application/json.

According to provided arguments, it also cleans up additional parameters or extracts sub-type suffixes.

Parameters:
  • media_type – Media-Type, full URL to media-type or namespace-formatted string that must be cleaned up.

  • suffix_subtype – Remove additional sub-type specializations details separated by + symbol such that an explicit format like application/vnd.api+json returns only its most basic suffix format defined as``application/json``.

  • strip_parameters – Removes additional media-type parameters such that only the leading part defining the type/subtype are returned. For example, this will get rid of ; charset=UTF-8 or ; version=4.0 parameters.

Note

Parameters suffix_subtype and strip_parameters are not necessarily exclusive.

weaver.formats.guess_target_format(request: weaver.typedefs.AnyRequestType, default: ContentType | str | None = ContentType.APP_JSON) ContentType[source]

Guess the best applicable response Content-Type header from the request.

Considers the request Accept header, format query and alternatively f query to parse possible formats. Full Media-Type are expected in the header. Query parameters can use both the full Media-Type, or only the sub-type (i.e.: JSON, XML, etc.), with case-insensitive names.

Defaults to ContentType.APP_JSON if none was specified as default explicitly and that no Accept` header or ``format/f queries were provided. Otherwise, applies the specified default format specifiers were not provided in the request.

Applies some specific logic to handle automatically added Accept headers by many browsers such that sending requests to the API using them will not automatically default back to XML or similar HTML representations. If browsers are used to send requests, but that format/f queries are used directly in the URL, those will be applied since this is a very intuitive (and easier) approach to request different formats when using browsers.

When User-Agent clients are identified as another source, such as sending requests from a server or from code, both headers and query parameters are applied directly without question.

Returns:

Matched media-type or default.

weaver.formats.json_default_handler(obj: Any) weaver.typedefs.JSON | str | None[source]
weaver.formats.repr_json(data: Any, force_string: bool = True, ensure_ascii: bool = False, indent: int | None = 2, **kwargs: Any) weaver.typedefs.JSON | str | None[source]

Ensure that the input data can be serialized as JSON to return it formatted representation as such.

If formatting as JSON fails, returns the data as string representation or None accordingly.

weaver.formats.AnyOutputFormat[source]