Skip to content

Commit

Permalink
[wip] apply resolution of collection files for complex process input
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Aug 23, 2024
1 parent b504128 commit dd0c8c7
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 41 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pyramid_celery @ git+https://github.com/crim-ca/pyramid_celery.git@5.0.0a
pyramid_mako
pyramid_rewrite
pyramid_storage
pystac
pystac_client
python-box
python-dateutil
Expand Down
36 changes: 35 additions & 1 deletion weaver/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import enum
import inspect
import json
import os
import re
import shutil
import tempfile
import traceback
import uuid
import warnings
Expand Down Expand Up @@ -44,6 +47,7 @@
from weaver.store.base import StoreProcesses
from weaver.utils import localize_datetime # for backward compatibility of previously saved jobs not time-locale-aware
from weaver.utils import (
LoggerHandler,
VersionFormat,
apply_number_with_unit,
as_version_major_minor_patch,
Expand Down Expand Up @@ -90,6 +94,7 @@
Link,
Metadata,
Number,
Path,
Price,
QuoteProcessParameters,
QuoteProcessResults,
Expand Down Expand Up @@ -616,7 +621,7 @@ def check_accessible(self, settings, ignore=True):
return False


class Job(Base):
class Job(Base, LoggerHandler):
"""
Dictionary that contains :term:`Job` details for local :term:`Process` or remote :term:`OWS` execution.
Expand All @@ -630,6 +635,24 @@ def __init__(self, *args, **kwargs):
raise TypeError(f"Parameter 'task_id' is required for '{self.__name__}' creation.")
if not isinstance(self.id, (str, uuid.UUID)):
raise TypeError(f"Type 'str' or 'UUID' is required for '{self.__name__}.id'")
self["__tmpdir"] = None

def cleanup(self):
if self["__tmpdir"] and os.path.isdir(self["__tmpdir"]):
shutil.rmtree(self["__tmpdir"], ignore_errors=True)

@property
def tmpdir(self):
# type: () -> Path
"""
Optional temporary directory available for the :term:`Job` to store files needed for its operation.
It is up to the caller to remove the contents by calling :meth:`cleanup`.
"""
_tmpdir = self.get("__tmpdir")
if not _tmpdir:
_tmpdir = self["__tmpdir"] = tempfile.mkdtemp()
return _tmpdir

@staticmethod
def _get_message(message, size_limit=None):
Expand All @@ -654,7 +677,18 @@ def _get_err_msg(error, size_limit=None):
error_msg = Job._get_message(error.text, size_limit=size_limit)
return f"{error_msg} - code={error.code} - locator={error.locator}"

def log(self, level, message, *args, **kwargs):
# type: (AnyLogLevel, str, *str, **Any) -> None
"""
Provides the :class:`LoggerHandler` interface, allowing to pass the :term:`Job` directly as a logger reference.
The same parameters as :meth:`save_log` can be provided.
"""
message = message.format(*args, **kwargs)
return self.save_log(level=level, message=message, **kwargs)

def save_log(self,
*,
errors=None, # type: Optional[Union[str, Exception, WPSException, List[WPSException]]]
logger=None, # type: Optional[Logger]
message=None, # type: Optional[str]
Expand Down
11 changes: 11 additions & 0 deletions weaver/formats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import datetime
import functools
import json
import logging
import os
Expand Down Expand Up @@ -105,6 +106,7 @@ class ContentType(Constants):
APP_ZIP = "application/zip"
IMAGE_GEOTIFF = "image/tiff; subtype=geotiff"
IMAGE_OGC_GEOTIFF = "image/tiff; application=geotiff"
IMAGE_COG = "image/tiff; application=geotiff; profile=cloud-optimized"
IMAGE_JPEG = "image/jpeg"
IMAGE_GIF = "image/gif"
IMAGE_PNG = "image/png"
Expand Down Expand Up @@ -603,6 +605,7 @@ class SchemaRole(Constants):
OGC_MAPPING = {
ContentType.IMAGE_GEOTIFF: "geotiff",
ContentType.IMAGE_OGC_GEOTIFF: "geotiff",
ContentType.IMAGE_COG: "geotiff",
ContentType.APP_NETCDF: "netcdf",
}
FORMAT_NAMESPACE_MAPPINGS = {
Expand All @@ -623,6 +626,7 @@ class SchemaRole(Constants):
FORMAT_NAMESPACES = frozenset(FORMAT_NAMESPACE_DEFINITIONS)


@functools.cache
def get_allowed_extensions():
# type: () -> List[str]
"""
Expand All @@ -649,6 +653,7 @@ def get_allowed_extensions():
return list(base | extra)


@functools.cache
def get_format(media_type, default=None):
# type: (str, Optional[str]) -> Optional[Format]
"""
Expand All @@ -668,6 +673,7 @@ def get_format(media_type, default=None):
return fmt


@functools.cache
def get_extension(media_type, dot=True):
# type: (str, bool) -> str
"""
Expand Down Expand Up @@ -697,6 +703,7 @@ def _handle_dot(_ext):
return _handle_dot(ext)


@functools.cache
def get_content_type(extension, charset=None, default=None):
# type: (str, Optional[str], Optional[str]) -> Optional[str]
"""
Expand All @@ -721,6 +728,7 @@ def get_content_type(extension, charset=None, default=None):
return add_content_type_charset(ctype, charset)


@functools.cache
def add_content_type_charset(content_type, charset):
# type: (Union[str, ContentType], Optional[str]) -> str
"""
Expand All @@ -739,6 +747,7 @@ def add_content_type_charset(content_type, charset):
return content_type


@functools.cache
def get_cwl_file_format(media_type, make_reference=False, must_exist=True, allow_synonym=True): # pylint: disable=R1260
# type: (str, bool, bool, bool) -> Union[Tuple[Optional[JSON], Optional[str]], Optional[str]]
"""
Expand Down Expand Up @@ -860,6 +869,7 @@ def _request_extra_various(_media_type):
return None if make_reference else (None, None)


@functools.cache
def map_cwl_media_type(cwl_format):
# type: (Optional[str]) -> Optional[str]
"""
Expand Down Expand Up @@ -891,6 +901,7 @@ def map_cwl_media_type(cwl_format):
return ctype


@functools.cache
def clean_media_type_format(media_type, suffix_subtype=False, strip_parameters=False):
# type: (str, bool, bool) -> Optional[str]
"""
Expand Down
Loading

0 comments on commit dd0c8c7

Please sign in to comment.