| 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 |
|
|---|
| 6 |
|
|---|
| 7 | from grass.script.core import get_commands
|
|---|
| 8 | from grass.pygrass.modules.interface import Module
|
|---|
| 9 |
|
|---|
| 10 | _CMDS = list(get_commands()[0])
|
|---|
| 11 | _CMDS.sort()
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | class MetaModule(object):
|
|---|
| 15 | """Example how to use MetaModule
|
|---|
| 16 |
|
|---|
| 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 | u'...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 |
|
|---|
| 53 | def __dir__(self):
|
|---|
| 54 | return [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,
|
|---|
| 59 | name.strip('_').replace('_', '.')))
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | # https://grass.osgeo.org/grass77/manuals/full_index.html
|
|---|
| 63 | #[ d.* | db.* | g.* | i.* | m.* | ps.* | r.* | r3.* | t.* | v.* ]
|
|---|
| 64 | #
|
|---|
| 65 | # d.* display commands
|
|---|
| 66 | # db.* database commands
|
|---|
| 67 | # g.* general commands
|
|---|
| 68 | # i.* imagery commands
|
|---|
| 69 | # m.* miscellaneous commands
|
|---|
| 70 | # ps.* postscript commands
|
|---|
| 71 | # r.* raster commands
|
|---|
| 72 | # r3.* 3D raster commands
|
|---|
| 73 | # t.* temporal commands
|
|---|
| 74 | # v.* vector commands
|
|---|
| 75 |
|
|---|
| 76 | display = MetaModule('d')
|
|---|
| 77 | database = MetaModule('db')
|
|---|
| 78 | general = MetaModule('g')
|
|---|
| 79 | imagery = MetaModule('i')
|
|---|
| 80 | miscellaneous = MetaModule('m')
|
|---|
| 81 | postscript = MetaModule('ps')
|
|---|
| 82 | raster = MetaModule('r')
|
|---|
| 83 | raster3d = MetaModule('r3')
|
|---|
| 84 | temporal = MetaModule('t')
|
|---|
| 85 | vector = MetaModule('v')
|
|---|