summaryrefslogtreecommitdiff
path: root/scangobj-merge.py
blob: 51660ea7e7fa243842d2c1e8c41e0b8f9ac7ff73 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

"""
parse, update and write .signals and .args files
"""

import sys
import os

def debug(*args):
    pass

# OrderedDict class based on
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
# Licensed under the Python License
class OrderedDict(dict):
    def __init__(self, dict = None):
        self._keys = []
        dict.__init__(self, dict)

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        self._keys.remove(key)

    def __setitem__(self, key, item):
        dict.__setitem__(self, key, item)
        if key not in self._keys: self._keys.append(key)

    def clear(self):
        dict.clear(self)
        self._keys = []

    def copy(self):
        dict = dict.copy(self)
        dict._keys = self._keys[:]
        return dict

    def items(self):
        return zip(self._keys, self.values())

    def keys(self):
        return self._keys

    def popitem(self):
        try:
            key = self._keys[-1]
        except IndexError:
            raise KeyError('dictionary is empty')

        val = self[key]
        del self[key]

        return (key, val)

    def setdefault(self, key, failobj = None):
        dict.setdefault(self, key, failobj)
        if key not in self._keys: self._keys.append(key)

    def update(self, dict):
        dict.update(self, dict)
        for key in dict.keys():
            if key not in self._keys: self._keys.append(key)

    def values(self):
        return map(self.get, self._keys)

class Object:
    def __init__(self, name):
        self._signals = OrderedDict()
        self._args = OrderedDict()
        self.name = name

    def __repr__(self):
        return "<Object %s>" % self.name

    def add_signal(self, signal, overwrite=True):
        if not overwrite and self._signals.has_key(signal.name):
            raise IndexError, "signal %s already in %r" % (signal.name, self)
        self._signals[signal.name] = signal

    def add_arg(self, arg, overwrite=True):
        if not overwrite and self._args.has_key(arg.name):
            raise IndexError, "arg %s already in %r" % (arg.name, self)
        self._args[arg.name] = arg

class Docable:
    def __init__(self, **kwargs):
        for key in self.attrs:
            setattr(self, key, kwargs[key])
        self.dict = kwargs

    def __repr__(self):
        return "<%r %s>" % (str(self.__class__), self.name)

class Signal(Docable):
    attrs = ['name', 'returns', 'args']

class Arg(Docable):
    attrs = ['name', 'type', 'range', 'flags', 'nick', 'blurb', 'default']

class GDoc:
    def load_file(self, filename):
        try:
            lines = open(filename).readlines()
            self.load_data("".join(lines))
        except IOError:
            print "WARNING - could not read from %s" % filename

    def save_file(self, filename, backup=False):
        """
        Save the signals information to the given .signals file if the
        file content changed.
        """
        olddata = None
        try:
            lines = open(filename).readlines()
            olddata = "".join(lines)
        except IOError:
            print "WARNING - could not read from %s" % filename
        newdata = self.get_data()
        if olddata and olddata == newdata:
            return

        if olddata:
            if backup:
                os.rename(filename, filename + '.bak')

        handle = open(filename, "w")
        handle.write(newdata)
        handle.close()

