Skip to content
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

grass.script: Provide env parameter in the g.message API #3439

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Changes from all commits
Commits
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: 28 additions & 20 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def exec_command(
:param bool quiet: True to run quietly (<tt>--q</tt>)
:param bool superquiet: True to run quietly (<tt>--qq</tt>)
:param bool verbose: True to run verbosely (<tt>--v</tt>)
:param env: directory with environmental variables
:param env: dictionary with system environment variables (`os.environ` by default)
:param list kwargs: module's parameters

"""
Expand All @@ -652,16 +652,17 @@ def exec_command(
# interface to g.message


def message(msg, flag=None):
def message(msg, flag=None, env=None):
"""Display a message using `g.message`

:param str msg: message to be displayed
:param str flag: flags (given as string)
:param env: dictionary with system environment variables (`os.environ` by default)
"""
run_command("g.message", flags=flag, message=msg, errors="ignore")
run_command("g.message", flags=flag, message=msg, errors="ignore", env=env)


def debug(msg, debug=1):
def debug(msg, debug=1, env=None):
"""Display a debugging message using `g.message -d`.

The visibility of a debug message at runtime is controlled by
Expand All @@ -673,32 +674,35 @@ def debug(msg, debug=1):
Use 1 for messages generated once of few times,
3 for messages generated for each raster row or vector line,
5 for messages generated for each raster cell or vector point.
:param env: dictionary with system environment variables (`os.environ` by default)
"""
if debug_level() >= debug:
# TODO: quite a random hack here, do we need it somewhere else too?
if sys.platform == "win32":
msg = msg.replace("&", "^&")

run_command("g.message", flags="d", message=msg, debug=debug)
run_command("g.message", flags="d", message=msg, debug=debug, env=env)


def verbose(msg):
def verbose(msg, env=None):
"""Display a verbose message using `g.message -v`

:param str msg: verbose message to be displayed
:param env: dictionary with system environment variables (`os.environ` by default)
"""
message(msg, flag="v")
message(msg, flag="v", env=env)


def info(msg):
def info(msg, env=None):
"""Display an informational message using `g.message -i`

:param str msg: informational message to be displayed
:param env: dictionary with system environment variables (`os.environ` by default)
"""
message(msg, flag="i")
message(msg, flag="i", env=env)


def percent(i, n, s):
def percent(i, n, s, env=None):
"""Display a progress info message using `g.message -p`

::
Expand All @@ -712,44 +716,48 @@ def percent(i, n, s):
:param int i: current item
:param int n: total number of items
:param int s: increment size
:param env: dictionary with system environment variables (`os.environ` by default)
"""
message("%d %d %d" % (i, n, s), flag="p")
message("%d %d %d" % (i, n, s), flag="p", env=env)


def warning(msg):
def warning(msg, env=None):
"""Display a warning message using `g.message -w`

:param str msg: warning message to be displayed
:param env: dictionary with system environment variables (`os.environ` by default)
"""
message(msg, flag="w")
message(msg, flag="w", env=env)


def error(msg):
def error(msg, env=None):
"""Display an error message using `g.message -e`

This function does not end the execution of the program.
The right action after the error is up to the caller.
For error handling using the standard mechanism use :func:`fatal()`.

:param str msg: error message to be displayed
:param env: dictionary with system environment variables (`os.environ` by default)
"""
message(msg, flag="e")
message(msg, flag="e", env=env)


def fatal(msg):
def fatal(msg, env=None):
"""Display an error message using `g.message -e`, then abort or raise

Raises exception when module global raise_on_error is 'True', abort
(calls exit) otherwise.
Use :func:`set_raise_on_error()` to set the behavior.

:param str msg: error message to be displayed
:param env: dictionary with system environment variables (`os.environ` by default)
"""
global raise_on_error
if raise_on_error:
raise ScriptError(msg)

error(msg)
error(msg, env=None)
sys.exit(1)


Expand Down Expand Up @@ -1145,7 +1153,7 @@ def gisenv(env=None):
>>> print(env['GISDBASE']) # doctest: +SKIP
/opt/grass-data

:param env run with different environment
:param env: dictionary with system environment variables (`os.environ` by default)
:return: list of GRASS variables
"""
s = read_command("g.gisenv", flags="n", env=env)
Expand Down Expand Up @@ -1175,7 +1183,7 @@ def region(region3d=False, complete=False, env=None):

:param bool region3d: True to get 3D region
:param bool complete:
:param env env
:param env: dictionary with system environment variables (`os.environ` by default)

>>> curent_region = region()
>>> # obtain n, s, e and w values
Expand Down Expand Up @@ -1225,7 +1233,7 @@ def region_env(region3d=False, flags=None, env=None, **kwargs):

:param bool region3d: True to get 3D region
:param string flags: for example 'a'
:param env: different environment than current
:param env: dictionary with system environment variables (`os.environ` by default)
:param kwargs: g.region's parameters like 'raster', 'vector' or 'region'

::
Expand Down
Loading