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
|
"""
Entry point of wxGlade
@copyright: 2002-2007 Alberto Griggio
@copyright: 2013-2016 Carsten Grohmann
@copyright: 2016-2024 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
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-2024 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"))
parser.add_option("-c", "--use-config", dest="rc_file",
help=_("use specified wxgladerc config file instead of the default one") )
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 filename.startswith("file://"): filename = filename[7:]
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 ValueError('Code writer for "%s" is not available.'%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 _guiless_open_app(filename):
"""Load a new wxGlade project
GUI-less version of main.wxGladeFrame._open_app().
Returns True if successful."""
import time
from xml.sax import SAXParseException
from xml_parse import XmlWidgetBuilder, XmlParsingError
error_msg = None
infile = None
start = time.time()
common.root.clear()
common.root.init()
try:
try:
logging.info( _('Read wxGlade project from file "%s"'), filename )
input_file_version = None
if not isinstance(filename, list):
common.root.filename = filename
# decoding will done automatically by SAX XML library
if compat.PYTHON2:
infile = open(filename)
else:
infile = open(filename, "r", encoding="UTF8")
if hasattr(infile, "seek"):
# try to read file version number from the first few lines
import re
version_re = re.compile(r"<!-- generated by wxGlade (\d+)\.(\d+)\.(\d+)(\S*)\s*")
for n in range(3):
match = version_re.match( infile.readline() )
if match:
major, minor, sub, extension = match.groups()
input_file_version = (int(major), int(minor), int(major), extension)
break
infile.seek(0)
else:
common.root.filename = None
p = XmlWidgetBuilder(filename, input_file_version)
if infile is not None:
p.parse(infile)
else:
p.parse_string(filename)
filename = None
except (EnvironmentError, SAXParseException, XmlParsingError) as msg:
if config.debugging: raise
if infile is not None:
error_msg = _("Error loading file %s:\n%s") % (filename, msg)
else:
error_msg = _("Error loading from a file-like object:\n%s") % msg
except Exception as inst:
if config.debugging: raise
if filename:
fn = os.path.basename(filename).encode('ascii','replace')
msg = _('loading file "%s"') % fn
else:
msg = _('loading from a file-like object')
error_msg = ("%s: %s" % (msg, inst))
finally:
if infile and filename:
infile.close()
if error_msg:
common.root.clear()
common.root.new()
common.root.saved = True
logging.error(error_msg)
return False
if common.root.is_template:
logging.info(_("Template loaded"))
common.root.template_data = template.Template(filename)
common.root.filename = None
end = time.time()
logging.debug(_('Loading time: %.5f'), end - start)
common.root.saved = True
#common.property_panel.Raise()
duration = end - start
if filename:
logging.debug( _("Loaded %s in %.2f seconds") % (os.path.basename(filename), duration ))
else:
logging.debug( _("Loaded in %.2f seconds") % duration )
return True
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"""
import application, tree
# Instead of instantiating a main.wxGlade() object, that is
# derived from wx.App, we must do the equivalent work. The
# following lines are taken from main.wxGlade().OnInit() and
# main.wxGladeFrame.__init__()
common.init_preferences()
common.root = app = application.Application()
# Now we can load the file
if filename is not None:
b = _guiless_open_app(filename)
if not b:
sys.exit(1)
try:
if language not in common.code_writers:
raise ValueError('Code writer for "%s" is not available.'%language)
common.root.properties["language"].set(language)
common.root.generate_code(out_path=out_path)
#except errors.WxgBaseException as inst:
#if config.debugging: raise
#logging.error(inst)
#sys.exit(inst)
except Exception:
if config.debugging: raise
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)
if not config.testing:
sys.exit(0)
def init_stage1(options):
"""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(options)
# initialise own logging extensions
log.init(filename=config.log_file, encoding='utf-8',
level='DEBUG' if config.debugging else '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.debug(_('Base directory: %s'), config.wxglade_path)
logging.debug(_('Documentation directory: %s'), config.docs_path)
logging.debug(_('Icons directory: %s'), config.icons_path)
logging.debug(_('Build-in widgets directory: %s'), config.widgets_path)
logging.debug(_('Template directory: %s'), config.templates_path)
logging.debug(_('Credits file: %s'), config.credits_file)
logging.debug(_('License file: %s'), config.license_file)
logging.debug(_('Manual file: %s'), config.manual_file)
logging.debug(_('Tutorial file: %s'), config.tutorial_file)
logging.debug(_('Home directory: %s'), config.home_path)
logging.debug(_('Application data directory: %s'), config.appdata_path)
logging.debug(_('Configuration file: %s'), config.rc_file)
logging.debug(_('History file: %s'), config.history_file)
logging.debug(_('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")
if not os.path.exists(config.rc_file):
config.inform_screen_reader = msw.check_for_screen_reader()
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()
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(options)
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()
|