[GRASS-dev] g.search.module: shell style output

Vaclav Petras wenzeslaus at gmail.com
Sun Aug 21 19:39:19 PDT 2016


On Fri, Aug 19, 2016 at 2:15 AM, Jachym Cepicky <jachym.cepicky at gmail.com>
wrote:

> Hi,
>
> no special reason for not listing the module description too, just did not
> came to my mind
>

Thanks. Good to know.


>
> Just do it [1]
>

While using the modified version, I actually realized that "shell script
style" usually produces key-value pairs which can which can be evaluated by
shell's eval or grass.script.parse_command. Not all modules comply with
this, e.g. `g.extension -g` produces multiple key-values with same keys and
order matters, so this must be parsed in a special way. The result is
actually exactly the information g.search.modules is producing:

$ g.extension -g
...
name=v.habitat.dem
description=Calculates DEM derived characteristics of habitats.
keywords=vector,raster,terrain,statistics,sun,zonal statistics
name=v.in.gbif
description=importing of GBIF species distribution data
keywords=vector,geometry

`g.extension -l` produces list of modules in the same way as currently
`g.search.modules -g` produces:

$ g.extension -l

v.habitat.dem
v.in.gbif

As a result, I don't know what to do with -g, at this point I would just
replace the letter by -n (names only) or -s (short output with names only)
and add -t for table output (that's in the attached patch). -g can go to
renamed options for compatibility reasons for now.

For the future, we should try to keep in mind that g.extension and
g.search.module should have unified interfaces and/or outputs. And more
generally, we should define what -g "shell script style" means.


>
> J
>
> [1] https://www.youtube.com/watch?v=ZXsQAXx_ao0
>
> čt 18. 8. 2016 v 20:32 odesílatel Vaclav Petras <wenzeslaus at gmail.com>
> napsal:
>
>> Hi Jachym,
>>
>> the g.search.module -g flag (shell style output) outputs just names. Do
>> you have a particular reason for it? My use case is something like that:
>>
>> g.search.modules keyword="support" -g | sed -e "s/|[^|]*$//g" | sed -e
>> "s/|/\t/g"
>>
>> with the following desired output (name + keywords, description removed
>> by sed):
>>
>> g.version    general,support,citing,copyright,version,license
>> t.support    temporal,metadata,time
>> r.support    raster,metadata
>> r.support.stats    raster,statistics
>> r.out.gdal    raster,export
>> v.out.ogr    vector,export,OGR
>> r3.support    raster3d,metadata,voxel
>> g.findetc    general,map management,scripts
>> v.external    vector,import,external,OGR,PostGIS
>> g.message    general,support,scripts
>> g.tempfile    general,support,scripts
>> v.support    vector,metadata
>> r.external    raster,import,external
>>
>> I can actually see that outputting just module names can be advantageous
>> in some cases. But I want to get something like, so I can throw sed and
>> grep on it:
>>
>> v.support|vector,metadata|Updates vector map metadata.
>>
>> If we permit change of the interface, I think -g could do the output
>> above. This would make the -g output more like the others: same information
>> as by default and with -j, so we can even consider it fixing a bug.
>>
>> The current output with -g can be generated with some other flag. -n* for
>> "names only" perhaps?
>>
>> Best,
>> Vaclav
>>
>> * https://lists.osgeo.org/pipermail/grass-dev/2016-August/081556.html
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20160821/ee2413ff/attachment-0001.html>
-------------- next part --------------
Index: g.search.modules.py
===================================================================
--- g.search.modules.py	(revision 69155)
+++ g.search.modules.py	(working copy)
@@ -52,11 +52,16 @@
 #% guisection: Output
 #%end
 #%flag
-#% key: g
-#% description: Shell script format
+#% key: t
+#% description: Tabular format (pipe as a field separator)
 #% guisection: Output
 #%end
 #%flag
+#% key: s
+#% description: Print only name
+#% guisection: Output
+#%end
+#%flag
 #% key: j
 #% description: JSON format
 #% guisection: Output
@@ -83,8 +88,10 @@
     manpages = flags['m']
     exact_keywords = flags['k']
     out_format = None
-    if flags['g']:
-        out_format = 'shell'
+    if flags['t']:
+        out_format = 'table'
+    elif flags['s']:
+        out_format = 'name'
     elif flags['j']:
         out_format = 'json'
     else:
@@ -120,14 +127,17 @@
     if not out_format:
         _print_results(data)
 
-    elif out_format == 'shell':
-        _print_results_shell(data)
+    elif out_format == 'table':
+        _print_results_table(data)
 
+    elif out_format == 'name':
+        _print_results_name(data)
+
     elif out_format == 'json':
         _print_results_json(data)
 
 
-def _print_results_shell(data):
+def _print_results_name(data):
     """Print just the name attribute"""
 
     for item in data:
@@ -134,6 +144,18 @@
         print(item['name'])
 
 
+def _print_results_table(data):
+    """Print in shell script style"""
+
+    separator = '|'
+    for item in data:
+        # replace potential line endings just to be sure we keep the format
+        description = item['attributes']['description'].replace(
+            os.linesep, " ").replace("\n", " ")
+        print(separator.join((item['name'],
+                              item['attributes']['keywords'], description)))
+
+
 def _print_results_json(data):
     """Print JSON output"""
 


More information about the grass-dev mailing list