Skip to content

Commit 52ace4d

Browse files
Merge pull request #38 from actinia-org/create_actinia_pc_item
Add create_actinia_pc_item function to utils
2 parents 537f1ae + c0c912f commit 52ace4d

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Types of changes
1717
## [Unreleased]
1818
### Added
1919
- persistent processing
20+
- Add create_actinia_pc_item function to utils
2021

2122

2223
## [0.3.2] - 2023-12-21

src/actinia/utils.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def request_and_check(url, auth, status_code=200):
5252

5353

5454
def set_job_names(name, default_name="unknown_job"):
55+
"""Function to set the date/time to the job name"""
5556
now = datetime.now()
5657
if name is None:
5758
orig_name = default_name
@@ -60,3 +61,89 @@ def set_job_names(name, default_name="unknown_job"):
6061
orig_name = name
6162
name += f"_{now.strftime('%Y%d%m_%H%M%S')}"
6263
return orig_name, name
64+
65+
66+
def create_actinia_pc_item(
67+
id,
68+
module,
69+
inputs=None,
70+
outputs=None,
71+
flags=None,
72+
stdin=None,
73+
stdout=None,
74+
overwrite=False,
75+
superquiet=False,
76+
verbose=False,
77+
interface_description=False,
78+
):
79+
"""
80+
Creates a list item for an actinia process chain
81+
82+
Parameters
83+
----------
84+
id: str
85+
unique id for this item
86+
module: str
87+
some valid GRASS or actinia module
88+
inputs: list or dict
89+
list of input parameters with values in the form
90+
[{"param": key1, "value": value1}, {"param": key2, "value": value2},
91+
...]
92+
shorter alternative as dict
93+
{"key1": value1, "key2": value2, ...}
94+
outputs: list or dict
95+
list of output parameters with values in the form
96+
[{"param": key1, "value": value1}, {"param": key2, "value": value2},
97+
...]
98+
shorter alternative as dict
99+
{"key1": value1, "key2": value2, ...}
100+
flags: str
101+
optional flags for the module
102+
stdin: dict
103+
options to read stdin
104+
stdout: dict
105+
options to write to stdout
106+
must be of the form
107+
{"id": value1, "format": value2, "delimiter": value3}
108+
overwrite: bool
109+
optional, set to True to allow overwriting existing data
110+
superquiet: bool
111+
optional, set to True to suppress all messages but errors
112+
verbose: bool
113+
optional, set to True to allow verbose messages
114+
interface_description: bool
115+
optional, set to True to create an interface_description
116+
"""
117+
pc_item = {"id": str(id), "module": module}
118+
if inputs:
119+
if isinstance(inputs, list):
120+
pc_item["inputs"] = inputs
121+
elif isinstance(inputs, dict):
122+
tmplist = []
123+
for k, v in inputs.items():
124+
tmplist.append({"param": k, "value": v})
125+
pc_item["inputs"] = tmplist
126+
if outputs:
127+
if isinstance(outputs, list):
128+
pc_item["outputs"] = outputs
129+
elif isinstance(outputs, dict):
130+
tmplist = []
131+
for k, v in outputs.items():
132+
tmplist.append({"param": k, "value": v})
133+
pc_item["outputs"] = tmplist
134+
if flags:
135+
pc_item["flags"] = flags
136+
if stdin:
137+
pc_item["stdin"] = stdin
138+
if stdout:
139+
pc_item["stdout"] = stdout
140+
if overwrite is True:
141+
pc_item["overwrite"] = True
142+
if superquiet is True:
143+
pc_item["superquiet"] = True
144+
if verbose is True:
145+
pc_item["verbose"] = True
146+
if interface_description is True:
147+
pc_item["interface_description"] = True
148+
149+
return pc_item

tests/test_utils.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#######
4+
# actinia-python-client is a python client for actinia - an open source REST
5+
# API for scalable, distributed, high performance processing of geographical
6+
# data that uses GRASS GIS for computational tasks.
7+
#
8+
# Copyright (c) 2023 mundialis GmbH & Co. KG
9+
#
10+
# This program is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation, either version 3 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# This program is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
#
23+
#######
24+
25+
__license__ = "GPLv3"
26+
__author__ = "Anika Weinmann, Markus Metz"
27+
__copyright__ = "Copyright 2023, mundialis GmbH & Co. KG"
28+
__maintainer__ = "Anika Weinmann"
29+
30+
import pytest
31+
32+
from actinia.utils import (
33+
create_actinia_pc_item,
34+
request_and_check,
35+
set_job_names,
36+
)
37+
from .actinia_config import ACTINIA_BASEURL, ACTINIA_VERSION, ACTINIA_AUTH
38+
39+
40+
class TestActiniaUtils(object):
41+
def test_request_and_check(self):
42+
"""Test request_and_check utils function."""
43+
url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version"
44+
resp = request_and_check(url, ACTINIA_AUTH, status_code=200)
45+
assert "version" in resp
46+
47+
def test_request_and_check_wrong_url(self):
48+
"""Test request_and_check utils function with ."""
49+
url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version_fail"
50+
err_msg = "The requested URL was not found on the server."
51+
with pytest.raises(Exception) as excinfo:
52+
request_and_check(url, ACTINIA_AUTH, status_code=200)
53+
assert err_msg in str(excinfo.value)
54+
55+
def test_request_and_check_wrong_auth(self):
56+
"""Test request_and_check utils function with wrong auth."""
57+
url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/locations"
58+
err_msg = "Unauthorized Access"
59+
wrong_auth = ("actinia-gdi", "wrong_pw")
60+
with pytest.raises(Exception) as excinfo:
61+
request_and_check(url, wrong_auth, status_code=200)
62+
assert err_msg in str(excinfo.value)
63+
64+
def test_set_job_names(self):
65+
"""Test set_job_names utils function."""
66+
def_name = "def_job_name"
67+
name_to_set = "test"
68+
orig_name, name = set_job_names(name_to_set, default_name=def_name)
69+
assert name_to_set == orig_name
70+
assert name_to_set in name
71+
72+
def test_set_job_names_using_own_default(self):
73+
"""Test set_job_names utils function with using own default name."""
74+
def_name = "def_job_name"
75+
name_to_set = None
76+
orig_name, name = set_job_names(name_to_set, default_name=def_name)
77+
assert def_name == orig_name
78+
assert "job" in name
79+
80+
def test_set_job_names_using_default(self):
81+
"""Test set_job_names utils function with using default name."""
82+
name_to_set = None
83+
orig_name, name = set_job_names(name_to_set)
84+
assert "unknown_job" == orig_name
85+
assert "job" in name
86+
87+
def test_create_actinia_pc_item(self):
88+
"""Test set_job_names utils function with using default name."""
89+
module = "g.region"
90+
id = "pc_item_ide"
91+
inputs = {
92+
"raster": "elevation@PERMANENT",
93+
"res": "5000",
94+
}
95+
pc_item = create_actinia_pc_item(
96+
id=id,
97+
module="g.region",
98+
inputs=inputs,
99+
flags="g",
100+
)
101+
assert "module" in pc_item
102+
assert "id" in pc_item
103+
assert "inputs" in pc_item
104+
assert pc_item["module"] == module
105+
assert pc_item["id"] == id
106+
assert isinstance(pc_item["inputs"], list)
107+
for inp in pc_item["inputs"]:
108+
param = inp["param"]
109+
value = inp["value"]
110+
assert param in inputs
111+
assert inputs[param] == value

0 commit comments

Comments
 (0)