t.info: add format option and JSON output support#7043
t.info: add format option and JSON output support#7043sakirr05 wants to merge 5 commits intoOSGeo:mainfrom
Conversation
ninsbl
left a comment
There was a problem hiding this comment.
Some things to consider:
- the implementation seems incomplete / too specific. Please search the codebase for example for print_shell or print_self to see relevant occurences.
- Did you consider using a custom JSONEncoder for datetime (instead of your private function)?
- Please have a look at the shell output of
t.info. JSON format has to reflect all elements there. - There is a parser standard option for formats: https://grass.osgeo.org/grass-devel/manuals/parser_standard_options.html#g_opt_f_format (search the code base for: G_OPT_F_FORMAT for inspiration)
- In the tests please also test that metadata is returned properly (right values) not only the presence of selected keys.
Consider the following (not an implementation!):
from datetime import datetime
from json import dumps, JSONEncoder
import grass.temporal as tgis
tgis.init()
# Create custom JSONEncoder (may go into grass.script ?!? with a different name)
class DateTimeEncoder(JSONEncoder):
# Override the default method
def default(self, obj):
if isinstance(obj, (date, datetime)):
return obj.isoformat(" ")
# Open an existing STRDS
stds = tgis.open_old_stds("A@PERMANENT", "strds")
# Extract metadata to dict
md = {**stds.base.__dict__.get("D"), **stds.spatial_extent.__dict__.get("D"), **stds.temporal_extent.__dict__.get("D"), **stds.metadata.__dict__.get("D")}
# Print in JSON format
print(dumps(md, indent=4, cls=DateTimeEncoder))
#compare output to output of
stds.print_shell_info()
And please, read again the AI-usage policy! Thought-through PRs are much prefered over super-fast PRs that rely quite a bit on AI generated code...
|
hey @ninsbl with existing print_shell / print_self patterns and revisit the JSON handling, including using a custom JSONEncoder for datetime as suggested. |
- Add TemporalJSONEncoder in abstract_dataset.py (datetime -> ISO via default()) - Use json.dumps(..., cls=TemporalJSONEncoder, indent=4); remove default= - Keep print_json() alongside print_info/print_shell_info; print_self separate - _to_json_dict() uses same getters as print_shell_info (no parsing)
- format option: key=format, options=plain,shell,json, default=plain - Descriptions match standard GRASS modules (Human readable, Shell script style, JSON) - guisection: Print; -g without explicit format -> shell (backward compatibility)
- Assert metadata values: name, mapset, id, temporal_type, extent, number_of_maps - Assert start_time/end_time in ISO format - Replace key-only checks with value assertions; use deterministic r.mapcalc expression
310fa56 to
61b5bab
Compare
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
ref #7041
Instead of serializing or parsing the existing text output, JSON support is implemented at the temporal dataset abstraction level and built directly from the dataset’s metadata via existing getter methods. This keeps the output stable, structured, and suitable for scripting.
Details:
Adds a format option to t.info (plain, shell, json; default: plain)
Introduces print_json() for temporal datasets
Existing plain, shell, and -g output remains unchanged
JSON output uses the standard Python json module
Pytest coverage added for JSON output
Example:
t.info` type=strds input=precip_abs1 format=jsonExample output:
Tests:
New pytest tests ensure that t.info format=json returns valid JSON and includes the expected core metadata. Tests require a GRASS session and are intended to run in CI.