From 01b0fe1138ba226d41efacb263f0369e78cbe0aa Mon Sep 17 00:00:00 2001 From: MacPingu Date: Fri, 17 Sep 2021 15:55:21 +0200 Subject: [PATCH] added process to show usage of defaults (#114) * added process to show usage of defaults --- emu/processes/__init__.py | 2 + emu/processes/wps_show_defaults.py | 80 ++++++++++++++++++++++++++++++ environment.yml | 2 +- requirements.txt | 3 +- tests/test.cfg | 3 ++ tests/test_wps_caps.py | 1 + tests/test_wps_show_defaults.py | 50 +++++++++++++++++++ 7 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 emu/processes/wps_show_defaults.py create mode 100644 tests/test_wps_show_defaults.py diff --git a/emu/processes/__init__.py b/emu/processes/__init__.py index 13522f9..43fee7d 100644 --- a/emu/processes/__init__.py +++ b/emu/processes/__init__.py @@ -20,6 +20,7 @@ from .wps_translation import Translation from .wps_geodata import GeoData from .wps_pandas import Pandas +from .wps_show_defaults import ShowDefaults processes = [ @@ -45,4 +46,5 @@ Translation(), GeoData(), Pandas(), + ShowDefaults(), ] diff --git a/emu/processes/wps_show_defaults.py b/emu/processes/wps_show_defaults.py new file mode 100644 index 0000000..7c1d0a8 --- /dev/null +++ b/emu/processes/wps_show_defaults.py @@ -0,0 +1,80 @@ +import json + +from pywps import Process, LiteralInput, ComplexOutput +from pywps import FORMATS + +import logging +LOGGER = logging.getLogger("PYWPS") + + +class ShowDefaults(Process): + """ + Process with examples of default value usage in WPS. + """ + def __init__(self): + inputs = [ + LiteralInput( + 'string_0', + 'String 0: optional, no default', + data_type='string', + min_occurs=0 + ), + LiteralInput( + 'string_1', + 'String 1: optional with default', + data_type='string', + default='one', + min_occurs=0 + ), + LiteralInput( + 'string_2', + 'String 2: required with default', + data_type='string', + default='two', + min_occurs=1 + ), + LiteralInput( + 'string_3', + 'String 3: required no default', + data_type='string', + default='three', + min_occurs=1 + ), + ] + outputs = [ + ComplexOutput( + 'output', + 'Output', + as_reference=True, + supported_formats=[FORMATS.JSON]), + ] + + super(ShowDefaults, self).__init__( + self._handler, + identifier='show_defaults', + title="Show defaults", + abstract="Show usage of default values in WPS", + version="1.0", + inputs=inputs, + outputs=outputs, + store_supported=True, + status_supported=True + ) + + @staticmethod + def _handler(request, response): + response.update_status('PyWPS Process started.', 0) + + if 'string_0' in request.inputs: + string0 = request.inputs['string_0'][0].data + else: + string0 = "no value" + + response.outputs['output'].data = json.dumps({ + 'string0': string0, + 'string1': request.inputs['string_1'][0].data, + 'string2': request.inputs['string_2'][0].data, + 'string3': request.inputs['string_3'][0].data, + }) + response.update_status('PyWPS Process completed.', 100) + return response diff --git a/environment.yml b/environment.yml index bc8bcef..0c98fa0 100644 --- a/environment.yml +++ b/environment.yml @@ -5,7 +5,7 @@ channels: dependencies: - pip - python>=3.6 -- pywps>=4.4.0 +- pywps>=4.4.5,<4.5 - jinja2 - click - psutil diff --git a/requirements.txt b/requirements.txt index a788da1..e8fddab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,7 @@ -pywps>=4.4.0 +pywps>=4.4.5 jinja2 click psutil -defusedxml geomet xarray netCDF4 diff --git a/tests/test.cfg b/tests/test.cfg index f092411..b9fbbea 100644 --- a/tests/test.cfg +++ b/tests/test.cfg @@ -1,3 +1,6 @@ [server] allowedinputpaths=/ language = en-US,fr-CA,de-DE + +[logging] +level=DEBUG diff --git a/tests/test_wps_caps.py b/tests/test_wps_caps.py index b7f5258..ac340ec 100644 --- a/tests/test_wps_caps.py +++ b/tests/test_wps_caps.py @@ -29,6 +29,7 @@ def test_wps_caps(): 'output_formats', 'pandas', 'poly_centroid', + 'show_defaults', 'show_error', 'simple_dry_run', 'sleep', diff --git a/tests/test_wps_show_defaults.py b/tests/test_wps_show_defaults.py new file mode 100644 index 0000000..4d51971 --- /dev/null +++ b/tests/test_wps_show_defaults.py @@ -0,0 +1,50 @@ +import json + +from pywps import Service +from pywps import get_ElementMakerForVersion + +from .common import client_for +from emu.processes.wps_show_defaults import ShowDefaults + +VERSION = "1.0.0" + +WPS, OWS = get_ElementMakerForVersion(VERSION) + + +def test_wps_show_defaults_post(): + client = client_for(Service(processes=[ShowDefaults()])) + request_doc = WPS.Execute( + OWS.Identifier('show_defaults'), + WPS.DataInputs( + WPS.Input( + OWS.Identifier('string_0'), + WPS.Data(WPS.LiteralData("ZERO")) + ), + # WPS.Input( + # OWS.Identifier('string_1'), + # WPS.Data(WPS.LiteralData("ONE")) + # ), + # WPS.Input( + # OWS.Identifier('string_2'), + # WPS.Data(WPS.LiteralData("TWO")) + # ), + WPS.Input( + OWS.Identifier('string_3'), + WPS.Data(WPS.LiteralData("THREE")) + ) + ), + WPS.ResponseForm( + WPS.RawDataOutput( + OWS.Identifier('output') + ) + ), + version='1.0.0' + ) + resp = client.post_xml(doc=request_doc) + print(resp.data) + assert resp.status_code == 200 + result = json.loads(resp.data) + assert result['string0'] == 'ZERO' + assert result['string1'] == 'one' + assert result['string2'] == 'two' + assert result['string3'] == 'THREE'