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: Add separate function for setting location description #3431

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,14 +1794,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 @@ -101,3 +101,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)
Loading