예제 #1
0
파일: main.py 프로젝트: italomaia/spe
    def _open_app(self,
                  infilename,
                  use_progress_dialog=True,
                  is_filelike=False,
                  add_to_history=True):
        import time
        from xml_parse import XmlWidgetBuilder, ProgressXmlWidgetBuilder, \
             XmlParsingError
        from xml.sax import SAXParseException

        start = time.clock()

        common.app_tree.clear()
        if not is_filelike:
            common.app_tree.app.filename = infilename
        else:
            common.app_tree.filename = getattr(infilename, 'name', None)
        common.property_panel.Reparent(self.hidden_frame)
        # prevent the auto-expansion of nodes
        common.app_tree.auto_expand = False

        old_dir = os.getcwd()
        try:
            if not is_filelike:
                os.chdir(os.path.dirname(infilename))
                infile = open(infilename)
            else:
                infile = infilename
                infilename = getattr(infile, 'name', None)
            if use_progress_dialog and config.preferences.show_progress:
                p = ProgressXmlWidgetBuilder(input_file=infile)
            else:
                p = XmlWidgetBuilder()
            p.parse(infile)
        except (IOError, OSError, SAXParseException, XmlParsingError), msg:
            if locals().has_key('infile') and not is_filelike: infile.close()
            common.app_tree.clear()
            common.property_panel.Reparent(self.frame2)
            common.app_tree.app.saved = True
            wx.MessageBox(_("Error loading file %s: %s") % \
                          (misc.wxstr(infilename), misc.wxstr(msg)),
                          _("Error"), wx.OK|wx.CENTRE|wx.ICON_ERROR)
            # reset the auto-expansion of nodes
            common.app_tree.auto_expand = True
            os.chdir(old_dir)
            return False
