modulesdata.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. @todo analyze what exactly this class is doing
  23. @todo split this class into two classes
  24. @see modules::extensions::ExtensionModulesData
  25. """
  26. def __init__(self, modulesDesc = None):
  27. if modulesDesc is not None:
  28. self.moduleDesc = modulesDesc
  29. else:
  30. self.moduleDesc = LayerManagerMenuData().GetModules()
  31. self.moduleDict = self.GetDictOfModules()
  32. def GetCommandDesc(self, cmd):
  33. """!Gets the description for a given module (command).
  34. If the given module is not available, an empty string is returned.
  35. \code
  36. print data.GetCommandDesc('r.info')
  37. Outputs basic information about a raster map.
  38. \endcode
  39. """
  40. if cmd in self.moduleDesc:
  41. return self.moduleDesc[cmd]['description']
  42. return ''
  43. def GetCommandItems(self):
  44. """!Gets list of available modules (commands).
  45. The list contains available module names.
  46. \code
  47. print data.GetCommandItems()[0:4]
  48. ['d.barscale', 'd.colorlist', 'd.colortable', 'd.correlate']
  49. \endcode
  50. """
  51. items = list()
  52. mList = self.moduleDict
  53. prefixes = mList.keys()
  54. prefixes.sort()
  55. for prefix in prefixes:
  56. for command in mList[prefix]:
  57. name = prefix + '.' + command
  58. if name not in items:
  59. items.append(name)
  60. items.sort()
  61. return items
  62. def GetDictOfModules(self):
  63. """!Gets modules as a dictionary optimized for autocomplete.
  64. \code
  65. print data.GetDictOfModules()['r'][0:4]
  66. print data.GetDictOfModules()['r.li'][0:4]
  67. r: ['basins.fill', 'bitpattern', 'blend', 'buffer']
  68. r.li: ['cwed', 'dominance', 'edgedensity', 'mpa']
  69. \endcode
  70. """
  71. result = dict()
  72. for module in globalvar.grassCmd:
  73. try:
  74. group, name = module.split('.', 1)
  75. except ValueError:
  76. continue # TODO
  77. if group not in result:
  78. result[group] = list()
  79. result[group].append(name)
  80. # for better auto-completion:
  81. # not only result['r']={...,'colors.out',...}
  82. # but also result['r.colors']={'out',...}
  83. for i in range(len(name.split('.')) - 1):
  84. group = '.'.join([group, name.split('.', 1)[0]])
  85. name = name.split('.', 1)[1]
  86. if group not in result:
  87. result[group] = list()
  88. result[group].append(name)
  89. # sort list of names
  90. for group in result.keys():
  91. result[group].sort()
  92. return result
  93. def FindModules(self, text, findIn):
  94. """!Finds modules according to given text.
  95. @param text string to search
  96. @param findIn where to search for text
  97. (allowed values are 'description', 'keywords' and 'command')
  98. """
  99. modules = dict()
  100. iFound = 0
  101. for module, data in self.moduleDesc.iteritems():
  102. found = False
  103. if findIn == 'description':
  104. if text in data['description']:
  105. found = True
  106. elif findIn == 'keywords':
  107. if text in ','.join(data['keywords']):
  108. found = True
  109. elif findIn == 'command':
  110. if module[:len(text)] == text:
  111. found = True
  112. else:
  113. raise ValueError("Parameter findIn is not valid")
  114. if found:
  115. try:
  116. group, name = module.split('.')
  117. except ValueError:
  118. continue # TODO
  119. iFound += 1
  120. if group not in modules:
  121. modules[group] = list()
  122. modules[group].append(name)
  123. return modules, iFound
  124. def SetFilter(self, data = None):
  125. """!Sets filter modules
  126. If @p data is not specified, module dictionary is derived
  127. from an internal data structures.
  128. @todo Document this method.
  129. @param data data dict
  130. """
  131. if data:
  132. self.moduleDict = data
  133. else:
  134. self.moduleDict = self.GetDictOfModules()
  135. def test():
  136. data = ModulesData()
  137. module = 'r.info'
  138. print '%s:' % module, data.GetCommandDesc(module)
  139. print '[0:5]:', data.GetCommandItems()[0:5]
  140. modules = data.GetDictOfModules()
  141. print 'r:', modules['r'][0:4]
  142. print 'r.li:', modules['r.li'][0:4]
  143. if __name__ == '__main__':
  144. test()