OpenCores
URL https://opencores.org/ocsvn/configurator/configurator/trunk

Subversion Repositories configurator

[/] [configurator/] [trunk/] [src/] [Configurator.py] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 linus
#!/usr/bin/env python
2
#
3
# coding: utf8
4
#
5
 
6
import gtk
7
import re
8
 
9
import sys
10
import os.path
11
 
12 11 linus
INSTANCES = []
13
 
14 3 linus
class ConfigFile:
15
 
16
    def __init__(self, filename):
17
 
18
        self.filename = filename
19
 
20
        self.title = os.path.basename(filename)
21
        self.file = open(filename, "r")
22
 
23
        self.config = {
24
            "comment": "//",
25
            "define": "`define",
26
            "command": "//="
27
        }
28
 
29
        self.lines = []
30
 
31
        current_tab = "Main"
32
        self.tabs = {current_tab: []}
33
        self.tabs_titles = [current_tab]
34
 
35
        re0 = re.compile("//\s(\w.+)")
36
        re1 = re.compile("\/\/=(valid)\s(.+)")
37 6 unneback
        re2 = re.compile("(\/\/)?`define\s+(\w+)((\s+([\w'\.\"]+))|(\s+\/\/\s*(.*)))?\s+")
38 3 linus
 
39
        current_title = ""
40
        current_valid = ""
41
        current_select = None
42
        current_ifdef = 0
43
 
44
        for n, row in enumerate(self.file):
45
 
46
            self.lines.append(row)
47
 
48
            if re.match("\s+", row):
49
                current_title = ""
50
                current_valid = ""
51
                continue
52
 
53
            if re.match("\/\/=select", row):
54
                current_select = ConfigOptionSelect(
55
                    title = current_title
56
                )
57
                continue
58
 
59
            if re.match("\/\/=end", row):
60
                self.tabs[current_tab].append(current_select)
61
                current_select = None
62
                continue
63
 
64 10 linus
            m = re.match("\/\/=comment(\s+)(.*)(\s*)", row)
65
 
66
            if m:
67
                self.tabs[current_tab].append(ConfigLabel(m.group(2)))
68
                continue
69
 
70 5 unneback
            if re.match("\s*`ifn?def.*", row):
71 3 linus
                current_ifdef += 1
72
 
73
            if re.match("\s*`endif.*", row):
74
                current_ifdef -= 1
75
 
76
            if current_ifdef > 0:
77
                continue
78
 
79
            m = re.match("\/\/=tab ([\w ]+)\s+", row)
80
 
81
            if m:
82
                current_tab = m.group(1)
83
                if current_tab not in self.tabs_titles:
84
                    self.tabs[current_tab] = []
85
                    self.tabs_titles.append(current_tab)
86
                continue
87
 
88
            m = re0.match(row)
89
 
90
            if m:
91
                current_title = m.group(1)
92
                continue
93
 
94
            m = re1.match(row)
95
 
96
            if m:
97
                current_valid = m.group(2)
98
                continue
99
 
100
            m = re2.match(row)
101
 
102
            if m:
103
                (current_select if (current_select is not None) else self.tabs[current_tab]).append(ConfigOption(
104
                    line = n,
105
                    name = m.group(2),
106
                    title = ((m.group(7) if (m.group(7) is not None) else m.group(2)) if (current_select is not None) else (current_title if len(current_title) > 0 else m.group(2))),
107
                    valid = current_valid,
108
                    checkbox = (m.group(4) is None),
109
                    default = (m.group(5) if (m.group(5) is not None) else (m.group(1) is None))
110
                ))
111
                current_title = ""
112
                current_valid = ""
113
                continue
114
 
115
 
116
        self.file.close()
117
 
118
 
119
    def save(self):
120
 
121
        for tab in self.tabs:
122
            for opt in self.tabs[tab]:
123
                opt.save(self)
124
 
125
        self.file = open(self.filename, "w")
126
 
127
        for line in self.lines:
128
            self.file.write(line.rstrip(" \n\r") + "\n")
129
 
130
        self.file.close()
131
 
132
 
133
 
134 10 linus
class ConfigLabel(gtk.Label):
135
 
136
    def __init__(self, text):
137
 
138
        gtk.Label.__init__(self, "\n" + text)
139 12 unneback
        self.set_alignment(0, .5)
140
        self.set_use_markup(True)
141 10 linus
 
142
 
143
    def save(self, *args): pass
144
 
145
 
146 3 linus
class ConfigOption(gtk.HBox):
147
 
148
    def __init__(self, **kwargs):
149
 
150
        gtk.HBox.__init__(self, True)
151
 
152
        self.options = kwargs
153
 
154
        if kwargs['checkbox']:
155
            self.entry = gtk.CheckButton()
156
            self.entry.set_active(kwargs['default'])
157
        else:
158
            self.entry = gtk.Entry()
159
            self.entry.set_text(kwargs['default'])
160
 
161
        self.pack_start(gtk.Label(kwargs['title']))
162
        self.pack_start(self.entry)
163
 
164
 
165
    def save(self, cfgfile, comment = ""):
166
 
167
        s = ""
168
 
169
        if self.options['checkbox']:
170
            s += "" if self.entry.get_active() else "//"
171
 
172
        s += "`define "
173
        s += self.options['name']
174
 
