grass icon indicating copy to clipboard operation
grass copied to clipboard

r.info: Output comments as one string in JSON

Open wenzeslaus opened this issue 1 year ago • 5 comments

The part of history which appears under the comments key can be multiple lines and is stored in a format which limits line length. This is basically how r.info shows it by default in the plain output for humans. Long lines can be split and continuation is marked using a backslash.

This adds a function which puts the continued lines back together and adds newlines between the other lines. There is no newline at the end. This string is then serialized in JSON instead of the original list of strings.

This also avoids serializing the history twice when the h flag is not used, but it does change the behavior for what keys are included.

wenzeslaus avatar Aug 23 '24 15:08 wenzeslaus

I assume that following #4304, I should replace strcat by G_strlcat here, no?

wenzeslaus avatar Sep 13 '24 12:09 wenzeslaus

I assume that following #4304, I should replace strcat by G_strlcat here, no?

I think that would be good, and replace strncat() too. If you know it will fit into dest buffer, just suppress return value with (void).

nilason avatar Sep 13 '24 14:09 nilason

Nice. I just noticed that the parsed history may contain empty lines. See e.g.:

print(json.loads(gs.read_command("r.info", format="json", map="basin_50K"))["comments"])

No problem, but probably worth removing?

ninsbl avatar Sep 22 '24 20:09 ninsbl

Why to remove the empty line? That's what was stored. Why to remove it? See the results with this PR:

$ grass-dev ~/grassdata/nc_spm_08_grass7/ --tmp-mapset --exec python
>>> import grass.script as gs
>>> import json
>>> print(json.loads(gs.read_command("r.info", format="json", map="basin_50K"))["comments"])
Processing mode: All in RAM

r.watershed elevation="elevation" threshold=50000 accumulation="accum_50K" drainage="draindir_50K" basin="basin_50K"
>>> print(gs.parse_command("r.info", format="json", map="basin_50K")["comments"])
Processing mode: All in RAM

r.watershed elevation="elevation" threshold=50000 accumulation="accum_50K" drainage="draindir_50K" basin="basin_50K"

wenzeslaus avatar Sep 23 '24 01:09 wenzeslaus

What do you think about showing it in JSON just as it is stored in terms of newlines? Can you review this @ninsbl?

wenzeslaus avatar Oct 14 '24 18:10 wenzeslaus