task.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. """
  2. Get interface description of GRASS commands
  3. Based on gui/wxpython/gui_modules/menuform.py
  4. Usage:
  5. ::
  6. from grass.script import task as gtask
  7. gtask.command_info('r.info')
  8. (C) 2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. .. sectionauthor:: Martin Landa <landa.martin gmail.com>
  13. """
  14. import re
  15. import sys
  16. from .utils import decode, split
  17. from .core import *
  18. try:
  19. import xml.etree.ElementTree as etree
  20. except ImportError:
  21. import elementtree.ElementTree as etree # Python <= 2.4
  22. from xml.parsers import expat # TODO: works for any Python?
  23. # Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7
  24. # and ElementTree 1.3.
  25. if hasattr(etree, 'ParseError'):
  26. ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
  27. else:
  28. ETREE_EXCEPTIONS = (expat.ExpatError)
  29. if sys.version_info.major >= 3:
  30. unicode = str
  31. class grassTask:
  32. """This class holds the structures needed for filling by the parser
  33. Parameter blackList is a dictionary with fixed structure, eg.
  34. ::
  35. blackList = {'items' : {'d.legend' : { 'flags' : ['m'], 'params' : [] }},
  36. 'enabled': True}
  37. :param str path: full path
  38. :param blackList: hide some options in the GUI (dictionary)
  39. """
  40. def __init__(self, path=None, blackList=None):
  41. self.path = path
  42. self.name = _('unknown')
  43. self.params = list()
  44. self.description = ''
  45. self.label = ''
  46. self.flags = list()
  47. self.keywords = list()
  48. self.errorMsg = ''
  49. self.firstParam = None
  50. if blackList:
  51. self.blackList = blackList
  52. else:
  53. self.blackList = {'enabled': False, 'items': {}}
  54. if path is not None:
  55. try:
  56. processTask(tree=etree.fromstring(get_interface_description(path)),
  57. task=self)
  58. except ScriptError as e:
  59. self.errorMsg = e.value
  60. self.define_first()
  61. def define_first(self):
  62. """Define first parameter
  63. :return: name of first parameter
  64. """
  65. if len(self.params) > 0:
  66. self.firstParam = self.params[0]['name']
  67. return self.firstParam
  68. def get_error_msg(self):
  69. """Get error message ('' for no error)
  70. """
  71. return self.errorMsg
  72. def get_name(self):
  73. """Get task name
  74. """
  75. if sys.platform == 'win32':
  76. name, ext = os.path.splitext(self.name)
  77. if ext in ('.py', '.sh'):
  78. return name
  79. else:
  80. return self.name
  81. return self.name
  82. def get_description(self, full=True):
  83. """Get module's description
  84. :param bool full: True for label + desc
  85. """
  86. if self.label:
  87. if full:
  88. return self.label + ' ' + self.description
  89. else:
  90. return self.label
  91. else:
  92. return self.description
  93. def get_keywords(self):
  94. """Get module's keywords
  95. """
  96. return self.keywords
  97. def get_list_params(self, element='name'):
  98. """Get list of parameters
  99. :param str element: element name
  100. """
  101. params = []
  102. for p in self.params:
  103. params.append(p[element])
  104. return params
  105. def get_list_flags(self, element='name'):
  106. """Get list of flags
  107. :param str element: element name
  108. """
  109. flags = []
  110. for p in self.flags:
  111. flags.append(p[element])
  112. return flags
  113. def get_param(self, value, element='name', raiseError=True):
  114. """Find and return a param by name
  115. :param value: param's value
  116. :param str element: element name
  117. :param bool raiseError: True for raise on error
  118. """
  119. for p in self.params:
  120. val = p.get(element, None)
  121. if val is None:
  122. continue
  123. if isinstance(val, (list, tuple)):
  124. if value in val:
  125. return p
  126. elif isinstance(val, (bytes, unicode)):
  127. if p[element][:len(value)] == value:
  128. return p
  129. else:
  130. if p[element] == value:
  131. return p
  132. if raiseError:
  133. raise ValueError(_("Parameter element '%(element)s' not found: '%(value)s'") %
  134. {'element': element, 'value': value })
  135. else:
  136. return None
  137. def get_flag(self, aFlag):
  138. """Find and return a flag by name
  139. Raises ValueError when the flag is not found.
  140. :param str aFlag: name of the flag
  141. """
  142. for f in self.flags:
  143. if f['name'] == aFlag:
  144. return f
  145. raise ValueError(_("Flag not found: %s") % aFlag)
  146. def get_cmd_error(self):
  147. """Get error string produced by get_cmd(ignoreErrors = False)
  148. :return: list of errors
  149. """
  150. errorList = list()
  151. # determine if suppress_required flag is given
  152. for f in self.flags:
  153. if f['value'] and f['suppress_required']:
  154. return errorList
  155. for p in self.params:
  156. if not p.get('value', '') and p.get('required', False):
  157. if not p.get('default', ''):
  158. desc = p.get('label', '')
  159. if not desc:
  160. desc = p['description']
  161. errorList.append(_("Parameter '%(name)s' (%(desc)s) is missing.") %
  162. {'name': p['name'], 'desc': desc})
  163. return errorList
  164. def get_cmd(self, ignoreErrors=False, ignoreRequired=False,
  165. ignoreDefault=True):
  166. """Produce an array of command name and arguments for feeding
  167. into some execve-like command processor.
  168. :param bool ignoreErrors: True to return whatever has been built so
  169. far, even though it would not be a correct
  170. command for GRASS
  171. :param bool ignoreRequired: True to ignore required flags, otherwise
  172. '@<required@>' is shown
  173. :param bool ignoreDefault: True to ignore parameters with default values
  174. """
  175. cmd = [self.get_name()]
  176. suppress_required = False
  177. for flag in self.flags:
  178. if flag['value']:
  179. if len(flag['name']) > 1: # e.g. overwrite
  180. cmd += ['--' + flag['name']]
  181. else:
  182. cmd += ['-' + flag['name']]
  183. if flag['suppress_required']:
  184. suppress_required = True
  185. for p in self.params:
  186. if p.get('value', '') == '' and p.get('required', False):
  187. if p.get('default', '') != '':
  188. cmd += ['%s=%s' % (p['name'], p['default'])]
  189. elif ignoreErrors and not suppress_required and not ignoreRequired:
  190. cmd += ['%s=%s' % (p['name'], _('<required>'))]
  191. elif p.get('value', '') == '' and p.get('default', '') != '' and not ignoreDefault:
  192. cmd += ['%s=%s' % (p['name'], p['default'])]
  193. elif p.get('value', '') != '' and \
  194. (p['value'] != p.get('default', '') or not ignoreDefault):
  195. # output only values that have been set, and different from defaults
  196. cmd += ['%s=%s' % (p['name'], p['value'])]
  197. errList = self.get_cmd_error()
  198. if ignoreErrors is False and errList:
  199. raise ValueError('\n'.join(errList))
  200. return cmd
  201. def get_options(self):
  202. """Get options
  203. """
  204. return {'flags': self.flags, 'params': self.params}
  205. def has_required(self):
  206. """Check if command has at least one required parameter
  207. """
  208. for p in self.params:
  209. if p.get('required', False):
  210. return True
  211. return False
  212. def set_param(self, aParam, aValue, element='value'):
  213. """Set param value/values.
  214. """
  215. try:
  216. param = self.get_param(aParam)
  217. except ValueError:
  218. return
  219. param[element] = aValue
  220. def set_flag(self, aFlag, aValue, element='value'):
  221. """Enable / disable flag.
  222. """
  223. try:
  224. param = self.get_flag(aFlag)
  225. except ValueError:
  226. return
  227. param[element] = aValue
  228. def set_options(self, opts):
  229. """Set flags and parameters
  230. :param opts list of flags and parameters"""
  231. for opt in opts:
  232. if opt[0] == '-': # flag
  233. self.set_flag(opt.lstrip('-'), True)
  234. else: # parameter
  235. key, value = opt.split('=', 1)
  236. self.set_param(key, value)
  237. class processTask:
  238. """A ElementTree handler for the --interface-description output,
  239. as defined in grass-interface.dtd. Extend or modify this and the
  240. DTD if the XML output of GRASS' parser is extended or modified.
  241. :param tree: root tree node
  242. :param task: grassTask instance or None
  243. :param blackList: list of flags/params to hide
  244. :return: grassTask instance
  245. """
  246. def __init__(self, tree, task=None, blackList=None):
  247. if task:
  248. self.task = task
  249. else:
  250. self.task = grassTask()
  251. if blackList:
  252. self.task.blackList = blackList
  253. self.root = tree
  254. self._process_module()
  255. self._process_params()
  256. self._process_flags()
  257. self.task.define_first()
  258. def _process_module(self):
  259. """Process module description
  260. """
  261. self.task.name = self.root.get('name', default='unknown')
  262. # keywords
  263. for keyword in self._get_node_text(self.root, 'keywords').split(','):
  264. self.task.keywords.append(keyword.strip())
  265. self.task.label = self._get_node_text(self.root, 'label')
  266. self.task.description = self._get_node_text(self.root, 'description')
  267. def _process_params(self):
  268. """Process parameters
  269. """
  270. for p in self.root.findall('parameter'):
  271. # gisprompt
  272. node_gisprompt = p.find('gisprompt')
  273. gisprompt = False
  274. age = element = prompt = None
  275. if node_gisprompt is not None:
  276. gisprompt = True
  277. age = node_gisprompt.get('age', '')
  278. element = node_gisprompt.get('element', '')
  279. prompt = node_gisprompt.get('prompt', '')
  280. # value(s)
  281. values = []
  282. values_desc = []
  283. node_values = p.find('values')
  284. if node_values is not None:
  285. for pv in node_values.findall('value'):
  286. values.append(self._get_node_text(pv, 'name'))
  287. desc = self._get_node_text(pv, 'description')
  288. if desc:
  289. values_desc.append(desc)
  290. # keydesc
  291. key_desc = []
  292. node_key_desc = p.find('keydesc')
  293. if node_key_desc is not None:
  294. for ki in node_key_desc.findall('item'):
  295. key_desc.append(ki.text)
  296. if p.get('multiple', 'no') == 'yes':
  297. multiple = True
  298. else:
  299. multiple = False
  300. if p.get('required', 'no') == 'yes':
  301. required = True
  302. else:
  303. required = False
  304. if self.task.blackList['enabled'] and \
  305. self.task.name in self.task.blackList['items'] and \
  306. p.get('name') in self.task.blackList['items'][self.task.name].get('params', []):
  307. hidden = True
  308. else:
  309. hidden = False
  310. self.task.params.append({
  311. "name": p.get('name'),
  312. "type": p.get('type'),
  313. "required": required,
  314. "multiple": multiple,
  315. "label": self._get_node_text(p, 'label'),
  316. "description": self._get_node_text(p, 'description'),
  317. 'gisprompt': gisprompt,
  318. 'age': age,
  319. 'element': element,
  320. 'prompt': prompt,
  321. "guisection": self._get_node_text(p, 'guisection'),
  322. "guidependency": self._get_node_text(p, 'guidependency'),
  323. "default": self._get_node_text(p, 'default'),
  324. "values": values,
  325. "values_desc": values_desc,
  326. "value": '',
  327. "key_desc": key_desc,
  328. "hidden": hidden
  329. })
  330. def _process_flags(self):
  331. """Process flags
  332. """
  333. for p in self.root.findall('flag'):
  334. if self.task.blackList['enabled'] and \
  335. self.task.name in self.task.blackList['items'] and \
  336. p.get('name') in self.task.blackList['items'][self.task.name].get('flags', []):
  337. hidden = True
  338. else:
  339. hidden = False
  340. if p.find('suppress_required') is not None:
  341. suppress_required = True
  342. else:
  343. suppress_required = False
  344. self.task.flags.append({
  345. "name": p.get('name'),
  346. "label": self._get_node_text(p, 'label'),
  347. "description": self._get_node_text(p, 'description'),
  348. "guisection": self._get_node_text(p, 'guisection'),
  349. "suppress_required": suppress_required,
  350. "value": False,
  351. "hidden": hidden
  352. } )
  353. def _get_node_text(self, node, tag, default=''):
  354. """Get node text"""
  355. p = node.find(tag)
  356. if p is not None:
  357. res = ' '.join(p.text.split())
  358. return res
  359. return default
  360. def get_task(self):
  361. """Get grassTask instance"""
  362. return self.task
  363. def convert_xml_to_utf8(xml_text):
  364. # enc = locale.getdefaultlocale()[1]
  365. # modify: fetch encoding from the interface description text(xml)
  366. # e.g. <?xml version="1.0" encoding="GBK"?>
  367. pattern = re.compile(b'<\?xml[^>]*\Wencoding="([^"]*)"[^>]*\?>')
  368. m = re.match(pattern, xml_text)
  369. if m is None:
  370. return xml_text.encode("utf-8") if xml_text else None
  371. #
  372. enc = m.groups()[0]
  373. # modify: change the encoding to "utf-8", for correct parsing
  374. xml_text_utf8 = xml_text.decode(enc.decode('ascii')).encode("utf-8")
  375. p = re.compile(b'encoding="' + enc + b'"', re.IGNORECASE)
  376. xml_text_utf8 = p.sub(b'encoding="utf-8"', xml_text_utf8)
  377. return xml_text_utf8
  378. def get_interface_description(cmd):
  379. """Returns the XML description for the GRASS cmd (force text encoding to
  380. "utf-8").
  381. The DTD must be located in $GISBASE/gui/xml/grass-interface.dtd,
  382. otherwise the parser will not succeed.
  383. :param cmd: command (name of GRASS module)
  384. """
  385. try:
  386. p = Popen([cmd, '--interface-description'], stdout=PIPE,
  387. stderr=PIPE)
  388. cmdout, cmderr = p.communicate()
  389. # TODO: do it better (?)
  390. if not cmdout and sys.platform == 'win32':
  391. # we in fact expect pure module name (without extension)
  392. # so, lets remove extension
  393. if cmd.endswith('.py'):
  394. cmd = os.path.splitext(cmd)[0]
  395. if cmd == 'd.rast3d':
  396. sys.path.insert(0, os.path.join(os.getenv('GISBASE'),
  397. 'gui', 'scripts'))
  398. p = Popen([sys.executable, get_real_command(cmd),
  399. '--interface-description'],
  400. stdout=PIPE, stderr=PIPE)
  401. cmdout, cmderr = p.communicate()
  402. if cmd == 'd.rast3d':
  403. del sys.path[0] # remove gui/scripts from the path
  404. if p.returncode != 0:
  405. raise ScriptError(_("Unable to fetch interface description for command '<{cmd}>'."
  406. "\n\nDetails: <{det}>").format(cmd=cmd, det=decode(cmderr)))
  407. except OSError as e:
  408. raise ScriptError(_("Unable to fetch interface description for command '<{cmd}>'."
  409. "\n\nDetails: <{det}>").format(cmd=cmd, det=e))
  410. desc = convert_xml_to_utf8(cmdout)
  411. desc = desc.replace(b'grass-interface.dtd',
  412. os.path.join(os.getenv('GISBASE'), 'gui', 'xml',
  413. 'grass-interface.dtd').encode('utf-8'))
  414. return desc
  415. def parse_interface(name, parser=processTask, blackList=None):
  416. """Parse interface of given GRASS module
  417. The *name* is either GRASS module name (of a module on path) or
  418. a full or relative path to an executable.
  419. :param str name: name of GRASS module to be parsed
  420. :param parser:
  421. :param blackList:
  422. """
  423. try:
  424. tree = etree.fromstring(get_interface_description(name))
  425. except ETREE_EXCEPTIONS as error:
  426. raise ScriptError(_("Cannot parse interface description of"
  427. "<{name}> module: {error}").format(name=name, error=error))
  428. task = parser(tree, blackList=blackList).get_task()
  429. # if name from interface is different than the originally
  430. # provided name, then the provided name is likely a full path needed
  431. # to actually run the module later
  432. # (processTask uses only the XML which does not contain the original
  433. # path used to execute the module)
  434. if task.name != name:
  435. task.path = name
  436. return task
  437. def command_info(cmd):
  438. """Returns meta information for any GRASS command as dictionary
  439. with entries for description, keywords, usage, flags, and
  440. parameters, e.g.
  441. >>> command_info('g.tempfile') # doctest: +NORMALIZE_WHITESPACE
  442. {'keywords': ['general', 'support'], 'params': [{'gisprompt': False,
  443. 'multiple': False, 'name': 'pid', 'guidependency': '', 'default': '',
  444. 'age': None, 'required': True, 'value': '', 'label': '', 'guisection': '',
  445. 'key_desc': [], 'values': [], 'values_desc': [], 'prompt': None,
  446. 'hidden': False, 'element': None, 'type': 'integer', 'description':
  447. 'Process id to use when naming the tempfile'}], 'flags': [{'description':
  448. "Dry run - don't create a file, just prints it's file name", 'value':
  449. False, 'label': '', 'guisection': '', 'suppress_required': False,
  450. 'hidden': False, 'name': 'd'}, {'description': 'Print usage summary',
  451. 'value': False, 'label': '', 'guisection': '', 'suppress_required': False,
  452. 'hidden': False, 'name': 'help'}, {'description': 'Verbose module output',
  453. 'value': False, 'label': '', 'guisection': '', 'suppress_required': False,
  454. 'hidden': False, 'name': 'verbose'}, {'description': 'Quiet module output',
  455. 'value': False, 'label': '', 'guisection': '', 'suppress_required': False,
  456. 'hidden': False, 'name': 'quiet'}], 'description': "Creates a temporary
  457. file and prints it's file name.", 'usage': 'g.tempfile pid=integer [--help]
  458. [--verbose] [--quiet]'}
  459. >>> command_info('v.buffer')
  460. ['vector', 'geometry', 'buffer']
  461. :param str cmd: the command to query
  462. """
  463. task = parse_interface(cmd)
  464. cmdinfo = {}
  465. cmdinfo['description'] = task.get_description()
  466. cmdinfo['keywords'] = task.get_keywords()
  467. cmdinfo['flags'] = flags = task.get_options()['flags']
  468. cmdinfo['params'] = params = task.get_options()['params']
  469. usage = task.get_name()
  470. flags_short = list()
  471. flags_long = list()
  472. for f in flags:
  473. fname = f.get('name', 'unknown')
  474. if len(fname) > 1:
  475. flags_long.append(fname)
  476. else:
  477. flags_short.append(fname)
  478. if len(flags_short) > 1:
  479. usage += ' [-' + ''.join(flags_short) + ']'
  480. for p in params:
  481. ptype = ','.join(p.get('key_desc', []))
  482. if not ptype:
  483. ptype = p.get('type', '')
  484. req = p.get('required', False)
  485. if not req:
  486. usage += ' ['
  487. else:
  488. usage += ' '
  489. usage += p['name'] + '=' + ptype
  490. if p.get('multiple', False):
  491. usage += '[,' + ptype + ',...]'
  492. if not req:
  493. usage += ']'
  494. for key in flags_long:
  495. usage += ' [--' + key + ']'
  496. cmdinfo['usage'] = usage
  497. return cmdinfo
  498. def cmdtuple_to_list(cmd):
  499. """Convert command tuple to list.
  500. :param tuple cmd: GRASS command to be converted
  501. :return: command in list
  502. """
  503. cmdList = []
  504. if not cmd:
  505. return cmdList
  506. cmdList.append(cmd[0])
  507. if 'flags' in cmd[1]:
  508. for flag in cmd[1]['flags']:
  509. cmdList.append('-' + flag)
  510. for flag in ('help', 'verbose', 'quiet', 'overwrite'):
  511. if flag in cmd[1] and cmd[1][flag] is True:
  512. cmdList.append('--' + flag)
  513. for k, v in cmd[1].items():
  514. if k in ('flags', 'help', 'verbose', 'quiet', 'overwrite'):
  515. continue
  516. if ' ' in v:
  517. v = '"%s"' % v
  518. cmdList.append('%s=%s' % (k, v))
  519. return cmdList
  520. def cmdlist_to_tuple(cmd):
  521. """Convert command list to tuple for run_command() and others
  522. :param list cmd: GRASS command to be converted
  523. :return: command as tuple
  524. """
  525. if len(cmd) < 1:
  526. return None
  527. dcmd = {}
  528. for item in cmd[1:]:
  529. if '=' in item: # params
  530. key, value = item.split('=', 1)
  531. dcmd[str(key)] = value.replace('"', '')
  532. elif item[:2] == '--': # long flags
  533. flag = item[2:]
  534. if flag in ('help', 'verbose', 'quiet', 'overwrite'):
  535. dcmd[str(flag)] = True
  536. elif len(item) == 2 and item[0] == '-': # -> flags
  537. if 'flags' not in dcmd:
  538. dcmd['flags'] = ''
  539. dcmd['flags'] += item[1]
  540. else: # unnamed parameter
  541. module = parse_interface(cmd[0])
  542. dcmd[module.define_first()] = item
  543. return (cmd[0], dcmd)
  544. def cmdstring_to_tuple(cmd):
  545. """Convert command string to tuple for run_command() and others
  546. :param str cmd: command to be converted
  547. :return: command as tuple
  548. """
  549. return cmdlist_to_tuple(split(cmd))