Esempio n. 1
0
def load_sizers():
    """Load and initialise the sizer support modules into ordered dict instance.
    See edit_sizers.edit_sizers.init_all."""
    if config.use_gui:
        logging.info('Load sizer generators:')
    for lang in code_writers.keys():
        module_name = 'edit_sizers.%s_sizers_codegen' % code_writers[
            lang].lang_prefix
        try:
            sizer_module = plugins.import_module(config.wxglade_path,
                                                 module_name)
            if not sizer_module:
                # error already logged
                pass
            elif hasattr(sizer_module, 'initialize'):
                sizer_module.initialize()
            else:
                logging.warning(
                    _('Missing function "initialize()" in imported module %s. Skip initialisation.'
                      ), module_name)

            if config.use_gui:
                logging.info(_('  for %s'), lang)

        except (AttributeError, ImportError, NameError, SyntaxError,
                ValueError):
            logging.exception(_('ERROR loading module "%s"'), module_name)
        except:
            logging.exception(
                _('Unexpected error during import of widget module %s'),
                module_name)

    # initialise sizer GUI elements
    import edit_sizers
    return edit_sizers.init_gui()
Esempio n. 2
0
    def preview(self, widget, position=None):
        """Generate and instantiate preview widget.
        None will be returned in case of errors. The error details are written to the application log file."""

        preview_filename = self._get_preview_filename()
        if preview_filename is None: return

        if wx.Platform == "__WXMAC__" and not compat.PYTHON2:
            # workaround for Mac OS testing: sometimes the caches need to be invalidated
            import importlib
            importlib.invalidate_caches()

        # make a valid name for the class (this can be invalid for some sensible reasons...)
        preview_classname = widget.WX_CLASS.split('.')[-1].split(':')[-1]
        # randomize the class name to make preview work when there are multiple classes with the same name (e.g. XRC)
        preview_classname = '_%d_%s' % (random.randrange(10**8, 10**
                                                         9), preview_classname)
        widget.properties["class"].set_temp(preview_classname)

        frame = None
        try:
            self.generate_code(True, preview_filename, widget)
            # import generated preview module dynamically
            preview_path = os.path.dirname(preview_filename)
            preview_module_name = os.path.basename(preview_filename)
            preview_module_name = os.path.splitext(preview_module_name)[0]
            preview_module = plugins.import_module(preview_path,
                                                   preview_module_name)
            if not preview_module:
                misc.error_message(
                    _('Can not import the preview module from file \n"%s".\n'
                      'The details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please report it.')
                    % self.output_path)
                return None

            try:
                preview_class = getattr(preview_module,
                                        preview_classname)  # .klass)
            except AttributeError:
                # module loaded previously -> do a re-load XXX this is required for Python 3; check alternatives
                if sys.version_info.major < 3:
                    preview_module = reload(preview_module)
                else:
                    import importlib
                    preview_module = importlib.reload(preview_module)
                preview_class = getattr(preview_module, preview_classname)

            if not preview_class:
                misc.error_message(
                    _('No preview class "%s" found.\nThe details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please report it.')
                    % widget.klass)
                return None

            if issubclass(preview_class, wx.MDIChildFrame):
                frame = wx.MDIParentFrame(None, -1, '')
                frame.SetMenuBar(wx.MenuBar())  # avoid assertion error
                child = preview_class(frame, -1, '')
                child.SetTitle('<Preview> - ' + child.GetTitle())
                w, h = child.GetSize()
                frame.SetClientSize((w + 20, h + 20))
            elif not issubclass(preview_class, (wx.Frame, wx.Dialog)):
                # the toplevel class isn't really toplevel, add a frame...
                frame = wx.Frame(None, -1, widget.klass)
                if issubclass(preview_class, wx.MenuBar):
                    menubar = preview_class()
                    frame.SetMenuBar(menubar)
                    panel = wx.Panel(frame)
                elif issubclass(preview_class, wx.ToolBar):
                    toolbar = preview_class(frame, -1)
                    frame.SetToolBar(toolbar)
                    panel = wx.Panel(frame)
                    frame.SetMinSize(toolbar.GetBestSize())
                    frame.Fit()
                else:
                    panel = preview_class(frame, -1)
                    frame.Fit()
            else:
                frame = preview_class(None, -1, '')

            def on_close(event):
                frame.Unbind(wx.EVT_CHAR_HOOK)
                compat.DestroyLater(frame)
                widget.preview_widget = None
                widget.properties["preview"].set_label(_('Show Preview'))

            frame.Bind(wx.EVT_CLOSE, on_close)
            frame.SetTitle(_('<Preview> - %s') % frame.GetTitle())
            # raise the frame
            if position:
                frame.SetPosition(position)
            else:
                frame.CenterOnScreen()
                if widget.widget:
                    # avoid Design and Preview window at the same position
                    pos = widget.widget.GetPosition()
                    if frame.GetPosition() == pos:
                        frame.SetPosition((pos[0] + 200, pos[1] + 100))
            frame.Show()
            # install handler for key down events
            frame.Bind(wx.EVT_CHAR_HOOK, self.on_char_hook)

            # remove the temporary file
            if not config.debugging:
                name = os.path.join(preview_path, preview_module_name + ".py")
                if os.path.isfile(name): os.unlink(name)
        except Exception as inst:
            if config.debugging or config.testing: raise
            widget.preview_widget = None
            widget.properties["preview"].set_label(_('Show Preview'))
            bugdialog.Show(_("Generate Preview"), inst)

        return frame
