gmodules.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. """Specialized interfaces for invoking modules for testing framework
  3. Copyright (C) 2014 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS GIS
  6. for details.
  7. :authors: Vaclav Petras, Soeren Gebbert
  8. """
  9. import subprocess
  10. from grass.script.core import start_command
  11. from grass.script.utils import encode, decode
  12. from grass.exceptions import CalledModuleError
  13. from grass.pygrass.modules import Module
  14. from .utils import do_doctest_gettext_workaround
  15. class SimpleModule(Module):
  16. """Simple wrapper around pygrass.modules.Module to make sure that
  17. run\_, finish\_, stdout and stderr are set correctly.
  18. >>> mapcalc = SimpleModule('r.mapcalc', expression='test_a = 1',
  19. ... overwrite=True)
  20. >>> mapcalc.run()
  21. Module('r.mapcalc')
  22. >>> mapcalc.returncode
  23. 0
  24. >>> colors = SimpleModule('r.colors',
  25. ... map='test_a', rules='-', stdin_='1 red')
  26. >>> colors.run()
  27. Module('r.colors')
  28. >>> colors.returncode
  29. 0
  30. >>> str(colors.inputs.stdin)
  31. '1 red'
  32. >>> str(colors.outputs.stdout)
  33. ''
  34. >>> colors.outputs.stderr.strip()
  35. "Color table for raster map <test_a> set to 'rules'"
  36. """
  37. def __init__(self, cmd, *args, **kargs):
  38. for banned in ['stdout_', 'stderr_', 'finish_', 'run_']:
  39. if banned in kargs:
  40. raise ValueError('Do not set %s parameter'
  41. ', it would be overriden' % banned)
  42. kargs['stdout_'] = subprocess.PIPE
  43. kargs['stderr_'] = subprocess.PIPE
  44. kargs['finish_'] = True
  45. kargs['run_'] = False
  46. Module.__init__(self, cmd, *args, **kargs)
  47. def call_module(module, stdin=None,
  48. merge_stderr=False, capture_stdout=True, capture_stderr=True,
  49. **kwargs):
  50. r"""Run module with parameters given in `kwargs` and return its output.
  51. >>> print (call_module('g.region', flags='pg')) # doctest: +ELLIPSIS
  52. projection=...
  53. zone=...
  54. n=...
  55. s=...
  56. w=...
  57. >>> call_module('m.proj', flags='i', input='-', stdin="50.0 41.5")
  58. '8642890.65|6965155.61|0.00\n'
  59. >>> call_module('g.region', aabbbccc='notexist') # doctest: +IGNORE_EXCEPTION_DETAIL
  60. Traceback (most recent call last):
  61. ...
  62. CalledModuleError: Module run g.region ... ended with error
  63. If `stdin` is not set and `kwargs` contains ``input`` with value set
  64. to ``-`` (dash), the function raises an error.
  65. Note that ``input`` nor ``output`` parameters are used by this
  66. function itself, these are usually module parameters which this
  67. function just passes to it. However, when ``input`` is in parameters
  68. the function checks if its values is correct considering value of
  69. ``stdin`` parameter.
  70. :param str module: module name
  71. :param stdin: string to be used as module standard input (stdin) or `None`
  72. :param merge_stderr: if the standard error output should be merged with stdout
  73. :param kwargs: module parameters
  74. :returns: module standard output (stdout) as string or None if apture_stdout is False
  75. :raises CalledModuleError: if module return code is non-zero
  76. :raises ValueError: if the parameters are not correct
  77. .. note::
  78. The data read is buffered in memory, so do not use this method
  79. if the data size is large or unlimited.
  80. """
  81. # TODO: remove this:
  82. do_doctest_gettext_workaround()
  83. # implementation inspired by subprocess.check_output() function
  84. if stdin:
  85. if 'input' in kwargs and kwargs['input'] != '-':
  86. raise ValueError(_("input='-' must be used when stdin is specified"))
  87. if stdin == subprocess.PIPE:
  88. raise ValueError(_("stdin must be string or buffer, not PIPE"))
  89. kwargs['stdin'] = subprocess.PIPE # to be able to send data to stdin
  90. elif 'input' in kwargs and kwargs['input'] == '-':
  91. raise ValueError(_("stdin must be used when input='-'"))
  92. if merge_stderr and not (capture_stdout and capture_stderr):
  93. raise ValueError(_("You cannot merge stdout and stderr and not capture them"))
  94. if 'stdout' in kwargs:
  95. raise TypeError(_("stdout argument not allowed, it could be overridden"))
  96. if 'stderr' in kwargs:
  97. raise TypeError(_("stderr argument not allowed, it could be overridden"))
  98. if capture_stdout:
  99. kwargs['stdout'] = subprocess.PIPE
  100. if capture_stderr:
  101. if merge_stderr:
  102. kwargs['stderr'] = subprocess.STDOUT
  103. else:
  104. kwargs['stderr'] = subprocess.PIPE
  105. process = start_command(module, **kwargs)
  106. # input=None means no stdin (our default)
  107. # for no stdout, output is None which is out interface
  108. # for stderr=STDOUT or no stderr, errors is None
  109. # which is fine for CalledModuleError
  110. output, errors = process.communicate(input=encode(decode(stdin)) if stdin else None)
  111. returncode = process.poll()
  112. if returncode:
  113. raise CalledModuleError(returncode, module, kwargs, errors)
  114. return decode(output) if output else None