From c393df27b483b5cf9253e9ef1b47d85c9d660627 Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Thu, 21 Dec 2023 15:43:09 +0100 Subject: [PATCH 1/4] add create_actinia_pc_item function to utils --- src/actinia/utils.py | 85 ++++++++++++++++++++++++++++++++++ tests/test_utils.py | 107 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 tests/test_utils.py diff --git a/src/actinia/utils.py b/src/actinia/utils.py index 8a10a6c..60e7139 100644 --- a/src/actinia/utils.py +++ b/src/actinia/utils.py @@ -52,6 +52,7 @@ def request_and_check(url, auth, status_code=200): def set_job_names(name, default_name="unknown_job"): + """Function to set the date/time to the job name""" now = datetime.now() if name is None: orig_name = default_name @@ -60,3 +61,87 @@ def set_job_names(name, default_name="unknown_job"): orig_name = name name += f"_{now.strftime('%Y%d%m_%H%M%S')}" return orig_name, name + + +def create_actinia_pc_item( + id, + module, + inputs=None, + outputs=None, + flags=None, + stdin=None, + stdout=None, + overwrite=False, + superquiet=False, + verbose=False, + interface_description=False + ): + """ + Creates a list item for an actinia process chain + + Parameters + ---------- + id: str + unique id for this item + module: str + some valid GRASS or actinia module + inputs: list or dict + list of input parameters with values in the form + [{"param": key1, "value": value1}, {"param": key2, "value": value2}, ...] + shorter alternative as dict + {"key1": value1, "key2": value2, ...} + outputs: list or dict + list of output parameters with values in the form + [{"param": key1, "value": value1}, {"param": key2, "value": value2}, ...] + shorter alternative as dict + {"key1": value1, "key2": value2, ...} + flags: str + optional flags for the module + stdin: dict + options to read stdin + stdout: dict + options to write to stdout + must be of the form + {"id": value1, "format": value2, "delimiter": value3} + overwrite: bool + optional, set to True to allow overwriting existing data + superquiet: bool + optional, set to True to suppress all messages but errors + verbose: bool + optional, set to True to allow verbose messages + interface_description: bool + optional, set to True to create an interface_description + """ + pc_item = {"id": str(id), "module": module} + if inputs: + if isinstance(inputs, list): + pc_item["inputs"] = inputs + elif isinstance(inputs, dict): + tmplist = [] + for k, v in inputs.items(): + tmplist.append({"param": k, "value": v}) + pc_item["inputs"] = tmplist + if outputs: + if isinstance(outputs, list): + pc_item["outputs"] = outputs + elif isinstance(outputs, dict): + tmplist = [] + for k, v in outputs.items(): + tmplist.append({"param": k, "value": v}) + pc_item["outputs"] = tmplist + if flags: + pc_item["flags"] = flags + if stdin: + pc_item["stdin"] = stdin + if stdout: + pc_item["stdout"] = stdout + if overwrite is True: + pc_item["overwrite"] = True + if superquiet is True: + pc_item["superquiet"] = True + if verbose is True: + pc_item["verbose"] = True + if interface_description is True: + pc_item["interface_description"] = True + + return pc_item diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..463163f --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +####### +# actinia-python-client is a python client for actinia - an open source REST +# API for scalable, distributed, high performance processing of geographical +# data that uses GRASS GIS for computational tasks. +# +# Copyright (c) 2023 mundialis GmbH & Co. KG +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +####### + +__license__ = "GPLv3" +__author__ = "Anika Weinmann, Markus Metz" +__copyright__ = "Copyright 2023, mundialis GmbH & Co. KG" +__maintainer__ = "Anika Weinmann" + +from actinia.utils import * +from .actinia_config import ACTINIA_BASEURL, ACTINIA_VERSION, ACTINIA_AUTH + + +class TestActiniaUtils(object): + + def test_request_and_check(self): + """Test request_and_check utils function.""" + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version" + resp = request_and_check(url, ACTINIA_AUTH, status_code=200) + assert "version" in resp + + def test_request_and_check_wrong_url(self): + """Test request_and_check utils function with .""" + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version_fail" + err_msg = "The requested URL was not found on the server." + with pytest.raises(Exception) as excinfo: + resp = request_and_check(url, ACTINIA_AUTH, status_code=200) + assert err_msg in str(excinfo.value) + + def test_request_and_check_wrong_auth(self): + """Test request_and_check utils function with wrong auth.""" + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/locations" + err_msg = "Unauthorized Access" + wrong_auth = ("actinia-gdi", "wrong_pw") + with pytest.raises(Exception) as excinfo: + resp = request_and_check(url, wrong_auth, status_code=200) + assert err_msg in str(excinfo.value) + + def test_set_job_names(self): + """Test set_job_names utils function.""" + def_name = "def_job_name" + name_to_set = "test" + orig_name, name = set_job_names(name_to_set, default_name=def_name) + assert name_to_set == orig_name + assert name_to_set in name + + def test_set_job_names_using_own_default(self): + """Test set_job_names utils function with using own default name.""" + def_name = "def_job_name" + name_to_set = None + orig_name, name = set_job_names(name_to_set, default_name=def_name) + assert def_name == orig_name + assert "job" in name + + def test_set_job_names_using_default(self): + """Test set_job_names utils function with using default name.""" + name_to_set = None + orig_name, name = set_job_names(name_to_set) + import pdb; pdb.set_trace() + assert "unknown_job" == orig_name + assert "job" in name + + def test_create_actinia_pc_item(self): + """Test set_job_names utils function with using default name.""" + module = "g.region" + id = "pc_item_ide" + inputs = { + "raster": "elevation@PERMANENT", + "res": "5000", + } + pc_item = create_actinia_pc_item( + id=id, + module="g.region", + inputs= inputs, + flags="g", + ) + assert "module" in pc_item + assert "id" in pc_item + assert "inputs" in pc_item + assert pc_item["module"] == module + assert pc_item["id"] == id + assert isinstance(pc_item["inputs"], list) + for inp in pc_item["inputs"]: + param = inp["param"] + value = inp["value"] + assert param in inputs + assert inputs[param] == value From 1e8204bc1ef853a5cc0214683d5e195c4587e877 Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Thu, 21 Dec 2023 15:44:38 +0100 Subject: [PATCH 2/4] linting and changelog --- CHANGELOG.md | 1 + src/actinia/utils.py | 24 ++++++++++++------------ tests/test_utils.py | 7 ++++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1062517..36dcdd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Types of changes ## [Unreleased] ### Added - persistent processing +- Add create_actinia_pc_item function to utils ## [0.3.2] - 2023-12-21 diff --git a/src/actinia/utils.py b/src/actinia/utils.py index 60e7139..d63dc81 100644 --- a/src/actinia/utils.py +++ b/src/actinia/utils.py @@ -64,18 +64,18 @@ def set_job_names(name, default_name="unknown_job"): def create_actinia_pc_item( - id, - module, - inputs=None, - outputs=None, - flags=None, - stdin=None, - stdout=None, - overwrite=False, - superquiet=False, - verbose=False, - interface_description=False - ): + id, + module, + inputs=None, + outputs=None, + flags=None, + stdin=None, + stdout=None, + overwrite=False, + superquiet=False, + verbose=False, + interface_description=False, +): """ Creates a list item for an actinia process chain diff --git a/tests/test_utils.py b/tests/test_utils.py index 463163f..883ad3a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -32,7 +32,6 @@ class TestActiniaUtils(object): - def test_request_and_check(self): """Test request_and_check utils function.""" url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version" @@ -76,7 +75,9 @@ def test_set_job_names_using_default(self): """Test set_job_names utils function with using default name.""" name_to_set = None orig_name, name = set_job_names(name_to_set) - import pdb; pdb.set_trace() + import pdb + + pdb.set_trace() assert "unknown_job" == orig_name assert "job" in name @@ -91,7 +92,7 @@ def test_create_actinia_pc_item(self): pc_item = create_actinia_pc_item( id=id, module="g.region", - inputs= inputs, + inputs=inputs, flags="g", ) assert "module" in pc_item From 658c635b38969bc691468b44f12bc95a432f2c9d Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Thu, 21 Dec 2023 15:49:01 +0100 Subject: [PATCH 3/4] fixes --- src/actinia/utils.py | 6 ++++-- tests/test_utils.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/actinia/utils.py b/src/actinia/utils.py index d63dc81..6411512 100644 --- a/src/actinia/utils.py +++ b/src/actinia/utils.py @@ -87,12 +87,14 @@ def create_actinia_pc_item( some valid GRASS or actinia module inputs: list or dict list of input parameters with values in the form - [{"param": key1, "value": value1}, {"param": key2, "value": value2}, ...] + [{"param": key1, "value": value1}, {"param": key2, "value": value2}, + ...] shorter alternative as dict {"key1": value1, "key2": value2, ...} outputs: list or dict list of output parameters with values in the form - [{"param": key1, "value": value1}, {"param": key2, "value": value2}, ...] + [{"param": key1, "value": value1}, {"param": key2, "value": value2}, + ...] shorter alternative as dict {"key1": value1, "key2": value2, ...} flags: str diff --git a/tests/test_utils.py b/tests/test_utils.py index 883ad3a..914db2d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -27,7 +27,13 @@ __copyright__ = "Copyright 2023, mundialis GmbH & Co. KG" __maintainer__ = "Anika Weinmann" -from actinia.utils import * +import pytest + +from actinia.utils import ( + create_actinia_pc_item, + request_and_check, + set_job_names, +) from .actinia_config import ACTINIA_BASEURL, ACTINIA_VERSION, ACTINIA_AUTH @@ -43,7 +49,7 @@ def test_request_and_check_wrong_url(self): url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version_fail" err_msg = "The requested URL was not found on the server." with pytest.raises(Exception) as excinfo: - resp = request_and_check(url, ACTINIA_AUTH, status_code=200) + request_and_check(url, ACTINIA_AUTH, status_code=200) assert err_msg in str(excinfo.value) def test_request_and_check_wrong_auth(self): @@ -52,7 +58,7 @@ def test_request_and_check_wrong_auth(self): err_msg = "Unauthorized Access" wrong_auth = ("actinia-gdi", "wrong_pw") with pytest.raises(Exception) as excinfo: - resp = request_and_check(url, wrong_auth, status_code=200) + request_and_check(url, wrong_auth, status_code=200) assert err_msg in str(excinfo.value) def test_set_job_names(self): From c0c912f56243db7601c5caa06bbd814912e3d6c9 Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Thu, 21 Dec 2023 15:58:04 +0100 Subject: [PATCH 4/4] fix --- tests/test_utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 914db2d..6db354b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -81,9 +81,6 @@ def test_set_job_names_using_default(self): """Test set_job_names utils function with using default name.""" name_to_set = None orig_name, name = set_job_names(name_to_set) - import pdb - - pdb.set_trace() assert "unknown_job" == orig_name assert "job" in name