forked from wxGlade/wxGlade
/
wxglade.py
executable file
·278 lines (227 loc) · 10.3 KB
/
wxglade.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
#!/usr/bin/env python2
"""
Entry point of wxGlade
@copyright: 2002-2007 Alberto Griggio
@copyright: 2013-2016 Carsten Grohmann
@copyright: 2016-2018 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import atexit
import codecs
import logging, os, sys, gettext, optparse
# Use a NullWriter with Unicode support (encoding attribute) to catch and
# drop all output in PyInstaller environment (standalone Edition)
#
# Fix for PyInstaller bug #1324
# https://github.com/pyinstaller/pyinstaller/issues/1324
if hasattr(sys, 'frozen') and not hasattr(sys.stderr, 'encoding'):
class NullWriter(object):
softspace = 0
encoding = 'UTF-8'
def write(*args):
pass
def flush(*args):
pass
sys.stdout = NullWriter()
sys.stderr = NullWriter()
t = gettext.translation(domain="wxglade", localedir="locale", fallback=True)
t.install("wxglade")
# avoid problems with "_" in __builtins__ being replaced:
def my_displayhook(value):
if value is not None:
print(repr(value))
sys.displayhook = my_displayhook
import common, config, compat, log, errors
def parse_command_line():
"Parse command line"
# list of all available languages; don't load code generators at this point!!
languages = ['C++', 'XRC', 'lisp', 'perl', 'python']
# inject
optparse.OptionParser.format_description = lambda self, formatter: self.description
version = _("wxGlade version %s\n"
"Copyright (C) 2007-2012 Alberto Griggio\n"
"Copyright (C) 2011-2016 Carsten Grohmann\n"
"Copyright (C) 2016-2017 Dietmar Schwertberger\n"
"License MIT: The MIT License\n"
" <http://www.opensource.org/licenses/mit-license.php>") % config.get_version()
usage = _("Usage: wxglade <WXG File> start the wxGlade GUI\n"
" or: wxglade <Options> <WXG File> generate code from command line\n"
" or: wxglade --version show programs version number and exit\n"
" or: wxglade -h|--help show this help message and exit")
parser = optparse.OptionParser( add_help_option=False, version=version, usage=usage )
parser.add_option('-h', '--help', dest='help', action='store_true', help=_('show this help message and exit'))
parser.add_option("-g", "--generate-code", type="choice", choices=languages, metavar="LANG", dest="language",
help=_("(required) output language, valid languages are: %s") % ", ".join(languages) )
parser.add_option("-o", "--output", metavar="PATH", dest="output",
help=_("(optional) output file in single-file mode or output directory in multi-file mode"))
options, args = parser.parse_args()
# print epilog because OptionParser.epilog isn't available to Python 2.3
if options.help:
parser.print_help()
print( _( "Example: Generate Python code out of myapp.wxg\n\n"
" wxglade -o output.py -g python myapp.wxg\n\n"
"Report bugs to: <wxglade-general@lists.sourceforge.net> or at\n"
" <https://sourceforge.net/projects/wxglade/>\n"
"wxGlade home page: <http://wxglade.sourceforge.net/>") )
sys.exit()
# Make an absolute version of path.
# According to the invoking dir of wxGlade (which can be different
# from '.' if it is invoked from a shell script).
if len(args) == 1:
filename = args[0]
if not os.path.isabs(filename):
filename = os.path.join(os.getcwd(), filename)
filename = os.path.normpath(os.path.expanduser(filename))
options.filename = filename
else:
options.filename = None
# check parameters
# - language
# - one file -> cmdline code generation
# - no / > one files -> usage
# - no language -> start gui
if options.language:
if len(args) == 1:
options.start_gui = False
elif len(args) == 0:
msg = _("No wxg file given.\n")
logging.error(msg)
parser.print_help()
sys.exit(msg)
else:
msg = _("Too many wxg files given.\n")
logging.error(msg)
parser.print_help()
sys.exit(msg)
else:
options.start_gui = True
# check output path
if options.output:
options.output = os.path.normpath(os.path.expanduser(options.output))
return options
def command_line_code_generation(filename, language, out_path=None):
"""Starts a code generator without starting the GUI.
filename: Name of wxg file to generate code from
language: Code generator language
out_path: output file / output directory"""
from xml_parse import CodeWriter
try:
if language not in common.code_writers:
raise errors.WxgMissingCodeWriter(language)
writer = common.code_writers[language]
CodeWriter( writer=writer, input=filename, out_path=out_path )
except errors.WxgBaseException as inst:
logging.error(inst)
sys.exit(inst)
except Exception:
logging.error( _("An exception occurred while generating the code for the application.\n"
"If you think this is a wxGlade bug, please report it.") )
logging.exception(_('Internal Error'))
sys.exit(1)
sys.exit(0)
def command_line_code_generation(filename, language, out_path=None):
"""Starts a code generator without starting the GUI.
filename: Name of wxg file to generate code from
language: Code generator language
out_path: output file / output directory"""
from main import wxGlade
app = wxGlade()
if filename is not None:
win = app.GetTopWindow()
win._open_app(filename, False)
win.cur_dir = os.path.dirname(filename)
try:
if language not in common.code_writers:
raise errors.WxgMissingCodeWriter(language)
common.app_tree.app.properties["language"].set(language)
common.app_tree.app.generate_code(out_path=out_path)
except errors.WxgBaseException as inst:
logging.error(inst)
sys.exit(inst)
except Exception:
logging.error( _("An exception occurred while generating the code for the application.\n"
"If you think this is a wxGlade bug, please report it.") )
logging.exception(_('Internal Error'))
sys.exit(1)
sys.exit(0)
def init_stage1():
"""Initialise paths for wxGlade (first stage)
Initialisation is split because the test suite doesn't work with proper initialised paths."""
config.version = config.get_version()
common.init_paths()
# initialise own logging extensions
log.init(filename=config.log_file, encoding='utf-8', level='INFO')
atexit.register(log.deinit)
# print versions
logging.info( _('Starting wxGlade version "%s" on Python %s'), config.version, config.py_version )
# print used paths
logging.info(_('Base directory: %s'), config.wxglade_path)
logging.info(_('Documentation directory: %s'), config.docs_path)
logging.info(_('Icons directory: %s'), config.icons_path)
logging.info(_('Build-in widgets directory: %s'), config.widgets_path)
logging.info(_('Template directory: %s'), config.templates_path)
logging.info(_('Credits file: %s'), config.credits_file)
logging.info(_('License file: %s'), config.license_file)
logging.info(_('Manual file: %s'), config.manual_file)
logging.info(_('Tutorial file: %s'), config.tutorial_file)
logging.info(_('Home directory: %s'), config.home_path)
logging.info(_('Application data directory: %s'), config.appdata_path)
logging.info(_('Configuration file: %s'), config.rc_file)
logging.info(_('History file: %s'), config.history_file)
logging.info(_('Log file: %s'), config.log_file)
# adapt application search path
sys.path.insert(0, config.wxglade_path)
sys.path.insert(1, config.widgets_path)
def init_stage2(use_gui):
"""Initialise the remaining (non-path) parts of wxGlade (second stage)
use_gui: Starting wxGlade GUI"""
config.use_gui = use_gui
if use_gui:
# import proper wx-module using wxversion, which is only available in Classic
if compat.IS_CLASSIC:
if not hasattr(sys, "frozen") and 'wx' not in sys.modules:
try:
import wxversion
wxversion.ensureMinimal('2.8')
except ImportError:
msg = _('Please install missing Python module "wxversion".')
logging.error(msg)
sys.exit(msg)
try:
import wx
except ImportError:
msg = _('Please install missing Python module "wxPython".')
logging.error(msg)
sys.exit(msg)
# store current version and platform ('not_set' is default)
config.platform = wx.Platform
config.wx_version = wx.__version__
if sys.platform=="win32":
# register ".wxg" extension
try:
import msw
msw.register_extensions(["wxg"], "wxGlade")
except ImportError:
pass
# codewrites, widgets and sizers are loaded in class main.wxGladeFrame
else:
# use_gui has to be set before importing config
common.init_preferences()
if config.preferences.log_debug_info:
log.setDebugLevel()
common.init_codegen()
def run_main():
"This main procedure is started by calling either wxglade.py or wxglade.pyw on windows."
# check command line parameters first
options = parse_command_line()
# initialise wxGlade (first stage and second stage)
init_stage1()
init_stage2(options.start_gui)
if options.start_gui:
# late import of main (imported wx) for using wxversion in init_stage2()
import main
main.main(options.filename)
else:
command_line_code_generation( filename=options.filename, language=options.language, out_path=options.output )
if __name__ == "__main__":
run_main()