Esempio n. 3
0
    def preview(self, widget, out_name=None):
        """\
        Generate and instantiate preview widget.

        None will be returned in case of errors. The error details are
        written to the application log file.
        """
        if out_name is None:
            import warnings
            warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
                                    "application")
            out_name = os.tempnam(None, 'wxg') + '.py'
        widget_class_name = widget.klass

        # make a valid name for the class (this can be invalid for
        # some sensible reasons...)
        widget.klass = widget.klass[widget.klass.rfind('.') + 1:]
        widget.klass = widget.klass[widget.klass.rfind(':') + 1:]

        # ALB 2003-11-08: always randomize the class name: this is to make
        # preview work even when there are multiple classes with the same name
        # (which makes sense for XRC output...)
        widget.klass = '_%d_%s' % \
                       (random.randrange(10 ** 8, 10 ** 9), widget.klass)

        self.real_output_path = self.output_path
        self.output_path = out_name
        real_multiple_files = self.multiple_files
        real_language = self.language
        real_use_gettext = self.use_gettext
        self.use_gettext = False
        self.language = 'python'
        self.multiple_files = 0
        overwrite = self.overwrite
        self.overwrite = 0

        frame = None
        try:
            self.generate_code(preview=True)
            # import generated preview module dynamically
            preview_path = os.path.dirname(self.output_path)
            preview_module_name = os.path.basename(self.output_path)
            preview_module_name = os.path.splitext(preview_module_name)[0]
            preview_module = plugins.import_module(preview_path,
                                                   preview_module_name)
            if not preview_module:
                wx.MessageBox(
                    _('Can not import the preview module from file \n'
                      '"%s".\n'
                      'The details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please '
                      'report it.') % self.output_path, _('Error'),
                    wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)
                return None

            preview_class = getattr(preview_module, widget.klass)

            if not preview_class:
                wx.MessageBox(
                    _('No preview class "%s" found.\n'
                      'The details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please '
                      'report it.') % widget.klass, _('Error'),
                    wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)
                return None

            if issubclass(preview_class, wx.MDIChildFrame):
                frame = wx.MDIParentFrame(None, -1, '')
                child = preview_class(frame, -1, '')
                child.SetTitle('<Preview> - ' + child.GetTitle())
                w, h = child.GetSize()
                frame.SetClientSize((w + 20, h + 20))
            elif not (issubclass(preview_class, wx.Frame)
                      or issubclass(preview_class, wx.Dialog)):
                # the toplevel class isn't really toplevel, add a frame...
                frame = wx.Frame(None, -1, widget_class_name)
                if issubclass(preview_class, wx.MenuBar):
                    menubar = preview_class()
                    frame.SetMenuBar(menubar)
                elif issubclass(preview_class, wx.ToolBar):
                    toolbar = preview_class(frame, -1)
                    frame.SetToolBar(toolbar)
                else:
                    panel = preview_class(frame, -1)
                frame.Fit()
            else:
                frame = preview_class(None, -1, '')

            def on_close(event):
                frame.Destroy()
                widget.preview_widget = None
                widget.preview_button.SetLabel(_('Preview'))

            wx.EVT_CLOSE(frame, on_close)
            frame.SetTitle(_('<Preview> - %s') % frame.GetTitle())
            # raise the frame
            frame.CenterOnScreen()
            frame.Show()
            # remove the temporary file (and the .pyc/.pyo ones too)
            for ext in '', 'c', 'o', '~':
                name = self.output_path + ext
                if os.path.isfile(name):
                    os.unlink(name)
        except Exception, inst:
            widget.preview_widget = None
            widget.preview_button.SetLabel(_('Preview'))
            bugdialog.Show(_("Generate Preview"), inst)
