-
Notifications
You must be signed in to change notification settings - Fork 1
/
glader_ui.py
468 lines (405 loc) · 16.1 KB
/
glader_ui.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python3
""" Glader - Gtk GUI
Provides the user interface for Glader.
-Christopher Welborn 09-15-2014
"""
import os
import sys
from glader_core import (
NAME,
VERSIONSTR,
import_fail,
)
from glader_util import (
settings,
GladeFile,
)
try:
from gi import require_version as gi_require_version
gi_require_version('Gtk', '3.0')
gi_require_version('GtkSource', '3.0')
from gi.repository import Gtk, GtkSource, GObject, Pango
except ImportError as eximp:
import_fail(eximp)
class App(Gtk.Window):
""" Main window with all components. """
def __init__(
self, filepath=None, outputfile=None,
dynamic_init=False, lib_mode=False):
Gtk.Window.__init__(self)
self.builder = Gtk.Builder()
# Register the GtkSourceView type.
GObject.type_register(GtkSource.View)
try:
scriptdir = os.path.abspath(sys.path[0])
gladefile = os.path.join(scriptdir, 'glader.glade')
self.builder.add_from_file(gladefile)
except Exception as ex:
print('\nError building main window!\n{}'.format(ex))
sys.exit(1)
# A GladeFile() instance set by generate_code().
self.glade = None
# Warnings issued already in generate_code().
# If a file needs a warning, the warning will be issued
# and it's filepath/warnings saved so that the warning is only
# issued once per file (with the same message).
self.warned_files = {}
# Get gui objects
self.btnFileOpen = self.builder.get_object('btnFileOpen')
if filepath:
# This will automatically trigger code generation
# because of btnFileOpen_selection_changed_cb()
self.btnFileOpen.set_filename(filepath)
self.btnGenerate = self.builder.get_object('btnGenerate')
self.btnSave = self.builder.get_object('btnSave')
self.chkDynamic = self.builder.get_object('chkDynamic')
if not dynamic_init:
# Load from settings if not set already.
dynamic_init = settings.get_bool('dynamic_init', default=False)
self.chkDynamic.set_active(dynamic_init)
self.chkLibMode = self.builder.get_object('chkLibMode')
if not lib_mode:
# Load from settings if not set already.
lib_mode = settings.get_bool('lib_mode', default=False)
self.chkLibMode.set_active(lib_mode)
self.comboTheme = self.builder.get_object('comboTheme')
# Initialize the cell renderer for the theme list.
celltheme = Gtk.CellRendererText()
self.comboTheme.pack_start(celltheme, True)
self.comboTheme.add_attribute(celltheme, 'text', 0)
# List store for the theme names.
self.listTheme = self.builder.get_object('listTheme')
self.lblFileOpen = self.builder.get_object('lblFileOpen')
self.lblOutput = self.builder.get_object('lblOutput')
self.scrollOutput = self.builder.get_object('scrollOutput')
# Build the SourceView and SourceBuffer with Python highlighting
self.bufferOutput = GtkSource.Buffer()
self.langManager = GtkSource.LanguageManager()
self.bufferLang = self.langManager.get_language('python3')
self.bufferOutput.set_language(self.bufferLang)
self.bufferOutput.set_highlight_syntax(True)
self.bufferOutput.set_highlight_matching_brackets(True)
# Set theme.
self.themeManager = GtkSource.StyleSchemeManager()
# Map from theme id to StyleScheme.
self.themes = {
tid: self.themeManager.get_scheme(tid)
for tid in self.themeManager.get_scheme_ids()
}
# Holds the currently selected theme info.
self.theme = None
# Load theme from config if available.
if not self.set_theme_config():
# Use first preferred theme if available.
themeprefs = (
'oblivion', 'tomorrownighteighties', 'twilight', 'kate'
)
for themeid in themeprefs:
theme = self.themes.get(themeid, None)
if theme:
self.set_theme(theme)
break
# Load theme names and select the chosen theme.
self.build_theme_list()
# Build actual view for the code.
self.srcviewOutput = self.builder.get_object('srcviewOutput')
self.srcviewOutput.set_buffer(self.bufferOutput)
self.srcviewOutput.modify_font(Pango.FontDescription('monospace'))
# This window.
self.winMain = self.builder.get_object('winMain')
# Initialize a MessageBoxes class to work with.
self.msgs = MessageBoxes(parent=self.winMain, title=NAME)
self.dlgSave = FileDialogSave(
parent=self.winMain,
title='Select an output file',
filters=(('Python Files', '*.py'), ('All Files', '*')),
filepath=outputfile
)
# Connect all signals.
self.builder.connect_signals(self)
# Title fix.
self.winMain.set_title(VERSIONSTR)
# Show the main window.
self.winMain.show_all()
def btnFileOpen_selection_changed_cb(self, widget, user_data=None):
""" Handler for btnFileOpen.selection-changed. """
filepath = widget.get_filename()
if filepath:
# Automatically generate code for selected files.
if self.warned_files.get(filepath, None):
# Manually re-opening the file resets the requirement warnings.
self.warned_files[filepath] = None
self.generate_code()
def btnGenerate_activate_cb(self, widget, user_data=None):
""" Handler for btnGenerate.activate. """
return self.btnGenerate_clicked_cb(user_data=user_data)
def btnGenerate_clicked_cb(self, widget, user_data=None):
""" Handler for btnGenerate.clicked """
return self.generate_code()
def btnSave_activate_cb(self, widget, user_data=None):
""" Handler for btnSave.activate. """
return self.btnSave_clicked_cb(user_data=user_data)
def btnSave_clicked_cb(self, widget, user_data=None):
""" Handler for btnSave.activate. """
return self.write_file()
def comboTheme_changed_cb(self, widget, user_data=None):
""" Handler for comboTheme.changed
Sets the current theme for srcviewOutput.
"""
# Selected TreeIter.
selitr = self.comboTheme.get_active_iter()
if selitr is None:
return None
# Value for column 0 (the theme name)
themename = self.listTheme.get_value(selitr, 0)
self.set_theme(themename)
def winMain_destroy_cb(self, widget, user_data=None):
""" Handler for winMain.destroy. """
# Try saving some preferences.
# Setting dynamic_init as a string is not needed with EasySettings,
# but I am doing it for human-friendly editing reasons.
# Pickle strings are ugly, and EasySettings.get_bool() will parse it.
settings.set(
'dynamic_init',
str(self.chkDynamic.get_active()).lower()
)
settings.set(
'lib_mode',
str(self.chkLibMode.get_active()).lower()
)
settings.set('theme_id', self.theme.get_id())
settings.save()
Gtk.main_quit()
# Helper functions -----------------------------------------------------
def build_theme_list(self):
""" Build the content for self.listTheme based on self.themes.
Sorts the names first.
"""
selected = -1
themeids = sorted(
self.themes,
key=lambda k: self.themes[k].get_name()
)
themenames = sorted((self.themes[k].get_name() for k in themeids))
selthemename = self.theme.get_name()
for i, themename in enumerate(themenames):
newrow = self.listTheme.append((themename, ))
self.listTheme.set_value(newrow, 0, themename)
if themename == selthemename:
selected = i
# Set the currently selected theme.
if selected > -1:
self.comboTheme.set_active(selected)
def generate_code(self):
""" Does the actual glade parsing/code generation. """
filepath = self.btnFileOpen.get_filename()
if not filepath:
self.msgs.warn('Please select an input file.')
return None
elif not os.path.exists(filepath):
self.glade = None
self.bufferOutput.set_text('')
self.msgs.warn('Glade file does not exist: {}'.format(filepath))
return None
dynamic = self.chkDynamic.get_active()
try:
gladefile = GladeFile(filepath=filepath, dynamic_init=dynamic)
except Exception as ex:
errfmt = 'Error parsing glade file:\n {}\n\n{}'
self.msgs.error(errfmt.format(filepath, ex))
self.glade = None
return None
lib_mode = self.chkLibMode.get_active()
content = gladefile.get_content(lib_mode=lib_mode)
self.bufferOutput.set_text(content)
self.glade = gladefile
warnings = gladefile.warning_msgs()
if warnings and (self.warned_files.get(filepath, None) != warnings):
self.warned_files[filepath] = warnings
self.msgs.warn(warnings)
def get_theme_by_name(self, name):
""" Retrieves a StyleScheme from self.themes by it's proper name.
Like: Kate, or Oblivion.
Returns None if the theme can't be found.
"""
for themeid, stylescheme in self.themes.items():
themename = stylescheme.get_name()
if name == themename:
return stylescheme
return None
def set_theme(self, scheme_identifier):
""" Sets the current highlight theme by id, name, or StyleScheme.
or by prefetched StyleScheme.
Return True if the theme was set, otherwise False.
"""
if isinstance(scheme_identifier, str):
# Id or name?
theme = self.themes.get(scheme_identifier, None)
if theme is None:
# Name.
theme = self.get_theme_by_name(scheme_identifier)
elif isinstance(scheme_identifier, GtkSource.StyleScheme):
# StyleScheme (prefetched)
theme = scheme_identifier
else:
# Unknown type for set_theme().
errfmt = 'Expected name, id, or StyleScheme. Got: {}'
raise ValueError(errfmt.format(type(scheme_identifier)))
if theme is not None:
self.theme = theme
self.bufferOutput.set_style_scheme(theme)
return True
return False
def set_theme_config(self):
""" Try loading a theme from config.
Return True if a theme was set, otherwise False.
"""
themeid = settings.get('theme_id', None)
if themeid:
return self.set_theme(themeid)
def write_file(self):
""" Write the generated code to a file. """
# Get generated code content.
content = self.bufferOutput.get_text(
self.bufferOutput.get_start_iter(),
self.bufferOutput.get_end_iter(),
True)
if not content:
self.msgs.error('There is nothing to save.')
return None
outputfile = self.dlgSave.show()
if not outputfile:
return None
try:
with open(outputfile, 'w') as f:
f.write(content)
except EnvironmentError as ex:
errfmt = 'There was an error writing to:\n {}\n\n{}'
self.msgs.error(errfmt.format(outputfile, ex))
return None
msglines = ['File was saved: {}'.format(outputfile)]
try:
self.glade.make_executable(outputfile)
msglines.append('Mode +rwx (774) was set to make it executable.')
except (PermissionError, EnvironmentError) as experm:
errfmt = 'Unable to make it executable:\n {}'
msglines.append(errfmt.format(experm))
self.msgs.info('\n'.join(msglines))
return None
class FileDialogSave(object):
""" A quick and easy file save dialog. """
def __init__(self, parent=None, title=None, filters=None, filepath=None):
self.parent = parent
self.title = title or 'Select a file name'
self.filters = filters or (('All Files', '*'),)
# Initial filepath to show.
self.filepath = filepath
def _build_filters(self, filters=None):
""" build dlgwindow filters from list/tuple of filters
filters = (('Description', '*.type'), ('Desc2', '*.typ2'))
"""
filters = filters or self.filters
# use self.filter to build from instead of custom filters
if filters is None:
filters = (('All Files', '*'),)
filefilters = []
for desc, pat in filters:
dfilter = Gtk.FileFilter()
dfilter.set_name(desc)
dfilter.add_pattern(pat)
filefilters.append(dfilter)
return filefilters
def show(self):
# Create Dialog.
dlg = Gtk.FileChooserDialog(
self.title,
self.parent,
Gtk.FileChooserAction.SAVE,
(
'_Cancel', Gtk.ResponseType.CANCEL,
'_Save', Gtk.ResponseType.OK
)
)
dlg.set_default_response(Gtk.ResponseType.OK)
if self.filepath:
dlg.set_filename(self.filepath)
# build filters
for dlgfilter in self._build_filters():
dlg.add_filter(dlgfilter)
# Show hidden files
dlg.set_show_hidden(True)
# Show Dialog, get response
response = dlg.run()
respfile = dlg.get_filename()
dlg.destroy()
return respfile if response == Gtk.ResponseType.OK else None
class MessageBoxes(object):
""" A helper for simple msg dialogs. """
def __init__(self, parent=None, title=None):
self.parent = parent
self.title = title or ''
def info(self, message):
dialog = Gtk.MessageDialog(
self.parent,
0,
Gtk.MessageType.INFO,
Gtk.ButtonsType.OK,
self.title)
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
def error(self, message, cancel=False):
btn = Gtk.ButtonsType.CANCEL if cancel else Gtk.ButtonsType.OK
dialog = Gtk.MessageDialog(
self.parent,
0,
Gtk.MessageType.ERROR,
btn,
self.title)
dialog.format_secondary_text(message)
dialog.run()
dialog.destroy()
def warn(self, message, okcancel=False):
btns = Gtk.ButtonsType.OK_CANCEL if okcancel else Gtk.ButtonsType.OK
dialog = Gtk.MessageDialog(
self.parent,
0,
Gtk.MessageType.WARNING,
btns,
self.title)
dialog.format_secondary_text(message)
response = dialog.run()
dialog.destroy()
return True if response == Gtk.ResponseType.OK else False
def question(self, message):
dialog = Gtk.MessageDialog(
self.parent,
0,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.YES_NO,
self.title)
dialog.format_secondary_text(message)
response = dialog.run()
dialog.destroy()
return True if response == Gtk.ResponseType.YES else False
def inspect_object(o):
""" Prints a repr() and dir() for an object for debugging. """
print('{!r}:'.format(o))
fmt = ' {}'.format
attrs = (fmt(a) for a in dir(o) if not a.startswith('_'))
print('\n'.join(attrs))
def gui_main(
filepath=None, outputfile=None, dynamic_init=False, lib_mode=False):
""" Main entry point for the program. """
app = App( # noqa
filepath=filepath,
outputfile=outputfile,
dynamic_init=dynamic_init,
lib_mode=lib_mode,
)
ret = Gtk.main()
sys.exit(ret)
if __name__ == '__main__':
# Won't hurt to run it anyway, but it should really run from glade.py.
mainret = gui_main()
sys.exit(mainret)