Skip to content

Commit

Permalink
grass.script: Add separate function for setting location description (#…
Browse files Browse the repository at this point in the history
…3431)

This adds a separate function for setting location (aka title) stored in MYNAME file. It includes tests of this function.
  • Loading branch information
wenzeslaus committed Mar 7, 2024
1 parent 9cb4745 commit ac63b38
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
11 changes: 8 additions & 3 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1821,14 +1821,19 @@ def create_location(
if ps.returncode != 0 and error:
raise ScriptError(repr(error))

_set_location_description(dbase, location, desc)


def _set_location_description(path, location, text):
"""Set description (aka title aka MYNAME) for a location"""
try:
fd = codecs.open(
os.path.join(dbase, location, "PERMANENT", "MYNAME"),
os.path.join(path, location, "PERMANENT", "MYNAME"),
encoding="utf-8",
mode="w",
)
if desc:
fd.write(desc + os.linesep)
if text:
fd.write(text + os.linesep)
else:
fd.write(os.linesep)
fd.close()
Expand Down
31 changes: 31 additions & 0 deletions python/grass/script/tests/grass_script_core_location_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,34 @@ def test_files(tmp_path):
description_file = base_path / "MYNAME"
assert description_file.exists()
assert description_file.read_text(encoding="utf-8").strip() == description


def set_and_test_description(tmp_path, text):
"""Set text as description and check the result"""
name = "test"
gs.core._create_location_xy(tmp_path, name) # pylint: disable=protected-access
gs.core._set_location_description(tmp_path, name, text)
description_file = tmp_path / name / "PERMANENT" / "MYNAME"
assert description_file.exists()
text = text if text else "" # None and empty should both yield empty.
assert description_file.read_text(encoding="utf-8").strip() == text


def test_location_description_setter_ascii(tmp_path):
"""Check ASCII text"""
set_and_test_description(tmp_path, "This is a test")


def test_location_description_setter_unicode(tmp_path):
"""Check unicode text"""
set_and_test_description(tmp_path, "This is a test (not Gauss-Krüger or Křovák)")


def test_location_description_setter_empty(tmp_path):
"""Check empty text"""
set_and_test_description(tmp_path, "")


def test_location_description_setter_none(tmp_path):
"""Check None in place of text"""
set_and_test_description(tmp_path, None)

0 comments on commit ac63b38

Please sign in to comment.