[GRASS-SVN] r69155 - grass/trunk/scripts/g.search.modules
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Aug 18 14:23:14 PDT 2016
Author: wenzeslaus
Date: 2016-08-18 14:23:13 -0700 (Thu, 18 Aug 2016)
New Revision: 69155
Modified:
grass/trunk/scripts/g.search.modules/g.search.modules.py
Log:
g.search.modules: search for an exact keyword associated with a module using -k flag
Modified: grass/trunk/scripts/g.search.modules/g.search.modules.py
===================================================================
--- grass/trunk/scripts/g.search.modules/g.search.modules.py 2016-08-18 15:12:39 UTC (rev 69154)
+++ grass/trunk/scripts/g.search.modules/g.search.modules.py 2016-08-18 21:23:13 UTC (rev 69155)
@@ -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