module.py 18 KB

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