class Signals(GDoc):
    def __init__(self):
        self._objects = OrderedDict()

    def load_data(self, data):
        """
        Load the .signals lines, creating our list of objects and signals.
        """
        import re
        smatcher = re.compile(
            '(?s)'                                      # make . match \n
            '<SIGNAL>\n(.*?)</SIGNAL>\n'
            )
        nmatcher = re.compile(
            '<NAME>'
            '(?P<object>\S*)'                           # store object
            '::'
            '(?P<signal>\S*)'                           # store signal
            '</NAME>'
        )
        rmatcher = re.compile(
            '(?s)'                                      # make . match \n
            '<RETURNS>(?P<returns>\S*)</RETURNS>\n'     # store returns
            '(?P<args>.*)'                              # store args
        )
        for block in smatcher.findall(data):
            nmatch = nmatcher.search(block)
            if nmatch:
                o = nmatch.group('object')
                debug("Found object", o)
                debug("Found signal", nmatch.group('signal'))
                if not self._objects.has_key(o):
                    object = Object(o)
                    self._objects[o] = object

                rmatch = rmatcher.search(block)
                if rmatch:
                    dict = rmatch.groupdict().copy()
                    dict['name'] = nmatch.group('signal')
                    signal = Signal(**dict)
                    self._objects[o].add_signal(signal)

    def get_data(self):
        lines = []
        for o in self._objects.values():
            for s in o._signals.values():
                block = """<SIGNAL>
<NAME>%(object)s::%(name)s</NAME>
<RETURNS>%(returns)s</RETURNS>
%(args)s</SIGNAL>
"""
                d = s.dict.copy()
                d['object'] = o.name
                lines.append(block % d)

        return "\n".join(lines) + '\n'

class Args(GDoc):
    def __init__(self):
        self._objects = OrderedDict()

    def load_data(self, data):
        """
        Load the .args lines, creating our list of objects and args.
        """
        import re
        amatcher = re.compile(
            '(?s)'                                      # make . match \n
            '<ARG>\n(.*?)</ARG>\n'
            )
        nmatcher = re.compile(
            '<NAME>'
            '(?P<object>\S*)'                           # store object
            '::'
            '(?P<arg>\S*)'                              # store arg
            '</NAME>'
        )
        rmatcher = re.compile(
            '(?s)'                                      # make . match \n
            '<TYPE>(?P<type>\S*)</TYPE>\n'              # store type
            '<RANGE>(?P<range>.*?)</RANGE>\n'           # store range
            '<FLAGS>(?P<flags>\S*)</FLAGS>\n'           # store flags
            '<NICK>(?P<nick>.*?)</NICK>\n'              # store nick
            '<BLURB>(?P<blurb>.*?)</BLURB>\n'           # store blurb
            '<DEFAULT>(?P<default>.*?)</DEFAULT>\n'     # store default
        )
        for block in amatcher.findall(data):
            nmatch = nmatcher.search(block)
            if nmatch:
                o = nmatch.group('object')
                debug("Found object", o)
                debug("Found arg", nmatch.group('arg'))
                if not self._objects.has_key(o):
                    object = Object(o)
                    self._objects[o] = object

                rmatch = rmatcher.search(block)
                if rmatch:
                    dict = rmatch.groupdict().copy()
                    dict['name'] = nmatch.group('arg')
                    arg = Arg(**dict)
                    self._objects[o].add_arg(arg)
                else:
                    print "ERROR: could not match arg from block %s" % block

    def get_data(self):
        lines = []
        for o in self._objects.values():
            for a in o._args.values():
                block = """<ARG>
<NAME>%(object)s::%(name)s</NAME>
<TYPE>%(type)s</TYPE>
<RANGE>%(range)s</RANGE>
<FLAGS>%(flags)s</FLAGS>
<NICK>%(nick)s</NICK>
<BLURB>%(blurb)s</BLURB>
<DEFAULT>%(default)s</DEFAULT>
</ARG>
"""
                d = a.dict.copy()
                d['object'] = o.name
                lines.append(block % d)

        return "\n".join(lines) + '\n'

def main(argv):
    modulename = None
    try:
        modulename = argv[1]
    except IndexError:
        sys.stderr.write('Pleae provide a documentation module name\n')
        sys.exit(1)

    print "Merging scangobj output for %s" % modulename
    signals = Signals()
    signals.load_file(modulename + '.signals')
    signals.load_file(modulename + '.signals.new')
    signals.save_file(modulename + '.signals', backup=True)

    args = Args()
    args.load_file(modulename + '.args')
    args.load_file(modulename + '.args.new')
    args.save_file(modulename + '.args', backup=True)

main(sys.argv)