def importShortcuts(fn): """ Module function to import the keyboard shortcuts for the defined E5Actions. @param fn filename of the import file (string) """ # let the plugin manager create on demand plugin objects pm = e5App().getObject("PluginManager") pm.initOnDemandPlugins() f = QFile(fn) if f.open(QIODevice.ReadOnly): from E5XML.ShortcutsReader import ShortcutsReader reader = ShortcutsReader(f) reader.readXML() f.close() if not reader.hasError(): shortcuts = reader.getShortcuts() setActions(shortcuts) saveShortcuts() syncPreferences() else: E5MessageBox.critical( None, QCoreApplication.translate( "Shortcuts", "Import Keyboard Shortcuts"), QCoreApplication.translate( "Shortcuts", "<p>The keyboard shortcuts could not be read from file" " <b>{0}</b>.</p>") .format(fn)) return
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