From b95ea0157f66005edc06ed57a44cac9ef47dc7b3 Mon Sep 17 00:00:00 2001 From: Kriti Birda Date: Sun, 16 Jun 2024 18:01:47 +0530 Subject: [PATCH] add shell output format --- lib/gis/testsuite/test_parser_json.py | 4 +++- vector/v.info/local_proto.h | 2 +- vector/v.info/parse.c | 18 +++++++++++++++--- vector/v.info/print.c | 26 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/gis/testsuite/test_parser_json.py b/lib/gis/testsuite/test_parser_json.py index b325de2314c..918fb453d04 100644 --- a/lib/gis/testsuite/test_parser_json.py +++ b/lib/gis/testsuite/test_parser_json.py @@ -97,13 +97,15 @@ def test_v_info(self): inputs = [ {"param": "map", "value": "hospitals@PERMANENT"}, {"param": "layer", "value": "1"}, + {"param": "format", "value": "plain"}, ] stdout, stderr = subprocess.Popen(args, stdout=subprocess.PIPE).communicate() print(stdout) json_code = json.loads(decode(stdout)) + print(json_code) self.assertEqual(json_code["module"], "v.info") - self.assertEqual(len(json_code["inputs"]), 2) + self.assertEqual(len(json_code["inputs"]), 3) self.assertEqual(json_code["inputs"], inputs) diff --git a/vector/v.info/local_proto.h b/vector/v.info/local_proto.h index 243a6ca7668..1ed32468562 100644 --- a/vector/v.info/local_proto.h +++ b/vector/v.info/local_proto.h @@ -6,7 +6,7 @@ #define SHELL_REGION 0x04 #define SHELL_TOPO 0x08 -enum OutputFormat { PLAIN, JSON }; +enum OutputFormat { PLAIN, SHELL, JSON }; /* level1.c */ int level_one_info(struct Map_info *); diff --git a/vector/v.info/parse.c b/vector/v.info/parse.c index 7d59ed922eb..54820420e01 100644 --- a/vector/v.info/parse.c +++ b/vector/v.info/parse.c @@ -43,6 +43,10 @@ void parse_args(int argc, char **argv, char **input, char **field, int *history, topo_flag->guisection = _("Print"); format_opt = G_define_standard_option(G_OPT_F_FORMAT); + format_opt->options = "plain,shell,json"; + format_opt->descriptions = _("plain;Human readable text output;" + "shell;shell script style text output;" + "json;JSON (JavaScript Object Notation);"); format_opt->guisection = _("Print"); if (G_parser(argc, argv)) @@ -60,8 +64,16 @@ void parse_args(int argc, char **argv, char **input, char **field, int *history, if (topo_flag->answer) *shell |= SHELL_TOPO; - if (strcmp(format_opt->answer, "json") == 0) - *format_ptr = JSON; - else + if (strcmp(format_opt->answer, "plain") == 0) { *format_ptr = PLAIN; + } + else if (strcmp(format_opt->answer, "json") == 0) + *format_ptr = JSON; + else { + *format_ptr = SHELL; + } + + if (*shell != 0 && *format_ptr == PLAIN) { + *format_ptr = SHELL; + } } diff --git a/vector/v.info/print.c b/vector/v.info/print.c index 1cd5a31b75a..ab8fe61e987 100644 --- a/vector/v.info/print.c +++ b/vector/v.info/print.c @@ -52,6 +52,8 @@ void print_region(struct Map_info *Map, enum OutputFormat format, switch (format) { case PLAIN: + break; + case SHELL: G_format_northing(box.N, tmp1, -1); G_format_northing(box.S, tmp2, -1); fprintf(stdout, "north=%s\n", tmp1); @@ -96,6 +98,8 @@ void print_topo(struct Map_info *Map, enum OutputFormat format, switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "nodes=%d\n", Vect_get_num_nodes(Map)); fflush(stdout); @@ -248,6 +252,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "name=%s\n", Vect_get_name(Map)); fprintf(stdout, "mapset=%s\n", Vect_get_mapset(Map)); fprintf(stdout, "location=%s\n", G_location()); @@ -280,6 +286,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, G_format_timestamp(&ts, timebuff); switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "timestamp=%s\n", timebuff); break; case JSON: @@ -290,6 +298,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, else { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "timestamp=none\n"); break; case JSON: @@ -301,6 +311,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, if (map_type == GV_FORMAT_OGR || map_type == GV_FORMAT_OGR_DIRECT) { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "format=%s,%s\n", Vect_maptype_info(Map), Vect_get_finfo_format_info(Map)); fprintf(stdout, "ogr_layer=%s\n", Vect_get_finfo_layer_name(Map)); @@ -331,6 +343,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "format=%s,%s\n", Vect_maptype_info(Map), Vect_get_finfo_format_info(Map)); fprintf(stdout, "pg_table=%s\n", Vect_get_finfo_layer_name(Map)); @@ -360,6 +374,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, if (topo_format == GV_TOPO_POSTGIS) { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "pg_topo_schema=%s\n", toposchema_name); fprintf(stdout, "pg_topo_column=%s\n", topogeom_column); break; @@ -375,6 +391,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, else { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "format=%s\n", Vect_maptype_info(Map)); break; case JSON: @@ -386,6 +404,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "level=%d\n", Vect_level(Map)); break; case JSON: @@ -395,6 +415,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, if (Vect_level(Map) > 0) { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "num_dblinks=%d\n", Vect_get_num_dblinks(Map)); break; case JSON: @@ -408,6 +430,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, if (fi != NULL) { switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "attribute_layer_number=%i\n", fi->number); fprintf(stdout, "attribute_layer_name=%s\n", fi->name); fprintf(stdout, "attribute_database=%s\n", fi->database); @@ -437,6 +461,8 @@ void print_shell(struct Map_info *Map, const char *field_opt, switch (format) { case PLAIN: + break; + case SHELL: fprintf(stdout, "projection=%s\n", Vect_get_proj_name(Map)); if (G_projection() == PROJECTION_UTM) { fprintf(stdout, "zone=%d\n", Vect_get_zone(Map));