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

Subversion Repositories configurator

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

Go to most recent revision | 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
 
140
 
141
    def save(self, *args): pass
142
 
143
 
144 3 linus
class ConfigOption(gtk.HBox):
145
 
146
    def __init__(self, **kwargs):
147
 
148
        gtk.HBox.__init__(self, True)
149
 
150
        self.options = kwargs
151
 
152
        if kwargs['checkbox']:
153
            self.entry = gtk.CheckButton()
154
            self.entry.set_active(kwargs['default'])
155
        else:
156
            self.entry = gtk.Entry()
157
            self.entry.set_text(kwargs['default'])
158
 
159
        self.pack_start(gtk.Label(kwargs['title']))
160
        self.pack_start(self.entry)
161
 
162
 
163
    def save(self, cfgfile, comment = ""):
164
 
165
        s = ""
166
 
167
        if self.options['checkbox']:
168
            s += "" if self.entry.get_active() else "//"
169
 
170
        s += "`define "
171
        s += self.options['name']
172
 
173
        if not self.options['checkbox']:
174
            s += " "
175
            s += self.entry.get_text()
176
 
177
        if len(comment) > 0:
178
            s += " // " + comment
179
 
180
        cfgfile.lines[self.options['line']] = s
181
 
182
 
183
 
184
class ConfigOptionSelect(gtk.HBox):
185
 
186
    def __init__(self, **kwargs):
187
 
188
        gtk.HBox.__init__(self, True)
189
 
190
        self.opts = []
191
        self.options = kwargs
192
 
193
        self.entry = gtk.combo_box_new_text()
194
 
195
        self.pack_start(gtk.Label(kwargs['title']))
196
        self.pack_start(self.entry)
197
 
198
        self.entry.connect("changed", self.onchanged)
199
 
200
 
201
    def append(self, opt):
202
 
203
        self.opts.append(opt)
204
        self.entry.append_text(opt.options['title'])
205
 
206
        if opt.options['default']:
207
            self.entry.set_active(len(self.opts) - 1)
208
 
209
 
210
    def onchanged(self, *args):
211
 
212
        for i, opt in enumerate(self.opts):
213
            opt.entry.set_active(i == self.entry.get_active())
214
 
215
 
216
    def save(self, cfgfile):
217
 
218
        for opt in self.opts:
219
            opt.save(cfgfile, opt.options['title'])
220
 
221
 
222
 
223
class ConfigWindow(gtk.Window):
224
 
225
    def __init__(self, cfgfilename = None):
226
 
227 11 linus
        INSTANCES.append(self)
228
 
229 3 linus
        gtk.Window.__init__(self)
230
 
231
        self.set_title("Configurator")
232
        self.set_default_size(400, 600)
233
 
234
        self.box = gtk.VBox()
235
        self.notebook = gtk.Notebook()
236 11 linus
        self.toolbar = ConfigToolbar(self)
237 3 linus
 
238
        self.add(self.box)
239
 
240 11 linus
        self.box.pack_start(self.toolbar, False)
241 3 linus
        self.box.pack_start(self.notebook, True)
242
 
243
        if cfgfilename is not None:
244 11 linus
            self.load(cfgfilename)
245
        else:
246
            self.configfile = None
247 3 linus
 
248 11 linus
        self.connect("delete_event", self.close)
249
        self.show_all()
250
 
251 3 linus
 
252 11 linus
    def load(self, cfgfilename):
253 3 linus
 
254 11 linus
        cfg = ConfigFile(cfgfilename)
255
 
256 3 linus
        self.set_title(cfg.title + " - Configurator")
257
 
258
        self.configfile = cfg
259
 
260
        for tab in cfg.tabs_titles:
261
            box = gtk.VBox()
262
            for opt in cfg.tabs[tab]:
263
                box.pack_start(opt, False)
264
            box.pack_start(gtk.Label(""), True)
265
            sw = gtk.ScrolledWindow()
266
            sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
267
            sw.add_with_viewport(box)
268
            self.notebook.append_page(sw, gtk.Label(tab))
269
 
270
        self.show_all()
271
 
272
 
273
    def save(self):
274
 
275
        self.configfile.save()
276
 
277
 
278 11 linus
    def close(self, *args):
279 3 linus
 
280 11 linus
        # Maybe check if file modified and ask to save it?
281
 
282
        INSTANCES.remove(self)
283
        self.destroy();
284
 
285
        if len(INSTANCES) == 0:
286 3 linus
            gtk.main_quit()
287
 
288 11 linus
 
289
 
290
class ConfigToolbar(gtk.Toolbar):
291
 
292
    def __init__(self, cfg):
293 3 linus
 
294 11 linus
        self.cfg = cfg
295 3 linus
 
296 11 linus
        gtk.Toolbar.__init__(self)
297 3 linus
 
298 11 linus
        self.insert_stock(gtk.STOCK_OPEN, "Open a file", None, self.open, None, -1)
299
        self.insert_stock(gtk.STOCK_SAVE, "Save the current file", None, self.save, None, -1)
300
        self.insert_stock(gtk.STOCK_CLOSE, "Close the current file", None, self.close, None, -1)
301
 
302 3 linus
 
303 11 linus
    def open(self, *args):
304
 
305
        chooser = gtk.FileChooserDialog(
306
            "Open file...", None,
307
            gtk.FILE_CHOOSER_ACTION_OPEN, (
308
            gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
309
            gtk.STOCK_OPEN, gtk.RESPONSE_OK
310
        ))
311
 
312
        if chooser.run() != gtk.RESPONSE_OK:
313
            return
314
 
315
        filename = chooser.get_filename()
316
 
317
        chooser.destroy()
318
 
319
        if self.cfg.configfile is None:
320
            self.cfg.load(filename)
321
        else:
322
            ConfigWindow(filename)
323
 
324
 
325
    def save(self, *args):
326
        self.cfg.save()
327
 
328
    def close(self, *args):
329
        self.cfg.close()
330
 
331 3 linus
 
332
if __name__ == '__main__':
333 11 linus
 
334
    if len(sys.argv) == 1:
335
        ConfigWindow()
336
 
337
    for f in sys.argv[1:]:
338
        ConfigWindow(f)
339
 
340
    gtk.main()
341
 

powered by: WebSVN 2.1.0

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