shortcuts.py 2.5 KB

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