modulesdata.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """!
  2. @package core.modulesdata
  3. @brief Provides information about available modules
  4. Classes:
  5. - modules::modulesdata
  6. (C) 2009-2012 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Martin Landa <landa.martin gmail.com>
  10. @author Vaclav Petras <wenzeslaus gmail.com>
  11. @author Anna Kratochvilova <kratochanna gmail.com>
  12. """
  13. import sys
  14. import os
  15. if __name__ == '__main__':
  16. sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "gui", "wxpython"))
  17. from core import globalvar
  18. from lmgr.menudata import LayerManagerMenuData
  19. class ModulesData(object):
  20. """!Holds information about modules.
  21. @todo add doctest
  22. """
  23. def __init__(self, modulesDesc = None):
  24. if modulesDesc is not None:
  25. self.moduleDesc = modulesDesc
  26. else:
  27. self.moduleDesc = LayerManagerMenuData().GetModules()
  28. self.moduleDict = self.GetDictOfModules()
  29. def GetCommandDesc(self, cmd):
  30. """!Gets the description for a given module (command).
  31. If the given module is not available, an empty string is returned.
  32. \code
  33. print data.GetCommandDesc('r.info')
  34. Outputs basic information about a raster map.
  35. \endcode
  36. """
  37. if cmd in self.moduleDesc:
  38. return self.moduleDesc[cmd]['desc']
  39. return ''
  40. def GetCommandItems(self):
  41. """!Gets list of available modules (commands).
  42. The list contains available module names.
  43. \code
  44. print data.GetCommandItems()[0:4]
  45. ['d.barscale', 'd.colorlist', 'd.colortable', 'd.correlate']
  46. \endcode
  47. """
  48. items = list()
  49. mList = self.moduleDict
  50. prefixes = mList.keys()
  51. prefixes.sort()
  52. for prefix in prefixes:
  53. for command in mList[prefix]:
  54. name = prefix + '.' + command
  55. if name not in items:
  56. items.append(name)
  57. items.sort()
  58. return items
  59. def GetDictOfModules(self):
  60. """!Gets modules as a dictionary optimized for autocomplete.
  61. \code
  62. print data.GetDictOfModules()['r'][0:4]
  63. print data.GetDictOfModules()['r.li'][0:4]
  64. r: ['basins.fill', 'bitpattern', 'blend', 'buffer']
  65. r.li: ['cwed', 'dominance', 'edgedensity', 'mpa']
  66. \endcode
  67. """
  68. result = dict()
  69. for module in globalvar.grassCmd:
  70. try:
  71. group, name = module.split('.', 1)
  72. except ValueError:
  73. continue # TODO
  74. if group not in result:
  75. result[group] = list()
  76. result[group].append(name)
  77. # for better auto-completion:
  78. # not only result['r']={...,'colors.out',...}
  79. # but also result['r.colors']={'out',...}
  80. for i in range(len(name.split('.')) - 1):
  81. group = '.'.join([group, name.split('.', 1)[0]])
  82. name = name.split('.', 1)[1]
  83. if group not in result:
  84. result[group] = list()
  85. result[group].append(name)
  86. # sort list of names
  87. for group in result.keys():
  88. result[group].sort()
  89. return result
  90. def FindModules(self, text, findIn):
  91. """!Finds modules according to given text.
  92. @param text string to search
  93. @param findIn where to search for text
  94. (allowed values are 'description', 'keywords' and 'command')
  95. """
  96. modules = dict()
  97. iFound = 0
  98. for module, data in self.moduleDesc.iteritems():
  99. found = False
  100. if findIn == 'description':
  101. if text in data['desc']:
  102. found = True
  103. elif findIn == 'keywords':
  104. if text in ','.join(data['keywords']):
  105. found = True
  106. elif findIn == 'command':
  107. if module[:len(text)] == text:
  108. found = True
  109. else:
  110. raise ValueError("Parameter findIn is not valid")
  111. if found:
  112. try:
  113. group, name = module.split('.')
  114. except ValueError:
  115. continue # TODO
  116. iFound += 1
  117. if group not in modules:
  118. modules[group] = list()
  119. modules[group].append(name)
  120. return modules, iFound
  121. def SetFilter(self, data = None):
  122. """!Sets filter modules
  123. If @p data is not specified, module dictionary is derived
  124. from an internal data structures.
  125. @todo Document this method.
  126. @param data data dict
  127. """
  128. if data:
  129. self.moduleDict = data
  130. else:
  131. self.moduleDict = self.GetDictOfModules()
  132. def test():
  133. data = ModulesData()
  134. module = 'r.info'
  135. print '%s:' % module, data.GetCommandDesc(module)
  136. print '[0:5]:', data.GetCommandItems()[0:5]
  137. modules = data.GetDictOfModules()
  138. print 'r:', modules['r'][0:4]
  139. print 'r.li:', modules['r.li'][0:4]
  140. if __name__ == '__main__':
  141. test()