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

Subversion Repositories configurator

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

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