shortcuts.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. from __future__ import (
  3. nested_scopes,
  4. generators,
  5. division,
  6. absolute_import,
  7. with_statement,
  8. print_function,
  9. unicode_literals,
  10. )
  11. import fnmatch
  12. from grass.script.core import get_commands
  13. from grass.pygrass.modules.interface import Module
  14. _CMDS = list(get_commands()[0])
  15. _CMDS.sort()
  16. class MetaModule(object):
  17. """Example how to use MetaModule
  18. >>> g = MetaModule('g')
  19. >>> g_list = g.list
  20. >>> g_list.name
  21. 'g.list'
  22. >>> g_list.required
  23. ['type']
  24. >>> g_list.inputs.type = 'raster'
  25. >>> g_list.inputs.mapset = 'PERMANENT'
  26. >>> g_list.stdout_ = -1
  27. >>> g_list.run()
  28. Module('g.list')
  29. >>> g_list.outputs.stdout # doctest: +ELLIPSIS
  30. '...basin...elevation...'
  31. >>> r = MetaModule('r')
  32. >>> what = r.what
  33. >>> what.description
  34. 'Queries raster maps on their category values and category labels.'
  35. >>> what.inputs.map = 'elevation'
  36. >>> what.inputs.coordinates = [640000,220500] # doctest: +SKIP
  37. >>> what.run() # doctest: +SKIP
  38. >>> v = MetaModule('v')
  39. >>> v.import # doctest: +ELLIPSIS
  40. Traceback (most recent call last):
  41. File ".../doctest.py", line 1315, in __run
  42. compileflags, 1) in test.globs
  43. File "<doctest grass.pygrass.modules.shortcuts.MetaModule[16]>", line 1
  44. v.import
  45. ^
  46. SyntaxError: invalid syntax
  47. >>> v.import_
  48. Module('v.import')
  49. """
  50. def __init__(self, prefix, cls=None):
  51. self.prefix = prefix
  52. self.cls = cls if cls else Module
  53. def __dir__(self):
  54. return [
  55. mod[(len(self.prefix) + 1) :].replace(".", "_")
  56. for mod in fnmatch.filter(_CMDS, "%s.*" % self.prefix)
  57. ]
  58. def __getattr__(self, name):
  59. return self.cls("%s.%s" % (self.prefix, name.strip("_").replace("_", ".")))
  60. # https://grass.osgeo.org/grass79/manuals/full_index.html
  61. # [ d.* | db.* | g.* | i.* | m.* | ps.* | r.* | r3.* | t.* | v.* ]
  62. #
  63. # d.* display commands
  64. # db.* database commands
  65. # g.* general commands
  66. # i.* imagery commands
  67. # m.* miscellaneous commands
  68. # ps.* postscript commands
  69. # r.* raster commands
  70. # r3.* 3D raster commands
  71. # t.* temporal commands
  72. # v.* vector commands
  73. display = MetaModule("d")
  74. database = MetaModule("db")
  75. general = MetaModule("g")
  76. imagery = MetaModule("i")
  77. miscellaneous = MetaModule("m")
  78. postscript = MetaModule("ps")
  79. raster = MetaModule("r")
  80. raster3d = MetaModule("r3")
  81. temporal = MetaModule("t")
  82. vector = MetaModule("v")