[GRASS-SVN] r70186 - grass/branches/releasebranch_7_2/scripts/g.search.modules

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jan 1 12:27:44 PST 2017


Author: neteler
Date: 2017-01-01 12:27:44 -0800 (Sun, 01 Jan 2017)
New Revision: 70186

Modified:
   grass/branches/releasebranch_7_2/scripts/g.search.modules/g.search.modules.py
Log:
g.search.modules: search for an exact keyword associated with a module using -k flag (trunk, r69155)

Modified: grass/branches/releasebranch_7_2/scripts/g.search.modules/g.search.modules.py
===================================================================
--- grass/branches/releasebranch_7_2/scripts/g.search.modules/g.search.modules.py	2017-01-01 20:26:13 UTC (rev 70185)
+++ grass/branches/releasebranch_7_2/scripts/g.search.modules/g.search.modules.py	2017-01-01 20:27:44 UTC (rev 70186)
@@ -41,6 +41,12 @@
 #% guisection: Output
 #%end
 #%flag
+#% key: k
+#% label: Search only for the exact keyword in module keyword list
+#% description: Instead of full text search, search only in actual keywords
+#% guisection: Output
+#%end
+#%flag
 #% key: c
 #% description: Use colorized (more readable) output to terminal
 #% guisection: Output
@@ -72,10 +78,10 @@
 
 def main():
     global COLORIZE
-    keywords = options['keyword'].lower().split(',')
     AND = flags['a']
     NOT = flags['n']
     manpages = flags['m']
+    exact_keywords = flags['k']
     out_format = None
     if flags['g']:
         out_format = 'shell'
@@ -84,8 +90,13 @@
     else:
         COLORIZE = flags['c']
 
-    modules = _search_module(keywords, AND, NOT, manpages)
+    if exact_keywords:
+        keywords = options['keyword'].split(',')
+    else:
+        keywords = options['keyword'].lower().split(',')
 
+    modules = _search_module(keywords, AND, NOT, manpages, exact_keywords)
+
     print_results(modules, out_format)
 
 
@@ -168,7 +179,8 @@
         return colored(text, attrs=attrs)
 
 
-def _search_module(keywords, logical_and=False, invert=False, manpages=False):
+def _search_module(keywords, logical_and=False, invert=False, manpages=False,
+                   exact_keywords=False):
     """Search modules by given keywords
 
     :param list.<str> keywords: list of keywords
@@ -200,7 +212,11 @@
             keyword = keywords[idx]
             keyword_found = False
 
-            keyword_found = _basic_search(keyword, name, description, module_keywords)
+            if exact_keywords:
+                keyword_found = _exact_search(keyword, module_keywords)
+            else:
+                keyword_found = _basic_search(keyword, name, description,
+                                              module_keywords)
 
             if not keyword_found and manpages:
                 keyword_found = _manpage_search(keyword, name)
@@ -234,7 +250,11 @@
 
 
 def _basic_search(pattern, name, description, module_keywords):
+    """Search for a string in all the provided strings.
 
+    This lowercases the strings before searching in them, so the pattern
+    string should be lowercased too.
+    """
     if name.lower().find(pattern) > -1 or\
        description.lower().find(pattern) > -1 or\
        module_keywords.lower().find(pattern) > -1:
@@ -244,6 +264,19 @@
         return False
 
 
+def _exact_search(keyword, module_keywords):
+    """Compare exact keyword with module keywords
+
+    :param keyword: exact keyword to find in the list (not lowercased)
+    :param module_keywords: comma separated list of keywords
+    """
+    module_keywords = module_keywords.split(',')
+    for current in module_keywords:
+        if keyword == current:
+            return True
+    return False
+
+
 def _manpage_search(pattern, name):
 
     manpage = grass.read_command('g.manual', flags='m', entry=name)



More information about the grass-commit mailing list