175
        if not self.options['checkbox']:
176
            s += " "
177
            s += self.entry.get_text()
178
 
179
        if len(comment) > 0:
180
            s += " // " + comment
181
 
182
        cfgfile.lines[self.options['line']] = s
183
 
184
 
185
 
186
class ConfigOptionSelect(gtk.HBox):
187
 
188
    def __init__(self, **kwargs):
189
 
190
        gtk.HBox.__init__(self, True)
191
 
192
        self.opts = []
193
        self.options = kwargs
194
 
195
        self.entry = gtk.combo_box_new_text()
196
 
197
        self.pack_start(gtk.Label(kwargs['title']))
198
        self.pack_start(self.entry)
199
 
200
        self.entry.connect("changed", self.onchanged)
201
 
202
 
203
    def append(self, opt):
204
 
205
        self.opts.append(opt)
206
        self.entry.append_text(opt.options['title'])
207
 
208
        if opt.options['default']:
209
            self.entry.set_active(len(self.opts) - 1)
210
 
211
 
212
    def onchanged(self, *args):
213
 
214
        for i, opt in enumerate(self.opts):
215
            opt.entry.set_active(i == self.entry.get_active())
216
 
217
 
218
    def save(self, cfgfile):
219
 
220
        for opt in self.opts:
221
            opt.save(cfgfile, opt.options['title'])
222
 
223
 
224
 
225
class ConfigWindow(gtk.Window):
226
 
227
    def __init__(self, cfgfilename = None):
228
 
229 11 linus
        INSTANCES.append(self)
230
 
231 3 linus
        gtk.Window.__init__(self)
232
 
233
        self.set_title("Configurator")
234
        self.set_default_size(400, 600)
235
 
236
        self.box = gtk.VBox()
237
        self.notebook = gtk.Notebook()
238 11 linus
        self.toolbar = ConfigToolbar(self)
239 3 linus
 
240
        self.add(self.box)
241
 
242 11 linus
        self.box.pack_start(self.toolbar, False)
243 3 linus
        self.box.pack_start(self.notebook, True)
244
 
245
        if cfgfilename is not None:
246 11 linus
            self.load(cfgfilename)
247
        else:
248
            self.configfile = None
249 3 linus
 
250 11 linus
        self.connect("delete_event", self.close)
251
        self.show_all()
252
 
253 3 linus
 
254 11 linus
    def load(self, cfgfilename):
255 3 linus
 
256 11 linus
        cfg = ConfigFile(cfgfilename)
257
 
258 3 linus
        self.set_title(cfg.title + " - Configurator")
259
 
260
        self.configfile = cfg
261
 
262
        for tab in cfg.tabs_titles:
263
            box = gtk.VBox()
264
            for opt in cfg.tabs[tab]:
265
                box.pack_start(opt, False)
266
            box.pack_start(gtk.Label(""), True)
267
            sw = gtk.ScrolledWindow()
268
            sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
269
            sw.add_with_viewport(box)
270
            self.notebook.append_page(sw, gtk.Label(tab))
271
 
272
        self.show_all()
273
 
274
 
275
    def save(self):
276
 
277
        self.configfile.save()
278
 
279
 
280 11 linus
    def close(self, *args):
281 3 linus
 
282 11 linus
        # Maybe check if file modified and ask to save it?
283
 
284
        INSTANCES.remove(self)
285
        self.destroy();
286
 
287
        if len(INSTANCES) == 0:
288 3 linus
            gtk.main_quit()
289
 
290 11 linus
 
291
 
292
class ConfigToolbar(gtk.Toolbar):
293
 
294
    def __init__(self, cfg):
295 3 linus
 
296 11 linus
        self.cfg = cfg
297 3 linus
 
298 11 linus
        gtk.Toolbar.__init__(self)
299 3 linus
 
300 11 linus
        self.insert_stock(gtk.STOCK_OPEN, "Open a file", None, self.open, None, -1)
301
        self.insert_stock(gtk.STOCK_SAVE, "Save the current file", None, self.save, None, -1)
302
        self.insert_stock(gtk.STOCK_CLOSE, "Close the current file", None, self.close, None, -1)
303
 
304 3 linus
 
305 11 linus
    def open(self, *args):
306
 
307
        chooser = gtk.FileChooserDialog(
308
            "Open file...", None,
309
            gtk.FILE_CHOOSER_ACTION_OPEN, (
310
            gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
311
            gtk.STOCK_OPEN, gtk.RESPONSE_OK
312
        ))
313
 
314
        if chooser.run() != gtk.RESPONSE_OK:
315
            return
316
 
317
        filename = chooser.get_filename()
318
 
319
        chooser.destroy()
320
 
321
        if self.cfg.configfile is None:
322
            self.cfg.load(filename)
323
        else:
324
            ConfigWindow(filename)
325
 
326
 
327
    def save(self, *args):
328
        self.cfg.save()
329
 
330
    def close(self, *args):
331
        self.cfg.close()
332
 
333 3 linus
 
334
if __name__ == '__main__':
335 11 linus
 
336
    if len(sys.argv) == 1:
337
        ConfigWindow()
338
 
339
    for f in sys.argv[1:]:
340
        ConfigWindow(f)
341
 
342 12 unneback
    try: gtk.main()
343
    except KeyboardInterrupt: pass
344 11 linus
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.