shortcuts.py 2.6 KB

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