File: update_menudata.py

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (176 lines) | stat: -rw-r--r-- 5,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""!
@brief Support script for wxGUI - only for developers needs. Updates
menudata.xml file.

Parse all GRASS modules in the search path ('bin' & 'script') and
updates: - description (i.e. help) - keywords

Prints warning for missing modules.

(C) 2008-2010 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.

Usage: python support/update_menudata.py [-d]

 -d - dry run (prints diff, file is not updated)

@author Martin Landa <landa.martin gmail.com>
"""

import os
import sys
import tempfile
try:
    import xml.etree.ElementTree as etree
except ImportError:
    import elementtree.ElementTree as etree # Python <= 2.4

from grass.script import core as grass
from grass.script import task as gtask

if __name__ == "__main__":
    sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'wxpython'))
from lmgr.menudata  import ManagerData
from core.globalvar import grassCmd

def parseModules():
    """!Parse modules' interface"""
    modules = dict()
    
    # list of modules to be ignored
    ignore =  [ 'g.mapsets_picker.py',
                'v.type_wrapper.py',
                'g.parser',
                'vcolors' ]
    
    count = len(grassCmd)
    i = 0
    for module in grassCmd:
        i += 1
        if i % 10 == 0:
            grass.info('* %d/%d' % (i, count))
        if module in ignore:
            continue
        try:
            interface = gtask.parse_interface(module)
        except Exception, e:
            grass.error(module + ': ' + str(e))
            continue
        modules[interface.name] = { 'label'   : interface.label,
                                    'desc'    : interface.description,
                                    'keywords': interface.keywords }
        
    return modules

def updateData(data, modules):
    """!Update menu data tree"""
    # list of modules to be ignored
    ignore = ['v.type_wrapper.py',
              'vcolors']
    
    menu_modules = list()
    for node in data.tree.getiterator():
        if node.tag != 'menuitem':
            continue

        item = dict()
        for child in node.getchildren():
            item[child.tag] = child.text
        
        if 'command' not in item:
            continue
        
        if item['command'] in ignore:
            continue
        
        module = item['command'].split(' ')[0]
        if module not in modules:
            grass.warning("'%s' not found in modules" % item['command'])
            continue
        
        if modules[module]['label']:
            desc = modules[module]['label']
        else:
            desc = modules[module]['desc']
        if node.find('handler').text == 'OnMenuCmd':
            node.find('help').text = desc
        
        if 'keywords' not in modules[module]:
            grass.warning('%s: keywords missing' % module)
        else:
            if node.find('keywords') is None:
                node.insert(2, etree.Element('keywords'))
                grass.warning("Adding tag 'keywords' to '%s'" % module)
            node.find('keywords').text = ','.join(modules[module]['keywords'])
        
        menu_modules.append(item['command'])

    for module in modules.keys():
        if module not in menu_modules:
            grass.warning("'%s' not available from the menu" % module)
    
def writeData(data, file = None):
    """!Write updated menudata.xml"""
    if file is None:
        file = os.path.join('xml', 'menudata.xml')
    
    try:
        data.tree.write(file)
    except IOError:
        print >> sys.stderr, "'%s' not found. Please run the script from 'gui/wxpython'." % file
        return
    
    try:
        f = open(file, 'a')
        try:
            f.write('\n')
        finally:
            f.close()
    except IOError:
        print >> sys.stderr, "ERROR: Unable to write to menudata file."
    
def main(argv = None):
    if argv is None:
        argv = sys.argv

    if len(argv) > 1 and argv[1] == '-d':
        printDiff = True
    else:
        printDiff = False

    if len(argv) > 1 and argv[1] == '-h':
        print >> sys.stderr, __doc__
        return 1
    
    nuldev = file(os.devnull, 'w+')
    grass.info("Step 1: running make...")
    grass.call(['make'], stderr = nuldev)
    grass.info("Step 2: parsing modules...")
    modules = dict()
    modules = parseModules()
    grass.info("Step 3: reading menu data...")
    data = ManagerData()
    grass.info("Step 4: updating menu data...")
    updateData(data, modules)
    
    if printDiff:
        tempFile = tempfile.NamedTemporaryFile()
        grass.info("Step 5: diff menu data...")
        writeData(data, tempFile.name)
        
        grass.call(['diff', '-u',
                    os.path.join('xml', 'menudata.xml'),
                    tempFile.name], stderr = nuldev)
    else:
        grass.info("Step 5: writing menu data (menudata.xml)...")
        writeData(data)
        
    return 0

if __name__ == '__main__':
    if os.getenv("GISBASE") is None:
        sys.exit("You must be in GRASS GIS to run this program.")
    
    sys.exit(main())