Esempio n. 4
0
    def preview(self, widget, out_name=None):
        """Generate and instantiate preview widget.
        None will be returned in case of errors. The error details are written to the application log file."""
        if out_name is None:
            import warnings
            warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
                                    "application")
            try:
                out_name = os.tempnam(None, 'wxg') + '.py'
            except AttributeError:  # XXX use a different name; e.g. the project file name with "_temp"
                out_name = "C:\\Users\\Dietmar\\wxg_tmp.py"
        widget_class_name = widget.klass

        # make a valid name for the class (this can be invalid for some sensible reasons...)
        widget_class = widget.klass[widget.klass.rfind('.') + 1:]
        widget_class = widget.klass[widget.klass.rfind(':') + 1:]
        # ALB 2003-11-08: always randomize the class name: this is to make preview work even when there are multiple
        # classes with the same name (which makes sense for XRC output...)
        widget_class = '_%d_%s' % (random.randrange(10**8, 10**
                                                    9), widget_class)
        widget.properties["class"].set(widget_class)

        self.real_output_path = self.output_path
        self.properties["output_path"].set(out_name)
        real_multiple_files = self.multiple_files
        real_language = self.language
        real_use_gettext = self.use_gettext
        self.properties["use_gettext"].set(False)
        self.properties["language"].set('python')
        self.properties["multiple_files"].set(False)
        overwrite = self.overwrite
        self.properties["overwrite"].set(False)

        frame = None
        try:
            self.generate_code(preview=True)
            # import generated preview module dynamically
            preview_path = os.path.dirname(self.output_path)
            preview_module_name = os.path.basename(self.output_path)
            preview_module_name = os.path.splitext(preview_module_name)[0]
            preview_module = plugins.import_module(preview_path,
                                                   preview_module_name)
            if not preview_module:
                wx.MessageBox(
                    _('Can not import the preview module from file \n"%s".\n'
                      'The details are written to the log file.\nIf you think this is a wxGlade bug, please '
                      'report it.') % self.output_path, _('Error'),
                    wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)
                return None

            try:
                preview_class = getattr(preview_module, widget.klass)
            except AttributeError:
                # module loade previously -> do a re-load XXX this is required for Python 3; check alternatives
                import importlib
                preview_module = importlib.reload(preview_module)
                preview_class = getattr(preview_module, widget.klass)

            if not preview_class:
                wx.MessageBox(
                    _('No preview class "%s" found.\nThe details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please report it.')
                    % widget.klass, _('Error'),
                    wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)
                return None

            if issubclass(preview_class, wx.MDIChildFrame):
                frame = wx.MDIParentFrame(None, -1, '')
                child = preview_class(frame, -1, '')
                child.SetTitle('<Preview> - ' + child.GetTitle())
                w, h = child.GetSize()
                frame.SetClientSize((w + 20, h + 20))
            elif not issubclass(preview_class, (wx.Frame, wx.Dialog)):
                # the toplevel class isn't really toplevel, add a frame...
                frame = wx.Frame(None, -1, widget_class_name)
                if issubclass(preview_class, wx.MenuBar):
                    menubar = preview_class()
                    frame.SetMenuBar(menubar)
                elif issubclass(preview_class, wx.ToolBar):
                    toolbar = preview_class(frame, -1)
                    frame.SetToolBar(toolbar)
                else:
                    panel = preview_class(frame, -1)
                frame.Fit()
            else:
                frame = preview_class(None, -1, '')

            def on_close(event):
                frame.Destroy()
                widget.preview_widget = None
                widget.properties["preview"].set_label(_('Preview'))

            wx.EVT_CLOSE(frame, on_close)
            frame.SetTitle(_('<Preview> - %s') % frame.GetTitle())
            # raise the frame
            frame.CenterOnScreen()
            frame.Show()
            # remove the temporary file (and the .pyc/.pyo ones too)
            for ext in ('', 'c', 'o', '~'):
                name = self.output_path + ext
                if os.path.isfile(name):
                    os.unlink(name)
        except Exception as inst:
            widget.preview_widget = None
            widget.properties["preview"].set_label(_('Preview'))
            bugdialog.Show(_("Generate Preview"), inst)

        # restore app state
        widget.properties["class"].set(widget_class_name)
        self.properties["output_path"].set(self.real_output_path)
        del self.real_output_path
        self.properties["multiple_files"].set(real_multiple_files)
        self.properties["language"].set(real_language)
        self.properties["use_gettext"].set(real_use_gettext)
        self.properties["overwrite"].set(overwrite)
        return frame
    def preview(self, widget, position=None):
        """Generate and instantiate preview widget.
        None will be returned in case of errors. The error details are written to the application log file."""
        # some checks
        #if compat.IS_PHOENIX:
        #found = common.app_tree.find_widgets_by_classnames(widget.node, "EditPropertyGridManager")
        #if found:
        #error = ("Preview with PropertyGridManager controls is currently deactivated as it causes crashes "
        #"with wxPython Phoenix")
        #wx.MessageBox( error, _('Error'), wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION )
        #return
        # XXX check other things as well, e.g. different bitmap sizes for BitmapButton

        preview_filename = self._get_preview_filename()
        if preview_filename is None: return
        widget_class_name = widget.klass

        # make a valid name for the class (this can be invalid for some sensible reasons...)
        widget_class = widget.klass[widget.klass.rfind('.') + 1:]
        widget_class = widget.klass[widget.klass.rfind(':') + 1:]
        # ALB 2003-11-08: always randomize the class name: this is to make preview work even when there are multiple
        # classes with the same name (which makes sense for XRC output...)
        widget_class = '_%d_%s' % (random.randrange(10**8, 10**
                                                    9), widget_class)
        widget.properties["class"].set(widget_class)

        if wx.Platform == "__WXMAC__" and not compat.PYTHON2:
            # workaround for Mac OS testing: sometimes the caches need to be invalidated
            import importlib
            importlib.invalidate_caches()

        frame = None
        try:
            self.generate_code(True, preview_filename, widget)
            # import generated preview module dynamically
            preview_path = os.path.dirname(preview_filename)
            preview_module_name = os.path.basename(preview_filename)
            preview_module_name = os.path.splitext(preview_module_name)[0]
            preview_module = plugins.import_module(preview_path,
                                                   preview_module_name)
            if not preview_module:
                misc.error_message(
                    _('Can not import the preview module from file \n"%s".\n'
                      'The details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please report it.')
                    % self.output_path)
                return None

            try:
                preview_class = getattr(preview_module, widget.klass)
            except AttributeError:
                # module loade previously -> do a re-load XXX this is required for Python 3; check alternatives
                import importlib
                preview_module = importlib.reload(preview_module)
                preview_class = getattr(preview_module, widget.klass)

            if not preview_class:
                misc.error_message(
                    _('No preview class "%s" found.\nThe details are written to the log file.\n'
                      'If you think this is a wxGlade bug, please report it.')
                    % widget.klass)
                return None

            if issubclass(preview_class, wx.MDIChildFrame):
                frame = wx.MDIParentFrame(None, -1, '')
                frame.SetMenuBar(wx.MenuBar())  # avoid assertion error
                child = preview_class(frame, -1, '')
                child.SetTitle('<Preview> - ' + child.GetTitle())
                w, h = child.GetSize()
                frame.SetClientSize((w + 20, h + 20))
            elif not issubclass(preview_class, (wx.Frame, wx.Dialog)):
                # the toplevel class isn't really toplevel, add a frame...
                frame = wx.Frame(None, -1, widget_class_name)
                if issubclass(preview_class, wx.MenuBar):
                    menubar = preview_class()
                    frame.SetMenuBar(menubar)
                    panel = wx.Panel(frame)
                elif issubclass(preview_class, wx.ToolBar):
                    toolbar = preview_class(frame, -1)
                    frame.SetToolBar(toolbar)
                    panel = wx.Panel(frame)
                    frame.SetMinSize(toolbar.GetBestSize())
                    frame.Fit()
                else:
                    panel = preview_class(frame, -1)
                    frame.Fit()
            else:
                frame = preview_class(None, -1, '')

            def on_close(event):
                frame.Unbind(wx.EVT_CHAR_HOOK)
                compat.DestroyLater(frame)
                widget.preview_widget = None
                widget.properties["preview"].set_label(_('Show Preview'))

            frame.Bind(wx.EVT_CLOSE, on_close)
            frame.SetTitle(_('<Preview> - %s') % frame.GetTitle())
            # raise the frame
            if position:
                frame.SetPosition(position)
            else:
                frame.CenterOnScreen()
                if widget.widget:
                    # avoid Design and Preview window at the same position
                    pos = widget.widget.GetPosition()
                    if frame.GetPosition() == pos:
                        frame.SetPosition((pos[0] + 200, pos[1] + 100))
            frame.Show()
            # install handler for key down events
            frame.Bind(wx.EVT_CHAR_HOOK, self.on_char_hook)

            # remove the temporary file
            if not config.debugging:
                name = os.path.join(preview_path, preview_module_name + ".py")
                if os.path.isfile(name): os.unlink(name)
        except Exception as inst:
            if config.debugging or config.testing: raise
            widget.preview_widget = None
            widget.properties["preview"].set_label(_('Show Preview'))
            bugdialog.Show(_("Generate Preview"), inst)
        # XXX restore app state
        widget.properties["class"].set(widget_class_name)
        return frame