Esempio n. 1
0
def importShortcuts(fn):
    """
    Module function to import the keyboard shortcuts for the defined E4Actions.
    
    @param fn filename of the import file (string)
    @return flag indicating success
    """
    fn = unicode(fn)
    try:
        if fn.lower().endswith("kz"):
            try:
                import gzip
            except ImportError:
                KQMessageBox.critical(None,
                    QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
                    QApplication.translate("Shortcuts", 
                        """Compressed keyboard shortcut files"""
                        """ not supported. The compression library is missing."""))
                return False
            f = gzip.open(fn, "rb")
        else:
            f = open(fn, "rb")
        try:
            line = f.readline()
            dtdLine = f.readline()
        finally:
            f.close()
    except IOError:
        KQMessageBox.critical(None,
            QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
            QApplication.translate("Shortcuts", 
                "<p>The keyboard shortcuts could not be read from file <b>%1</b>.</p>")
                .arg(fn))
        return False
    
    if fn.lower().endswith("kz"):
        # work around for a bug in xmlproc
        validating = False
    else:
        validating = dtdLine.startswith("<!DOCTYPE")
    parser = make_parser(validating)
    handler = ShortcutsHandler()
    er = XMLEntityResolver()
    eh = XMLErrorHandler()
    
    parser.setContentHandler(handler)
    parser.setEntityResolver(er)
    parser.setErrorHandler(eh)
    
    try:
        if fn.lower().endswith("kz"):
            try:
                import gzip
            except ImportError:
                KQMessageBox.critical(None,
                    QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
                    QApplication.translate("Shortcuts", 
                        """Compressed keyboard shortcut files"""
                        """ not supported. The compression library is missing."""))
                return False
            f = gzip.open(fn, "rb")
        else:
            f = open(fn, "rb")
        try:
            try:
                parser.parse(f)
            except UnicodeEncodeError:
                f.seek(0)
                buf = cStringIO.StringIO(f.read())
                parser.parse(buf)
        finally:
            f.close()
    except IOError:
        KQMessageBox.critical(None,
            QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
            QApplication.translate("Shortcuts", 
                "<p>The keyboard shortcuts could not be read from file <b>%1</b>.</p>")
                .arg(fn))
        return False
        
    except XMLFatalParseError:
        KQMessageBox.critical(None,
            QApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
            QApplication.translate("Shortcuts", 
                "<p>The keyboard shortcuts file <b>%1</b> has invalid contents.</p>")
                .arg(fn))
        eh.showParseMessages()
        return False
        
    eh.showParseMessages()
    
    shortcuts = handler.getShortcuts()
    
    if handler.getVersion() == "3.5":
        setActions_35(shortcuts)
    else:
        setActions(shortcuts)
    
    saveShortcuts()
    syncPreferences()
    
    return True
 def __populateList(self):
     """
     Private method to populate the list of available plugins.
     """
     self.repositoryList.clear()
     self.__stableItem = None
     self.__unstableItem = None
     self.__unknownItem = None
     
     self.downloadProgress.setValue(0)
     self.__doneMethod = None
     
     if os.path.exists(self.pluginRepositoryFile):
         self.__repositoryMissing = False
         try:
             f = open(self.pluginRepositoryFile, "rb")
             line = f.readline()
             dtdLine = f.readline()
             f.close()
         except IOError:
             KQMessageBox.critical(None,
                 self.trUtf8("Read plugins repository file"),
                 self.trUtf8("<p>The plugins repository file <b>%1</b> "
                             "could not be read. Select Update</p>")\
                     .arg(self.pluginRepositoryFile))
             return
         
         # now read the file
         if line.startswith('<?xml'):
             parser = make_parser(dtdLine.startswith("<!DOCTYPE"))
             handler = PluginRepositoryHandler(self)
             er = XMLEntityResolver()
             eh = XMLErrorHandler()
             
             parser.setContentHandler(handler)
             parser.setEntityResolver(er)
             parser.setErrorHandler(eh)
             
             try:
                 f = open(self.pluginRepositoryFile, "rb")
                 try:
                     try:
                         parser.parse(f)
                     except UnicodeEncodeError:
                         f.seek(0)
                         buf = cStringIO.StringIO(f.read())
                         parser.parse(buf)
                 finally:
                     f.close()
             except IOError:
                 KQMessageBox.critical(None,
                     self.trUtf8("Read plugins repository file"),
                     self.trUtf8("<p>The plugins repository file <b>%1</b> "
                                 "could not be read. Select Update</p>")\
                         .arg(self.pluginRepositoryFile))
                 return
             except XMLFatalParseError:
                 pass
             
             eh.showParseMessages()
             
             self.repositoryList.resizeColumnToContents(0)
             self.repositoryList.resizeColumnToContents(1)
             self.repositoryList.resizeColumnToContents(2)
             self.__resortRepositoryList()
             
             url = Preferences.getUI("PluginRepositoryUrl")
             if url != self.repositoryUrlEdit.text():
                 self.repositoryUrlEdit.setText(url)
                 KQMessageBox.warning(self,
                     self.trUtf8("Plugins Repository URL Changed"),
                     self.trUtf8("""The URL of the Plugins Repository has"""
                                 """ changed. Select the "Update" button to get"""
                                 """ the new repository file."""))
         else:
             KQMessageBox.critical(None,
                 self.trUtf8("Read plugins repository file"),
                 self.trUtf8("<p>The plugins repository file <b>%1</b> "
                             "has an unsupported format.</p>")\
                     .arg(self.pluginRepositoryFile))
     else:
         self.__repositoryMissing = True
         QTreeWidgetItem(self.repositoryList, \
             QStringList() \
                 << "" \
                 << self.trUtf8("No plugin repository file available."
                                "\nSelect Update."))
         self.repositoryList.resizeColumnToContents(1)
 def __readXMLMultiProject(self, fn, validating):
     """
     Private method to read the multi project data from an XML file.
     
     @param fn filename of the multi project file to be read (string or QString)
     @param validating flag indicating a validation of the XML file is
         requested (boolean)
     @return flag indicating success
     """
     fn = unicode(fn)
     if fn.lower().endswith("e4mz"):
         # work around for a bug in xmlproc
         validating = False
     
     parser = make_parser(validating)
     handler = MultiProjectHandler(self)
     er = XMLEntityResolver()
     eh = XMLErrorHandler()
     
     parser.setContentHandler(handler)
     parser.setEntityResolver(er)
     parser.setErrorHandler(eh)
     
     try:
         if fn.lower().endswith("e4mz"):
             try:
                 import gzip
             except ImportError:
                 QApplication.restoreOverrideCursor()
                 KQMessageBox.critical(None,
                     self.trUtf8("Read multiproject file"),
                     self.trUtf8("""Compressed multiproject files not supported."""
                                 """ The compression library is missing."""))
                 return False
             f = gzip.open(fn, "rb")
         else:
             f = open(fn, "rb")
         try:
             try:
                 parser.parse(f)
             except UnicodeEncodeError:
                 f.seek(0)
                 buf = cStringIO.StringIO(f.read())
                 parser.parse(buf)
         finally:
             f.close()
     except IOError:
         QApplication.restoreOverrideCursor()
         KQMessageBox.critical(None,
             self.trUtf8("Read multiproject file"),
             self.trUtf8("<p>The multiproject file <b>%1</b> could not be read.</p>")\
                 .arg(fn))
         return False
     except XMLFatalParseError:
         QApplication.restoreOverrideCursor()
         KQMessageBox.critical(None,
             self.trUtf8("Read multiproject file"),
             self.trUtf8("<p>The multiproject file <b>%1</b> has invalid "
                 "contents.</p>").arg(fn))
         eh.showParseMessages()
         return False
     
     QApplication.restoreOverrideCursor()
     eh.showParseMessages()
     QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
     QApplication.processEvents()
     return True
 def readTemplates(self, filename = None):
     """
     Public method to read in the templates file (.e4c)
     
     @param filename name of a templates file to read (string or QString)
     """
     try:
         if filename is None:
             fn = os.path.join(Utilities.getConfigDir(), "eric4templates.e4c")
             if not os.path.exists(fn):
                 return
         else:
             fn = unicode(filename)
         f = open(fn, "rb")
         line = f.readline()
         dtdLine = f.readline()
         f.close()
     except IOError:
         KQMessageBox.critical(None,
             self.trUtf8("Read templates"),
             self.trUtf8("<p>The templates file <b>%1</b> could not be read.</p>")
                 .arg(fn))
         return
         
     # now read the file
     if line.startswith('<?xml'):
         parser = make_parser(dtdLine.startswith("<!DOCTYPE"))
         handler = TemplatesHandler(templateViewer = self)
         er = XMLEntityResolver()
         eh = XMLErrorHandler()
         
         parser.setContentHandler(handler)
         parser.setEntityResolver(er)
         parser.setErrorHandler(eh)
         
         try:
             f = open(fn, "rb")
             try:
                 try:
                     parser.parse(f)
                 except UnicodeEncodeError:
                     f.seek(0)
                     buf = cStringIO.StringIO(f.read())
                     parser.parse(buf)
             finally:
                 f.close()
         except IOError:
             KQMessageBox.critical(None,
                 self.trUtf8("Read templates"),
                 self.trUtf8("<p>The templates file <b>%1</b> could not be read.</p>")
                     .arg(fn))
             return
         except XMLFatalParseError:
             pass
             
         eh.showParseMessages()
     else:
         KQMessageBox.critical(None,
             self.trUtf8("Read templates"),
             self.trUtf8("<p>The templates file <b>%1</b> has an"
                         " unsupported format.</p>")
                 .arg(fn))
         line = f.readline()
         dtdLine = f.readline()
     finally:
         f.close()
 except IOError, err:
     KQMessageBox.critical(self,
         self.trUtf8("Import Highlighting Styles"),
         self.trUtf8("""<p>The highlighting styles could not be read"""
                     """ from file <b>%1</b>.</p><p>Reason: %2</p>""")\
             .arg(fn)\
             .arg(str(err))
     )
     return
 
 validating = dtdLine.startswith("<!DOCTYPE")
 parser = make_parser(validating)
 handler = HighlightingStylesHandler(lexers)
 er = XMLEntityResolver()
 eh = XMLErrorHandler()
 
 parser.setContentHandler(handler)
 parser.setEntityResolver(er)
 parser.setErrorHandler(eh)
 
 try:
     f = open(fn, "rb")
     try:
         try:
             parser.parse(f)
         except UnicodeEncodeError:
             f.seek(0)