Skip to content

grass.experimental: Add NumPy arrays IO to Tools #5878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d905882
grass.experimental: Add object to access modules as functions
wenzeslaus Apr 18, 2023
aaef183
Support verbosity, overwrite and region freezing
wenzeslaus Apr 21, 2023
54db575
Raise exception instead of calling handle_errors
wenzeslaus Apr 22, 2023
82f5894
Allow to specify stdin and use a new instance of Tools itself to exec…
wenzeslaus Apr 22, 2023
0f1e210
Add ignore errors, r_mapcalc example, draft tests
wenzeslaus Apr 22, 2023
f4e3fed
Add test for exceptions
wenzeslaus Apr 24, 2023
04087e8
Add tests and Makefile
wenzeslaus May 4, 2023
6ab8e40
Convert values to ints and floats in keyval
wenzeslaus May 4, 2023
744cfac
Do not overwrite by default to follow default behavior in GRASS GIS
wenzeslaus May 4, 2023
24c27e6
Add doc, remove old code and todos
wenzeslaus Jun 3, 2023
ff187a6
Add to top Makefile
wenzeslaus Jun 3, 2023
22773c8
Add docs for tests
wenzeslaus Jun 3, 2023
2911065
Allow test to fail because of the missing seed parameter (so results …
wenzeslaus Jun 4, 2023
3ac46c3
Merge branch 'main' into add-session-tools-object
echoix Nov 11, 2024
437d46e
Allow for optional output capture (error handling and printing still …
wenzeslaus Apr 23, 2025
cb8f483
Merge branch 'main' into add-session-tools-object
wenzeslaus Apr 23, 2025
a958142
Merge remote-tracking branch 'upstream/main' into add-session-tools-o…
wenzeslaus Apr 25, 2025
61972d4
Access JSON as dict directly without an attribute using getitem. Sugg…
wenzeslaus Apr 25, 2025
c86d8ff
Fix whitespace and regexp
wenzeslaus Apr 25, 2025
3b995c9
Represent not captured stdout as None, not empty string.
wenzeslaus Apr 25, 2025
d8c354d
Merge remote-tracking branch 'upstream/main' into add-session-tools-o…
wenzeslaus Apr 29, 2025
4cc5a32
Add run subcommand to have a CLI use case for the tools. It runs one …
wenzeslaus Apr 29, 2025
459b2ad
Update function name
wenzeslaus Apr 30, 2025
513c9f8
Add prototype code for numpy support
wenzeslaus Jun 2, 2025
24ef6b9
Merge main branch
wenzeslaus Jun 2, 2025
4a1e374
Make the special features standalone objects used by composition
wenzeslaus Jun 11, 2025
651df11
Merge remote-tracking branch 'upstream/main' into add-session-tools-o…
wenzeslaus Jun 11, 2025
41ad7ae
Remove r.pack IO
wenzeslaus Jun 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion python/grass/app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,30 @@
import tempfile
import os
import sys
import subprocess
from pathlib import Path


import grass.script as gs
from grass.app.data import lock_mapset, unlock_mapset, MapsetLockingException
from grass.experimental.tools import Tools


def subcommand_run_tool(args, tool_args: list, help: bool):
command = [args.tool_name, *tool_args]
with tempfile.TemporaryDirectory() as tmp_dir_name:
project_name = "project"
project_path = Path(tmp_dir_name) / project_name
gs.create_project(project_path)
with gs.setup.init(project_path) as session:
if help:
result = subprocess.run(command, env=session.env)
return result.returncode
tools = Tools(capture_output=False)
try:
tools.run_from_list(command)
except subprocess.CalledProcessError as error:
return error.returncode


def subcommand_lock_mapset(args):
Expand Down Expand Up @@ -77,6 +97,10 @@ def main(args=None, program=None):

# Subcommand parsers

subparser = subparsers.add_parser("run", help="run a tool")
subparser.add_argument("tool_name", type=str)
subparser.set_defaults(func=subcommand_run_tool)

subparser = subparsers.add_parser("lock", help="lock a mapset")
subparser.add_argument("mapset_path", type=str)
subparser.add_argument(
Expand Down Expand Up @@ -120,5 +144,27 @@ def main(args=None, program=None):
subparser.add_argument("page", type=str)
subparser.set_defaults(func=subcommand_show_man)

parsed_args = parser.parse_args(args)
# Parsing

if not args:
args = sys.argv[1:]
raw_args = args.copy()
add_back = None
if len(raw_args) > 2 and raw_args[0] == "run":
# Getting the --help of tools needs to work around the standard help mechanism
# of argparse.
# Maybe a better workaround is to use custom --help, action="help", print_help,
# and dedicated tool help function complimentary with g.manual subcommand
# interface.
if "--help" in raw_args[2:]:
raw_args.remove("--help")
add_back = "--help"
elif "--h" in raw_args[2:]:
raw_args.remove("--h")
add_back = "--h"
parsed_args, other_args = parser.parse_known_args(raw_args)
if parsed_args.subcommand == "run":
if add_back:
other_args.append(add_back)
return parsed_args.func(parsed_args, other_args, help=bool(add_back))
return parsed_args.func(parsed_args)
3 changes: 2 additions & 1 deletion python/grass/experimental/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ DSTDIR = $(ETC)/python/grass/experimental

MODULES = \
create \
mapset
mapset \
tools

PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
Expand Down
8 changes: 8 additions & 0 deletions python/grass/experimental/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def xy_session_for_module(tmp_path_factory):
yield session


@pytest.fixture
def xy_dataset_session(tmp_path):
"""Creates a session with a mapset which has vector with a float column"""
gs.core._create_location_xy(tmp_path, "test") # pylint: disable=protected-access
with gs.setup.init(tmp_path / "test") as session:
yield session


@pytest.fixture
def unique_id():
"""A unique alphanumeric identifier"""
Expand Down
Loading
Loading