module.py 17 KB

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