module.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. # -*- coding: utf-8 -*-
  2. from __future__ import (nested_scopes, generators, division, absolute_import,
  3. with_statement, print_function, unicode_literals)
  4. import sys
  5. from multiprocessing import cpu_count
  6. from functools import wraps
  7. if sys.version_info[0] == 2:
  8. from itertools import izip_longest as zip_longest
  9. else:
  10. from itertools import zip_longest
  11. from xml.etree.ElementTree import fromstring
  12. import time
  13. from grass.exceptions import CalledModuleError
  14. from grass.script.core import Popen, PIPE
  15. from grass.pygrass.errors import GrassError, ParameterError
  16. from grass.pygrass.functions import docstring_property
  17. from grass.pygrass.modules.interface.parameter import Parameter
  18. from grass.pygrass.modules.interface.flag import Flag
  19. from grass.pygrass.modules.interface.typedict import TypeDict
  20. from grass.pygrass.modules.interface.read import GETFROMTAG, DOC
  21. from grass.pygrass.messages import get_msgr
  22. def mdebug(level, msg='', extra=None):
  23. """Debug decorators for class methods.
  24. :param level: the debug level
  25. :type level: int
  26. :param msg: Debug message
  27. :type msg: str
  28. :param extra: Function that return a string
  29. :type msg: func
  30. """
  31. msgr = get_msgr()
  32. def decorator(method):
  33. @wraps(method)
  34. def wrapper(self, *args, **kargs):
  35. sargs = ', ' + ' , '.join([repr(a) for a in args]) if args else ''
  36. skargs = (' , '.join(['%s=%r' % (k, v) for k, v in kargs.items()])
  37. if kargs else '')
  38. opts = "%s%s%s" % (sargs, ',' if sargs and skargs else '', skargs)
  39. dmsg = "%s.%s(self%s): %s %s" % (self.__class__.__name__,
  40. method.__name__,
  41. opts, msg,
  42. extra(self, *args, **kargs)
  43. if extra else '')
  44. msgr.debug(level, dmsg)
  45. return method(self, *args, **kargs)
  46. return wrapper
  47. return decorator
  48. def _get_bash(self, *args, **kargs):
  49. return self.get_bash()
  50. class ParallelModuleQueue(object):
  51. """This class is designed to run an arbitrary number of pygrass Module
  52. processes in parallel.
  53. Objects of type grass.pygrass.modules.Module can be put into the
  54. queue using put() method. When the queue is full with the maximum
  55. number of parallel processes it will wait for all processes to finish,
  56. sets the stdout and stderr of the Module object and removes it
  57. from the queue when its finished.
  58. This class will raise a GrassError in case a Module process exits
  59. with a return code other than 0.
  60. Usage:
  61. >>> import copy
  62. >>> from grass.pygrass.modules import Module, ParallelModuleQueue
  63. >>> mapcalc_list = []
  64. >>> mapcalc = Module("r.mapcalc", overwrite=True, run_=False)
  65. >>> queue = ParallelModuleQueue(nprocs=3)
  66. >>> for i in xrange(5):
  67. ... new_mapcalc = copy.deepcopy(mapcalc)
  68. ... mapcalc_list.append(new_mapcalc)
  69. ... new_mapcalc(expression="test_pygrass_%i = %i"%(i, i))
  70. ... queue.put(new_mapcalc)
  71. Module('r.mapcalc')
  72. Module('r.mapcalc')
  73. Module('r.mapcalc')
  74. Module('r.mapcalc')
  75. Module('r.mapcalc')
  76. >>> queue.wait()
  77. >>> for mapcalc in mapcalc_list:
  78. ... print(mapcalc.popen.returncode)
  79. 0
  80. 0
  81. 0
  82. 0
  83. 0
  84. """
  85. def __init__(self, nprocs=1):
  86. """Constructor
  87. :param nprocs: The maximum number of Module processes that
  88. can be run in parallel, defualt is 1, if None
  89. then use all the available CPUs.
  90. :type nprocs: int
  91. """
  92. nprocs = int(nprocs) if nprocs else cpu_count()
  93. self._num_procs = nprocs
  94. self._list = nprocs * [None]
  95. self._proc_count = 0
  96. def put(self, module):
  97. """Put the next Module object in the queue
  98. To run the Module objects in parallel the run\_ and finish\_ options
  99. of the Module must be set to False.
  100. :param module: a preconfigured Module object with run\_ and finish\_
  101. set to False
  102. :type module: Module object
  103. """
  104. self._list[self._proc_count] = module
  105. # Force that finish is False, otherwise the execution
  106. # will not be parallel
  107. self._list[self._proc_count].finish_ = False
  108. self._list[self._proc_count].run()
  109. self._proc_count += 1
  110. if self._proc_count == self._num_procs:
  111. self.wait()
  112. def get(self, num):
  113. """Get a Module object from the queue
  114. :param num: the number of the object in queue
  115. :type num: int
  116. :returns: the Module object or None if num is not in the queue
  117. """
  118. if num < self._num_procs:
  119. return self._list[num]
  120. return None
  121. def get_num_run_procs(self):
  122. """Get the number of Module processes that are in the queue running
  123. or finished
  124. :returns: the maximum number fo Module processes running/finished in
  125. the queue
  126. """
  127. return len(self._list)
  128. def get_max_num_procs(self):
  129. """Return the maximum number of parallel Module processes
  130. """
  131. return self._num_procs
  132. def set_max_num_procs(self, nprocs):
  133. """Set the maximum number of Module processes that should run
  134. in parallel
  135. :param nprocs: The maximum number of Module processes that can be
  136. run in parallel
  137. :type nprocs: int
  138. """
  139. self._num_procs = int(nprocs)
  140. self.wait()
  141. def wait(self):
  142. """Wait for all Module processes that are in the list to finish
  143. and set the modules stdout and stderr output options
  144. """
  145. for proc in self._list:
  146. if proc:
  147. stdout, stderr = proc.popen.communicate(input=proc.stdin)
  148. proc.outputs['stdout'].value = stdout if stdout else ''
  149. proc.outputs['stderr'].value = stderr if stderr else ''
  150. if proc.popen.returncode != 0:
  151. GrassError(("Error running module %s") % (proc.name))
  152. self._list = self._num_procs * [None]
  153. self._proc_count = 0
  154. class Module(object):
  155. """This class is design to wrap/run/interact with the GRASS modules.
  156. The class during the init phase read the XML description generate using
  157. the ``--interface-description`` in order to understand which parameters
  158. are required which optionals. ::
  159. >>> from grass.pygrass.modules import Module
  160. >>> from subprocess import PIPE
  161. >>> import copy
  162. >>> region = Module("g.region")
  163. >>> region.flags.p = True # set flags
  164. >>> region.flags.u = True
  165. >>> region.flags["3"].value = True # set numeric flags
  166. >>> region.get_bash()
  167. u'g.region -p -3 -u'
  168. >>> new_region = copy.deepcopy(region)
  169. >>> new_region.inputs.res = "10"
  170. >>> new_region.get_bash()
  171. u'g.region res=10 -p -3 -u'
  172. >>> neighbors = Module("r.neighbors")
  173. >>> neighbors.inputs.input = "mapA"
  174. >>> neighbors.outputs.output = "mapB"
  175. >>> neighbors.inputs.size = 5
  176. >>> neighbors.inputs.quantile = 0.5
  177. >>> neighbors.get_bash()
  178. u'r.neighbors input=mapA method=average size=5 quantile=0.5 output=mapB'
  179. >>> new_neighbors1 = copy.deepcopy(neighbors)
  180. >>> new_neighbors1.inputs.input = "mapD"
  181. >>> new_neighbors1.inputs.size = 3
  182. >>> new_neighbors1.inputs.quantile = 0.5
  183. >>> new_neighbors1.get_bash()
  184. u'r.neighbors input=mapD method=average size=3 quantile=0.5 output=mapB'
  185. >>> new_neighbors2 = copy.deepcopy(neighbors)
  186. >>> new_neighbors2(input="mapD", size=3, run_=False)
  187. Module('r.neighbors')
  188. >>> new_neighbors2.get_bash()
  189. u'r.neighbors input=mapD method=average size=3 quantile=0.5 output=mapB'
  190. >>> neighbors = Module("r.neighbors")
  191. >>> neighbors.get_bash()
  192. u'r.neighbors method=average size=3'
  193. >>> new_neighbors3 = copy.deepcopy(neighbors)
  194. >>> new_neighbors3(input="mapA", size=3, output="mapB", run_=False)
  195. Module('r.neighbors')
  196. >>> new_neighbors3.get_bash()
  197. u'r.neighbors input=mapA method=average size=3 output=mapB'
  198. >>> mapcalc = Module("r.mapcalc", expression="test_a = 1",
  199. ... overwrite=True, run_=False)
  200. >>> mapcalc.run()
  201. Module('r.mapcalc')
  202. >>> mapcalc.popen.returncode
  203. 0
  204. >>> colors = Module("r.colors", map="test_a", rules="-",
  205. ... run_=False, stdout_=PIPE,
  206. ... stderr_=PIPE, stdin_="1 red")
  207. >>> colors.run()
  208. Module('r.colors')
  209. >>> colors.popen.returncode
  210. 0
  211. >>> colors.inputs["stdin"].value
  212. u'1 red'
  213. >>> colors.outputs["stdout"].value
  214. u''
  215. >>> colors.outputs["stderr"].value.strip()
  216. "Color table for raster map <test_a> set to 'rules'"
  217. >>> colors = Module("r.colors", map="test_a", rules="-",
  218. ... run_=False, finish_=False, stdin_=PIPE)
  219. >>> colors.run()
  220. Module('r.colors')
  221. >>> stdout, stderr = colors.popen.communicate(input="1 red")
  222. >>> colors.popen.returncode
  223. 0
  224. >>> stdout
  225. >>> stderr
  226. >>> colors = Module("r.colors", map="test_a", rules="-",
  227. ... run_=False, finish_=False,
  228. ... stdin_=PIPE, stderr_=PIPE)
  229. >>> colors.run()
  230. Module('r.colors')
  231. >>> stdout, stderr = colors.popen.communicate(input="1 red")
  232. >>> colors.popen.returncode
  233. 0
  234. >>> stdout
  235. >>> stderr.strip()
  236. "Color table for raster map <test_a> set to 'rules'"
  237. Run a second time
  238. >>> colors.run()
  239. Module('r.colors')
  240. >>> stdout, stderr = colors.popen.communicate(input="1 blue")
  241. >>> colors.popen.returncode
  242. 0
  243. >>> stdout
  244. >>> stderr.strip()
  245. "Color table for raster map <test_a> set to 'rules'"
  246. Multiple run test
  247. >>> colors = Module("r.colors", map="test_a",
  248. ... color="ryb", run_=False)
  249. >>> colors.run()
  250. Module('r.colors')
  251. >>> colors(color="gyr")
  252. Module('r.colors')
  253. >>> colors.run()
  254. Module('r.colors')
  255. >>> colors(color="ryg")
  256. Module('r.colors')
  257. >>> colors(stderr_=PIPE)
  258. Module('r.colors')
  259. >>> colors.run()
  260. Module('r.colors')
  261. >>> print(colors.outputs["stderr"].value.strip())
  262. Color table for raster map <test_a> set to 'ryg'
  263. >>> colors(color="byg")
  264. Module('r.colors')
  265. >>> colors(stdout_=PIPE)
  266. Module('r.colors')
  267. >>> colors.run()
  268. Module('r.colors')
  269. >>> print(colors.outputs["stderr"].value.strip())
  270. Color table for raster map <test_a> set to 'byg'
  271. Often in the Module class you can find ``*args`` and ``kwargs`` annotation
  272. in methods, like in the __call__ method.
  273. Python allow developers to not specify all the arguments and
  274. keyword arguments of a method or function. ::
  275. def f(*args):
  276. for arg in args:
  277. print arg
  278. therefore if we call the function like:
  279. >>> f('grass', 'gis', 'modules') # doctest: +SKIP
  280. grass
  281. gis
  282. modules
  283. or we can define a new list:
  284. >>> words = ['grass', 'gis', 'modules'] # doctest: +SKIP
  285. >>> f(*words) # doctest: +SKIP
  286. grass
  287. gis
  288. modules
  289. we can do the same with keyword arguments, rewrite the above function: ::
  290. def f(*args, **kargs):
  291. for arg in args:
  292. print arg
  293. for key, value in kargs.items():
  294. print "%s = %r" % (key, value)
  295. now we can use the new function, with:
  296. >>> f('grass', 'gis', 'modules', os = 'linux', language = 'python')
  297. ... # doctest: +SKIP
  298. grass
  299. gis
  300. modules
  301. os = 'linux'
  302. language = 'python'
  303. or, as before we can, define a dictionary and give the dictionary to
  304. the function, like:
  305. >>> keywords = {'os' : 'linux', 'language' : 'python'} # doctest: +SKIP
  306. >>> f(*words, **keywords) # doctest: +SKIP
  307. grass
  308. gis
  309. modules
  310. os = 'linux'
  311. language = 'python'
  312. In the Module class we heavily use this language feature to pass arguments
  313. and keyword arguments to the grass module.
  314. """
  315. def __init__(self, cmd, *args, **kargs):
  316. if isinstance(cmd, unicode):
  317. self.name = str(cmd)
  318. elif isinstance(cmd, str):
  319. self.name = cmd
  320. else:
  321. raise GrassError("Problem initializing the module {s}".format(s=cmd))
  322. try:
  323. # call the command with --interface-description
  324. get_cmd_xml = Popen([cmd, "--interface-description"], stdout=PIPE)
  325. except OSError as e:
  326. print("OSError error({0}): {1}".format(e.errno, e.strerror))
  327. str_err = "Error running: `%s --interface-description`."
  328. raise GrassError(str_err % self.name)
  329. # get the xml of the module
  330. self.xml = get_cmd_xml.communicate()[0]
  331. # transform and parse the xml into an Element class:
  332. # http://docs.python.org/library/xml.etree.elementtree.html
  333. tree = fromstring(self.xml)
  334. for e in tree:
  335. if e.tag not in ('parameter', 'flag'):
  336. self.__setattr__(e.tag, GETFROMTAG[e.tag](e))
  337. #
  338. # extract parameters from the xml
  339. #
  340. self.params_list = [Parameter(p) for p in tree.findall("parameter")]
  341. self.inputs = TypeDict(Parameter)
  342. self.outputs = TypeDict(Parameter)
  343. self.required = []
  344. # Insert parameters into input/output and required
  345. for par in self.params_list:
  346. if par.input:
  347. self.inputs[par.name] = par
  348. else:
  349. self.outputs[par.name] = par
  350. if par.required:
  351. self.required.append(par.name)
  352. #
  353. # extract flags from the xml
  354. #
  355. flags_list = [Flag(f) for f in tree.findall("flag")]
  356. self.flags = TypeDict(Flag)
  357. for flag in flags_list:
  358. self.flags[flag.name] = flag
  359. #
  360. # Add new attributes to the class
  361. #
  362. self.run_ = True
  363. self.finish_ = True
  364. self.env_ = None
  365. self.stdin_ = None
  366. self.stdin = None
  367. self.stdout_ = None
  368. self.stderr_ = None
  369. diz = {'name': 'stdin', 'required': False,
  370. 'multiple': False, 'type': 'all',
  371. 'value': None}
  372. self.inputs['stdin'] = Parameter(diz=diz)
  373. diz['name'] = 'stdout'
  374. self.outputs['stdout'] = Parameter(diz=diz)
  375. diz['name'] = 'stderr'
  376. self.outputs['stderr'] = Parameter(diz=diz)
  377. self.popen = None
  378. self.time = None
  379. if args or kargs:
  380. self.__call__(*args, **kargs)
  381. self.__call__.__func__.__doc__ = self.__doc__
  382. def __call__(self, *args, **kargs):
  383. """Set module paramters to the class and, if run_ is True execute the
  384. module, therefore valid parameters are all the module parameters
  385. plus some extra parameters that are: run_, stdin_, stdout_, stderr_,
  386. env_ and finish_.
  387. """
  388. if not args and not kargs:
  389. self.run()
  390. return self
  391. #
  392. # check for extra kargs, set attribute and remove from dictionary
  393. #
  394. if 'flags' in kargs:
  395. for flg in kargs['flags']:
  396. self.flags[flg].value = True
  397. del(kargs['flags'])
  398. # set attributs
  399. for key in ('run_', 'env_', 'finish_', 'stdout_', 'stderr_'):
  400. if key in kargs:
  401. setattr(self, key, kargs.pop(key))
  402. # set inputs
  403. for key in ('stdin_', ):
  404. if key in kargs:
  405. self.inputs[key[:-1]].value = kargs.pop(key)
  406. #
  407. # check args
  408. #
  409. for param, arg in zip(self.params_list, args):
  410. param.value = arg
  411. for key, val in kargs.items():
  412. if key in self.inputs:
  413. self.inputs[key].value = val
  414. elif key in self.outputs:
  415. self.outputs[key].value = val
  416. elif key in self.flags:
  417. # we need to add this, because some parameters (overwrite,
  418. # verbose and quiet) work like parameters
  419. self.flags[key].value = val
  420. else:
  421. raise ParameterError('%s is not a valid parameter.' % key)
  422. #
  423. # check if execute
  424. #
  425. if self.run_:
  426. #
  427. # check reqire parameters
  428. #
  429. for k in self.required:
  430. if ((k in self.inputs and self.inputs[k].value is None) or
  431. (k in self.outputs and self.outputs[k].value is None)):
  432. msg = "Required parameter <%s> not set."
  433. raise ParameterError(msg % k)
  434. return self.run()
  435. return self
  436. def get_bash(self):
  437. """Return a BASH rapresentation of the Module."""
  438. return ' '.join(self.make_cmd())
  439. def get_python(self):
  440. """Return a Python rapresentation of the Module."""
  441. prefix = self.name.split('.')[0]
  442. name = '_'.join(self.name.split('.')[1:])
  443. params = ', '.join([par.get_python() for par in self.params_list
  444. if par.get_python() != ''])
  445. flags = ''.join([flg.get_python()
  446. for flg in self.flags.values()
  447. if not flg.special and flg.get_python() != ''])
  448. special = ', '.join([flg.get_python()
  449. for flg in self.flags.values()
  450. if flg.special and flg.get_python() != ''])
  451. # pre name par flg special
  452. if flags and special:
  453. return "%s.%s(%s, flags=%r, %s)" % (prefix, name, params,
  454. flags, special)
  455. elif flags:
  456. return "%s.%s(%s, flags=%r)" % (prefix, name, params, flags)
  457. elif special:
  458. return "%s.%s(%s, %s)" % (prefix, name, params, special)
  459. else:
  460. return "%s.%s(%s)" % (prefix, name, params)
  461. def __str__(self):
  462. """Return the command string that can be executed in a shell"""
  463. return ' '.join(self.make_cmd())
  464. def __repr__(self):
  465. return "Module(%r)" % self.name
  466. @docstring_property(__doc__)
  467. def __doc__(self):
  468. """{cmd_name}({cmd_params})
  469. """
  470. head = DOC['head'].format(cmd_name=self.name,
  471. cmd_params=('\n' + # go to a new line
  472. # give space under the function name
  473. (' ' * (len(self.name) + 1))).join([', '.join(
  474. # transform each parameter in string
  475. [str(param) for param in line if param is not None])
  476. # make a list of parameters with only 3 param per line
  477. for line in zip_longest(*[iter(self.params_list)] * 3)]),)
  478. params = '\n'.join([par.__doc__ for par in self.params_list])
  479. flags = self.flags.__doc__
  480. return '\n'.join([head, params, DOC['flag_head'], flags, DOC['foot']])
  481. def get_dict(self):
  482. """Return a dictionary that includes the name, all valid
  483. inputs, outputs and flags
  484. """
  485. dic = {}
  486. dic['name'] = self.name
  487. dic['inputs'] = [(k, v.value) for k, v in self.inputs.items()
  488. if v.value]
  489. dic['outputs'] = [(k, v.value) for k, v in self.outputs.items()
  490. if v.value]
  491. dic['flags'] = [flg for flg in self.flags if self.flags[flg].value]
  492. return dic
  493. def make_cmd(self):
  494. """Create the command string that can be executed in a shell
  495. :returns: the command string
  496. """
  497. skip = ['stdin', 'stdout', 'stderr']
  498. args = [self.name, ]
  499. for key in self.inputs:
  500. if key not in skip and self.inputs[key].value:
  501. args.append(self.inputs[key].get_bash())
  502. for key in self.outputs:
  503. if key not in skip and self.outputs[key].value:
  504. args.append(self.outputs[key].get_bash())
  505. for flg in self.flags:
  506. if self.flags[flg].value:
  507. args.append(str(self.flags[flg]))
  508. return args
  509. @mdebug(1, extra=_get_bash)
  510. def run(self):
  511. """Run the module
  512. :param node:
  513. :type node:
  514. This function will wait for the process to terminate in case
  515. finish_==True and sets up stdout and stderr. If finish_==False this
  516. function will return after starting the process. Use
  517. self.popen.communicate() of self.popen.wait() to wait for the process
  518. termination. The handling of stdout and stderr must then be done
  519. outside of this function.
  520. """
  521. if self.inputs['stdin'].value:
  522. self.stdin = self.inputs['stdin'].value
  523. self.stdin_ = PIPE
  524. cmd = self.make_cmd()
  525. start = time.time()
  526. self.popen = Popen(cmd,
  527. stdin=self.stdin_,
  528. stdout=self.stdout_,
  529. stderr=self.stderr_,
  530. env=self.env_)
  531. if self.finish_:
  532. stdout, stderr = self.popen.communicate(input=self.stdin)
  533. self.outputs['stdout'].value = stdout if stdout else ''
  534. self.outputs['stderr'].value = stderr if stderr else ''
  535. self.time = time.time() - start
  536. if self.popen.poll():
  537. raise CalledModuleError(returncode=self.popen.returncode,
  538. code=self.get_bash(),
  539. module=self.name, errors=stderr)
  540. return self
  541. ###############################################################################
  542. if __name__ == "__main__":
  543. import doctest
  544. doctest.testmod()