g.search.modules.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: g.search.modules
  5. # AUTHOR(S): Jachym Cepicky <jachym.cepicky gmail.com>
  6. # PURPOSE: g.search.modules in grass modules using keywords
  7. # COPYRIGHT: (C) 2015-2016 by the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General
  10. # Public License (>=v2). Read the file COPYING that
  11. # comes with GRASS for details.
  12. #
  13. #############################################################################
  14. #%module
  15. #% description: Search in GRASS modules using keywords
  16. #% keyword: general
  17. #% keyword: modules
  18. #% keyword: search
  19. #%end
  20. #%option
  21. #% key: keyword
  22. #% multiple: yes
  23. #% type: string
  24. #% description: Keyword to be searched
  25. #% required : yes
  26. #%end
  27. #%flag
  28. #% key: a
  29. #% description: Display only modules where all keywords are available (AND), default: OR
  30. #% guisection: Output
  31. #%end
  32. #%flag
  33. #% key: n
  34. #% description: Invert selection (logical NOT)
  35. #% guisection: Output
  36. #%end
  37. #%flag
  38. #% key: m
  39. #% description: Search in manual pages too (can be slow)
  40. #% guisection: Output
  41. #%end
  42. #%flag
  43. #% key: k
  44. #% label: Search only for the exact keyword in module keyword list
  45. #% description: Instead of full text search, search only in actual keywords
  46. #% guisection: Output
  47. #%end
  48. #%flag
  49. #% key: c
  50. #% description: Use colorized (more readable) output to terminal
  51. #% guisection: Output
  52. #%end
  53. #%flag
  54. #% key: g
  55. #% description: Shell script format
  56. #% guisection: Output
  57. #%end
  58. #%flag
  59. #% key: j
  60. #% description: JSON format
  61. #% guisection: Output
  62. #%end
  63. from __future__ import print_function
  64. import os
  65. import sys
  66. from grass.script.utils import diff_files, try_rmdir
  67. from grass.script import core as grass
  68. try:
  69. import xml.etree.ElementTree as etree
  70. except ImportError:
  71. import elementtree.ElementTree as etree # Python <= 2.4
  72. COLORIZE = False
  73. def main():
  74. global COLORIZE
  75. AND = flags['a']
  76. NOT = flags['n']
  77. manpages = flags['m']
  78. exact_keywords = flags['k']
  79. out_format = None
  80. if flags['g']:
  81. out_format = 'shell'
  82. elif flags['j']:
  83. out_format = 'json'
  84. else:
  85. COLORIZE = flags['c']
  86. if exact_keywords:
  87. keywords = options['keyword'].split(',')
  88. else:
  89. keywords = options['keyword'].lower().split(',')
  90. modules = _search_module(keywords, AND, NOT, manpages, exact_keywords)
  91. print_results(modules, out_format)
  92. def print_results(data, out_format=None):
  93. """
  94. Print result of the searching method
  95. each data item should have
  96. {
  97. 'name': name of the item,
  98. 'attributes': {
  99. # list of attributes to be shown too
  100. }
  101. }
  102. :param list.<dict> data: input list of found data items
  103. :param str out_format: output format 'shell', 'json', None
  104. """
  105. if not out_format:
  106. _print_results(data)
  107. elif out_format == 'shell':
  108. _print_results_shell(data)
  109. elif out_format == 'json':
  110. _print_results_json(data)
  111. def _print_results_shell(data):
  112. """Print just the name attribute"""
  113. for item in data:
  114. print(item['name'])
  115. def _print_results_json(data):
  116. """Print JSON output"""
  117. import json
  118. print(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
  119. def _print_results(data):
  120. import textwrap
  121. for item in data:
  122. print('\n{0}'.format(colorize(item['name'], attrs=['bold'])))
  123. for attr in item['attributes']:
  124. out = '{0}: {1}'.format(attr, item['attributes'][attr])
  125. out = textwrap.wrap(out, width=79, initial_indent=4 * ' ',
  126. subsequent_indent=4 * ' ' + len(attr) * ' ' + ' ')
  127. for line in out:
  128. print(line)
  129. def colorize(text, attrs=None, pattern=None):
  130. """Colorize given text input
  131. :param string text: input text to be colored
  132. :param list.<string> attrs: list of attributes as defined in termcolor package
  133. :param string pattern: text to be highlighted in input text
  134. :return: colored string
  135. """
  136. if COLORIZE:
  137. try:
  138. from termcolor import colored
  139. except ImportError:
  140. grass.fatal(_("Cannot colorize, python-termcolor is not installed"))
  141. else:
  142. def colored(pattern, attrs):
  143. return pattern
  144. if pattern:
  145. return text.replace(pattern, colored(pattern, attrs=attrs))
  146. else:
  147. return colored(text, attrs=attrs)
  148. def _search_module(keywords, logical_and=False, invert=False, manpages=False,
  149. exact_keywords=False):
  150. """Search modules by given keywords
  151. :param list.<str> keywords: list of keywords
  152. :param boolean logical_and: use AND (default OR)
  153. :param boolean manpages: search in manpages too
  154. :return dict: modules
  155. """
  156. WXGUIDIR = os.path.join(os.getenv("GISBASE"), "gui", "wxpython")
  157. filename = os.path.join(WXGUIDIR, 'xml', 'module_items.xml')
  158. menudata_file = open(filename, 'r')
  159. menudata = etree.parse(menudata_file)
  160. menudata_file.close()
  161. items = menudata.findall('module-item')
  162. found_modules = []
  163. for item in items:
  164. name = item.attrib['name']
  165. description = item.find('description').text
  166. module_keywords = item.find('keywords').text
  167. found = [False]
  168. if logical_and:
  169. found = [False] * len(keywords)
  170. for idx in range(len(keywords)):
  171. keyword = keywords[idx]
  172. keyword_found = False
  173. if exact_keywords:
  174. keyword_found = _exact_search(keyword, module_keywords)
  175. else:
  176. keyword_found = _basic_search(keyword, name, description,
  177. module_keywords)
  178. if not keyword_found and manpages:
  179. keyword_found = _manpage_search(keyword, name)
  180. if keyword_found:
  181. if logical_and:
  182. found[idx] = True
  183. else:
  184. found = [True]
  185. description = colorize(description,
  186. attrs=['underline'],
  187. pattern=keyword)
  188. module_keywords = colorize(module_keywords,
  189. attrs=['underline'],
  190. pattern=keyword)
  191. add = False not in found
  192. if invert:
  193. add = not add
  194. if add:
  195. found_modules.append({
  196. 'name': name,
  197. 'attributes': {
  198. 'keywords': module_keywords,
  199. 'description': description
  200. }
  201. })
  202. return found_modules
  203. def _basic_search(pattern, name, description, module_keywords):
  204. """Search for a string in all the provided strings.
  205. This lowercases the strings before searching in them, so the pattern
  206. string should be lowercased too.
  207. """
  208. if name.lower().find(pattern) > -1 or\
  209. description.lower().find(pattern) > -1 or\
  210. module_keywords.lower().find(pattern) > -1:
  211. return True
  212. else:
  213. return False
  214. def _exact_search(keyword, module_keywords):
  215. """Compare exact keyword with module keywords
  216. :param keyword: exact keyword to find in the list (not lowercased)
  217. :param module_keywords: comma separated list of keywords
  218. """
  219. module_keywords = module_keywords.split(',')
  220. for current in module_keywords:
  221. if keyword == current:
  222. return True
  223. return False
  224. def _manpage_search(pattern, name):
  225. manpage = grass.read_command('g.manual', flags='m', entry=name)
  226. return manpage.lower().find(pattern) > -1
  227. if __name__ == "__main__":
  228. options, flags = grass.parser()
  229. sys.exit(main())