module.py 20 KB

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