Skip to content

Commit

Permalink
add shell output format
Browse files Browse the repository at this point in the history
  • Loading branch information
kritibirda26 committed Jun 16, 2024
1 parent 159dbf6 commit b95ea01
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/gis/testsuite/test_parser_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion vector/v.info/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
18 changes: 15 additions & 3 deletions vector/v.info/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}
}
26 changes: 26 additions & 0 deletions vector/v.info/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b95ea01

Please sign in to comment.