예제 #2
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, ProgressXmlWidgetBuilder, XmlParsingError
    error_msg = None
    infile = None

    start = time.clock()

    common.app_tree.clear()
    common.app_tree.app.init()

    try:
        try:
            logging.info( _('Read wxGlade project from file "%s"'), filename )
            input_file_version = None

            if not isinstance(filename, list):
                common.app_tree.app.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("<!-- 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.app_tree.app.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.app_tree.clear()
            common.app_tree.app.new()
            common.app_tree.app.saved = True

            logging.error(error_msg)

            return False

    if common.app_tree.app.is_template:
        logging.info(_("Template loaded"))
        common.app_tree.app.template_data = template.Template(filename)
        common.app_tree.app.filename = None

    end = time.clock()
    logging.info(_('Loading time: %.5f'), end - start)

    common.app_tree.app.saved = True
    #common.property_panel.Raise()

    duration = end - start
    if filename:
        logging.info( _("Loaded %s in %.2f seconds") % (os.path.basename(filename), duration ))
    else:
        logging.info( _("Loaded in %.2f seconds") % duration )

    return True
예제 #3
0
    def _open_app(self,
                  filename,
                  use_progress_dialog=True,
                  add_to_history=True):
        "Load a new wxGlade project"

        error_msg = None
        infile = None

        start = time.clock()

        common.app_tree.clear()
        common.app_tree.app.init()
        common.app_tree.auto_expand = False  # disable auto-expansion of nodes

        try:
            try:
                self._logger.info(_('Read wxGlade project from file "%s"'),
                                  filename)

                if not isinstance(filename, list):
                    common.app_tree.app.filename = filename
                    # decoding will done automatically by SAX XML library
                    if compat.PYTHON2:
                        infile = open(filename)
                    else:
                        infile = open(filename, "r", encoding="UTF8")
                else:
                    common.app_tree.app.filename = None

                if use_progress_dialog and config.preferences.show_progress:
                    p = ProgressXmlWidgetBuilder(input_file=infile)
                else:
                    p = XmlWidgetBuilder()

                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: %s") % (
                        misc.wxstr(filename), misc.wxstr(msg))
                else:
                    error_msg = _("Error loading from a file-like object: %s"
                                  ) % misc.wxstr(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')
                bugdialog.Show(msg, inst)
        finally:
            if infile and filename:
                infile.close()

            if error_msg:
                common.app_tree.clear()
                common.app_tree.app.new()
                common.app_tree.app.saved = True
                common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

                wx.MessageBox(error_msg, _('Error'),
                              wx.OK | wx.CENTRE | wx.ICON_ERROR)

                return False

        misc.set_focused_widget(common.app_tree.root.widget, force=True)

        common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

        common.app_tree.expand()
        if common.app_tree.app.is_template:
            self._logger.info(_("Template loaded"))
            common.app_tree.app.template_data = template.Template(filename)
            common.app_tree.app.filename = None

        end = time.clock()
        self._logger.info(_('Loading time: %.5f'), end - start)

        common.app_tree.app.saved = True
        common.property_panel.Raise()

        if hasattr(self, 'file_history') and filename is not None and add_to_history and \
           (not common.app_tree.app.is_template):
            self.file_history.AddFileToHistory(misc.wxstr(filename))

        if config.preferences.autosave and self.autosave_timer is not None:
            self.autosave_timer.Start()

        duration = end - start
        if filename:
            self.user_message(
                _("Loaded %s in %.2f seconds") %
                (misc.wxstr(os.path.basename(filename)), duration))
        else:
            self.user_message(_("Loaded in %.2f seconds") % duration)

        return True
예제 #4
0
    def _open_app(self,
                  filename_or_filelike,
                  use_progress_dialog=True,
                  add_to_history=True):
        """\
        Load a new wxGlade project

        @param filename_or_filelike: Source filename or file-like object
        @type filename_or_filelike: file | StringIO

        @param use_progress_dialog: Show progress bar during loading WXG file
        @type use_progress_dialog: bool

        @param add_to_history: Add file to open to file history
        @type add_to_history: bool

        @return: True on Success
        @rtype: bool
        """
        if isinstance(filename_or_filelike, compat.StringIO):
            assert isinstance(filename_or_filelike.getvalue(), unicode)
        else:
            assert isinstance(filename_or_filelike, compat.basestring)

        error_msg = None
        filename = None
        infile = None
        old_dir = os.getcwd()

        if isinstance(filename_or_filelike, compat.basestring):
            common.app_tree.app.filename = filename_or_filelike
            filename = filename_or_filelike
        else:
            common.app_tree.filename = None

        start = time.clock()
        common.app_tree.clear()

        # disable auto-expansion of nodes
        common.app_tree.auto_expand = False

        try:
            try:
                if isinstance(filename_or_filelike, compat.StringIO):
                    # convert filename_or_filelike to UTF-8 and write back as lines, because
                    # ProgressXmlWidgetBuilder uses lines to calculate and show the position
                    tmp = filename_or_filelike.getvalue()
                    tmp = tmp.encode('UTF-8')
                    infile = compat.StringIO()
                    for line in tmp.split('\n'):
                        infile.write('%s\n' % line)
                    infile.seek(0)
                    self._logger.info(
                        _('Read wxGlade project from file-like object'))

                else:
                    self._logger.info(_('Read wxGlade project from file "%s"'),
                                      filename)
                    os.chdir(os.path.dirname(filename))
                    # decoding will done automatically by SAX XML library
                    if compat.PYTHON2:
                        infile = open(filename)
                    else:
                        infile = open(filename, "r", encoding="UTF8")

                if use_progress_dialog and config.preferences.show_progress:
                    p = ProgressXmlWidgetBuilder(input_file=infile)
                else:
                    p = XmlWidgetBuilder()

                p.parse(infile)
            except (EnvironmentError, SAXParseException,
                    XmlParsingError) as msg:
                if 'WINGDB_ACTIVE' in os.environ: raise
                if filename:
                    error_msg = _("Error loading file %s: %s") % (
                        misc.wxstr(filename), misc.wxstr(msg))
                else:
                    error_msg = _("Error loading from a file-like object: %s"
                                  ) % misc.wxstr(msg)
            except Exception as inst:
                if 'WINGDB_ACTIVE' in os.environ: raise
                if filename:
                    fn = os.path.basename(filename).encode('ascii', 'replace')
                    msg = _('loading file "%s"') % fn
                else:
                    msg = _('loading from a file-like object')
                bugdialog.Show(msg, inst)
        finally:
            if infile and filename:
                infile.close()

            if error_msg:
                common.app_tree.clear()
                common.app_tree.app.saved = True
                common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

                os.chdir(old_dir)

                wx.MessageBox(error_msg, _('Error'),
                              wx.OK | wx.CENTRE | wx.ICON_ERROR)

                return False

        misc.set_focused_widget(common.app_tree.root.widget)

        common.app_tree.auto_expand = True  # re-enable auto-expansion of nodes

        common.app_tree.expand()
        if common.app_tree.app.is_template:
            self._logger.info(_("Template loaded"))
            common.app_tree.app.template_data = template.Template(filename)
            common.app_tree.app.filename = None

        end = time.clock()
        self._logger.info(_('Loading time: %.5f'), end - start)

        common.app_tree.app.saved = True
        common.property_panel.Raise()

        if hasattr(self, 'file_history') and filename is not None and add_to_history and \
           (not common.app_tree.app.is_template):
            self.file_history.AddFileToHistory(misc.wxstr(filename))

        if config.preferences.autosave and self.autosave_timer is not None:
            self.autosave_timer.Start()

        duration = end - start
        if filename:
            self.user_message(
                _("Loaded %s in %.2f seconds") %
                (misc.wxstr(os.path.basename(filename)), duration))
        else:
            self.user_message(_("Loaded in %.2f seconds") % duration)

        return True