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

Subversion Repositories configurator

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

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
class ConfigFile:
13
 
14
    def __init__(self, filename):
15
 
16
        self.filename = filename
17
 
18
        self.title = os.path.basename(filename)
19
        self.file = open(filename, "r")
20
 
21
        self.config = {
22
            "comment": "//",
23
            "define": "`define",
24
            "command": "//="
25
        }
26
 
27
        self.lines = []
28
 
29
        current_tab = "Main"
30
        self.tabs = {current_tab: []}
31
        self.tabs_titles = [current_tab]
32
 
33
        re0 = re.compile("//\s(\w.+)")
34
        re1 = re.compile("\/\/=(valid)\s(.+)")
35 6 unneback
        re2 = re.compile("(\/\/)?`define\s+(\w+)((\s+([\w'\.\"]+))|(\s+\/\/\s*(.*)))?\s+")
36 3 linus
 
37
        current_title = ""
38
        current_valid = ""
39
        current_select = None
40
        current_ifdef = 0
41
 
42
        for n, row in enumerate(self.file):
43
 
44
            self.lines.append(row)
45
 
46
            if re.match("\s+", row):
47
                current_title = ""
48
                current_valid = ""
49
                continue
50
 
51
            if re.match("\/\/=select", row):
52
                current_select = ConfigOptionSelect(
53
                    title = current_title
54
                )
55
                continue
56
 
57
            if re.match("\/\/=end", row):
58
                self.tabs[current_tab].append(current_select)
59
                current_select = None
60
                continue
61
 
62 10 linus
            m = re.match("\/\/=comment(\s+)(.*)(\s*)", row)
63
 
64
            if m:
65
                self.tabs[current_tab].append(ConfigLabel(m.group(2)))
66
                continue
67
 
68 5 unneback
            if re.match("\s*`ifn?def.*", row):
69 3 linus
                current_ifdef += 1
70
 
71
            if re.match("\s*`endif.*", row):
72
                current_ifdef -= 1
73
 
74
            if current_ifdef > 0:
75
                continue
76
 
77
            m = re.match("\/\/=tab ([\w ]+)\s+", row)
78
 
79
            if m:
80
                current_tab = m.group(1)
81
                if current_tab not in self.tabs_titles:
82
                    self.tabs[current_tab] = []
83
                    self.tabs_titles.append(current_tab)
84
                continue
85
 
86
            m = re0.match(row)
87
 
88
            if m:
89
                current_title = m.group(1)
90
                continue
91
 
92
            m = re1.match(row)
93
 
94
            if m:
95
                current_valid = m.group(2)
96
                continue
97
 
98
            m = re2.match(row)
99
 
100
            if m:
101
                (current_select if (current_select is not None) else self.tabs[current_tab]).append(ConfigOption(
102
                    line = n,
103
                    name = m.group(2),
104
                    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))),
105
                    valid = current_valid,
106
                    checkbox = (m.group(4) is None),
107
                    default = (m.group(5) if (m.group(5) is not None) else (m.group(1) is None))
108
                ))
109
                current_title = ""
110
                current_valid = ""
111
                continue
112
 
113
 
114
        self.file.close()
115
 
116
 
117
    def save(self):
118
 
119
        for tab in self.tabs:
120
            for opt in self.tabs[tab]:
121
                opt.save(self)
122
 
123
        self.file = open(self.filename, "w")
124
 
125
        for line in self.lines:
126
            self.file.write(line.rstrip(" \n\r") + "\n")
127
 
128
        self.file.close()
129
 
130
 
131
 
132 10 linus
class ConfigLabel(gtk.Label):
133
 
134
    def __init__(self, text):
135
 
136
        gtk.Label.__init__(self, "\n" + text)
137
 
138
 
139
    def save(self, *args): pass
140
 
141
 
142 3 linus
class ConfigOption(gtk.HBox):
143
 
144
    def __init__(self, **kwargs):
145
 
146
        gtk.HBox.__init__(self, True)
