File: add-objects.py

package info (click to toggle)
gwyddion 2.57-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 48,860 kB
  • sloc: ansic: 405,916; python: 7,867; sh: 5,241; makefile: 4,507; xml: 3,786; cpp: 2,572; pascal: 418; perl: 154; ruby: 130
file content (104 lines) | stat: -rwxr-xr-x 3,197 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python2
# Written by Yeti <yeti@gwyddion.net>. Public domain.
import sys, re

debug = False

if len(sys.argv) < 3:
    print sys.argv[0], 'SECTION_FILE [--standard-files=FILE,...]'
    sys.exit(1)

title_re = re.compile(r'<TITLE>(?P<object>\w+)</TITLE>')
file_re = re.compile(r'<FILE>(?P<file>\w+)</FILE>')
section_file = sys.argv[1]
ignore_files = {}
for x in sys.argv[3:]:
    if x.startswith('--standard-files='):
        for f in x[len('--standard-files='):].split(','):
            ignore_files[f] = 1
    else:
        sys.stderr.write(sys.argv[0] + ': Unknown option ' + x + '\n')

fh = file(section_file, 'r')
lines = [line.strip() for line in fh.readlines()]
fh.close()
lines = [line for line in lines if line]

def add_std(tostd, ucname):
    tostd.add('GWY_IS_' + ucname)
    tostd.add('GWY_IS_' + ucname + '_CLASS')
    tostd.add('GWY_' + ucname)
    tostd.add('GWY_' + ucname + '_CLASS')
    tostd.add('GWY_' + ucname + '_GET_CLASS')
    tostd.add('GWY_TYPE_' + ucname)

sections = []
section = None
for line in lines:
    if line == '<SECTION>':
        assert section is None
        section = {'Symbols': []}
        continue

    if line == '</SECTION>':
        assert section is not None
        if section['FILE'] not in ignore_files and 'Standard' in section:
            tostd = set()
            substd = section['Standard']
            for symbol in substd:
                m = re.search(r'^GWY_(\w+)_GET_CLASS$', symbol)
                if m:
                    add_std(tostd, m.group(1))
                m = re.search(r'^GWY_TYPE_(\w+)$', symbol)
                if m:
                    add_std(tostd, m.group(1))
                m = re.search(r'^gwy_\w+_get_type$', symbol)
                if m:
                    tostd.add(symbol)

            if debug:
                print section['FILE'], tostd
            section['Symbols'].extend(symbol for symbol in substd
                                      if symbol not in tostd)
            section['Standard'] = [symbol for symbol in substd
                                   if symbol in tostd]
        section = None
        continue

    m = re.search(r'^<FILE>([^<]+)</FILE>', line)
    if m:
        assert section is not None
        section['FILE'] = m.group(1)
        sections.append(section)
        is_standard = False
        continue

    m = re.search(r'^<TITLE>([^<]+)</TITLE>', line)
    if m:
        assert section is not None
        section['TITLE'] = m.group(1)
        continue

    if line == '<SUBSECTION Standard>':
        is_standard = True
        section['Standard'] = []
        continue

    assert line[0] != '<'
    if is_standard:
        section['Standard'].append(line)
    else:
        section['Symbols'].append(line)

fh = file(section_file, 'w')
for section in sections:
    fh.write('<SECTION>\n')
    fh.write('<FILE>%s</FILE>\n' % (section['FILE']))
    if 'TITLE' in section:
        fh.write('<TITLE>%s</TITLE>\n' % (section['TITLE']))
    fh.write('\n'.join(section['Symbols']) + '\n')
    if 'Standard' in section:
        fh.write('<SUBSECTION Standard>\n')
        fh.write('\n'.join(section['Standard']) + '\n')
    fh.write('</SECTION>\n\n')
fh.close()