gutils.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. Utilities related to GRASS GIS for GRASS Python testing framework
  3. Copyright (C) 2014 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS GIS
  6. for details.
  7. :authors: Vaclav Petras
  8. """
  9. from grass.script.core import start_command, PIPE
  10. from grass.script.utils import decode
  11. from .gmodules import call_module
  12. from .checkers import text_to_keyvalue
  13. def get_current_mapset():
  14. """Get curret mapset name as a string"""
  15. return call_module("g.mapset", flags="p").strip()
  16. def is_map_in_mapset(name, type, mapset=None):
  17. """Check is map is present in the mapset (current mapset by default)
  18. This function is different from what we would expect in GRASS
  19. because it cares only about specific mapset, the current one by default,
  20. and it does not care that the map is accessible in other mapset.
  21. :param name: name of the map
  22. :param type: data type ('raster', 'raster3d', and 'vector')
  23. """
  24. if not mapset:
  25. mapset = get_current_mapset()
  26. # change type to element used by find file
  27. # otherwise, we are not checking the input,
  28. # so anything accepted by g.findfile will work but this can change in the
  29. # future (the documentation is clear about what's legal)
  30. # supporting both short and full names
  31. if type == "rast" or type == "raster":
  32. type = "cell"
  33. elif type == "rast3d" or type == "raster3d":
  34. type = "grid3"
  35. elif type == "vect":
  36. type = "vector"
  37. # g.findfile returns non-zero when file was not found
  38. # se we ignore return code and just focus on stdout
  39. process = start_command(
  40. "g.findfile",
  41. flags="n",
  42. element=type,
  43. file=name,
  44. mapset=mapset,
  45. stdout=PIPE,
  46. stderr=PIPE,
  47. )
  48. output, errors = process.communicate()
  49. info = text_to_keyvalue(decode(output), sep="=")
  50. # file is the key questioned in grass.script.core find_file()
  51. # return code should be equivalent to checking the output
  52. if info["file"]:
  53. return True
  54. else:
  55. return False