Skip to content

Commit

Permalink
find_file: translate more legacy elements (#2881)
Browse files Browse the repository at this point in the history
* translate more legacy elements

* remove find_file message
  • Loading branch information
ninsbl authored Mar 10, 2023
1 parent 679b2d0 commit 35123df
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,13 +1326,30 @@ def del_temp_region():

def find_file(name, element="cell", mapset=None, env=None):
"""Returns the output from running g.findfile as a
dictionary. Example:
dictionary.
Elements in g.findfile refer to mapset directories. However, in
parts of the code, different element terms like rast, raster, or rast3d
are used. For convenience the function translates such element types
to respective mapset elements. Current translations are:
"rast": "cell",
"raster": "cell",
"rast3d": "grid3",
"raster3d": "grid3",
"raster_3d": "grid3",
Example:
>>> result = find_file('elevation', element='cell')
>>> print(result['fullname'])
elevation@PERMANENT
>>> print(result['file']) # doctest: +ELLIPSIS
/.../PERMANENT/cell/elevation
>>> result = find_file('elevation', element='raster')
>>> print(result['fullname'])
elevation@PERMANENT
>>> print(result['file']) # doctest: +ELLIPSIS
/.../PERMANENT/cell/elevation
:param str name: file name
Expand All @@ -1342,11 +1359,19 @@ def find_file(name, element="cell", mapset=None, env=None):
:return: parsed output of g.findfile
"""
if element == "raster" or element == "rast":
verbose(_('Element type should be "cell" and not "%s"') % element)
element = "cell"
element_translation = {
"rast": "cell",
"raster": "cell",
"rast3d": "grid3",
"raster3d": "grid3",
"raster_3d": "grid3",
}

if element in element_translation:
element = element_translation[element]

# g.findfile returns non-zero when file was not found
# se we ignore return code and just focus on stdout
# so we ignore return code and just focus on stdout
process = start_command(
"g.findfile",
flags="n",
Expand Down

0 comments on commit 35123df

Please sign in to comment.