module.py 16 KB

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