module.py 19 KB

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