147
 
148
        self.options = kwargs
149
 
150
        if kwargs['checkbox']:
151
            self.entry = gtk.CheckButton()
152
            self.entry.set_active(kwargs['default'])
153
        else:
154
            self.entry = gtk.Entry()
155
            self.entry.set_text(kwargs['default'])
156
 
157
        self.pack_start(gtk.Label(kwargs['title']))
158
        self.pack_start(self.entry)
159
 
160
 
161
    def save(self, cfgfile, comment = ""):
162
 
163
        s = ""
164
 
165
        if self.options['checkbox']:
166
            s += "" if self.entry.get_active() else "//"
167
 
168
        s += "`define "
169
        s += self.options['name']
170
 
171
        if not self.options['checkbox']:
172
            s += " "
173
            s += self.entry.get_text()
174
 
175
        if len(comment) > 0:
176
            s += " // " + comment
177
 
178
        cfgfile.lines[self.options['line']] = s
179
 
180
 
181
 
182
class ConfigOptionSelect(gtk.HBox):
183
 
184
    def __init__(self, **kwargs):
185
 
186
        gtk.HBox.__init__(self, True)
187
 
188
        self.opts = []
189
        self.options = kwargs
190
 
191
        self.entry = gtk.combo_box_new_text()
192
 
193
        self.pack_start(gtk.Label(kwargs['title']))
194
        self.pack_start(self.entry)
195
 
196
        self.entry.connect("changed", self.onchanged)
197
 
198
 
199
    def append(self, opt):
200
 
201
        self.opts.append(opt)
202
        self.entry.append_text(opt.options['title'])
203
 
204
        if opt.options['default']:
205
            self.entry.set_active(len(self.opts) - 1)
206
 
207
 
208
    def onchanged(self, *args):
209
 
210
        for i, opt in enumerate(self.opts):
211
            opt.entry.set_active(i == self.entry.get_active())
212
 
213
 
214
    def save(self, cfgfile):
215
 
216
        for opt in self.opts:
217
            opt.save(cfgfile, opt.options['title'])
218
 
219
 
220
 
221
class ConfigWindow(gtk.Window):
222
 
223
    def __init__(self, cfgfilename = None):
224
 
225
        gtk.Window.__init__(self)
226
 
227
        self.set_title("Configurator")
228
        self.set_default_size(400, 600)
229
 
230
        self.box = gtk.VBox()
231
        self.button = gtk.Button("Save")
232
        self.notebook = gtk.Notebook()
233
 
234
        self.add(self.box)
235
 
236
        self.box.pack_start(self.notebook, True)
237
        self.box.pack_start(self.button, False)
238
 
239
        self.button.connect('clicked', lambda b: self.save())
240
 
241
        if cfgfilename is not None:
242
            self.load(ConfigFile(cfgfilename))
243
 
244
 
245
    def load(self, cfg):
246
 
247
        self.set_title(cfg.title + " - Configurator")
248
 
249
        self.configfile = cfg
250
 
251
        for tab in cfg.tabs_titles:
252
            box = gtk.VBox()
253
            for opt in cfg.tabs[tab]:
254
                box.pack_start(opt, False)
255
            box.pack_start(gtk.Label(""), True)
256
            sw = gtk.ScrolledWindow()
257
            sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
258
            sw.add_with_viewport(box)
259
            self.notebook.append_page(sw, gtk.Label(tab))
260
 
261
        self.show_all()
262
 
263
 
264
    def save(self):
265
 
266
        self.configfile.save()
267
 
268
 
269
    def run(self):
270
 
271
        def ondel(win, *args):
272
            gtk.main_quit()
273
            win.hide()
274
            return True
275
 
276
        handler_id = self.connect("delete_event", ondel)
277
 
278
        gtk.main()
279
 
280
        self.disconnect(handler_id)
281
 
282
 
283
 
284
if __name__ == '__main__':
285
    ConfigWindow(sys.argv[1]).run()

powered by: WebSVN 2.1.0

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