def add_plugin(self): info = "" if iswindows else " [.zip %s]" % _("files") path = choose_files( self, "add a plugin dialog", _("Add plugin"), filters=[(_("Plugins") + info, ["zip"])], all_files=False, select_only_single_file=True, ) if not path: return path = path[0] if path and os.access(path, os.R_OK) and path.lower().endswith(".zip"): if not question_dialog( self, _("Are you sure?"), "<p>" + _( "Installing plugins is a <b>security risk</b>. " "Plugins can contain a virus/malware. " "Only install it if you got it from a trusted source." " Are you sure you want to proceed?" ), show_copy_button=False, ): return from calibre.customize.ui import config installed_plugins = frozenset(config["plugins"]) try: plugin = add_plugin(path) except NameConflict as e: return error_dialog(self, _("Already exists"), unicode(e), show=True) self._plugin_model.populate() self._plugin_model.reset() self.changed_signal.emit() self.check_for_add_to_toolbars(plugin, previously_installed=plugin.name in installed_plugins) info_dialog( self, _("Success"), _( "Plugin <b>{0}</b> successfully installed under <b>" " {1} plugins</b>. You may have to restart calibre " "for the plugin to take effect." ).format(plugin.name, plugin.type), show=True, show_copy_button=False, ) idx = self._plugin_model.plugin_to_index_by_properties(plugin) if idx.isValid(): self.highlight_index(idx) else: error_dialog(self, _("No valid plugin path"), _("%s is not a valid plugin path") % path).exec_()
def add_plugin(self): info = '' if iswindows else ' [.zip %s]' % _('files') path = choose_files(self, 'add a plugin dialog', _('Add plugin'), filters=[(_('Plugins') + info, ['zip'])], all_files=False, select_only_single_file=True) if not path: return path = path[0] if path and os.access(path, os.R_OK) and path.lower().endswith('.zip'): if not question_dialog( self, _('Are you sure?'), '<p>' + _('Installing plugins is a <b>security risk</b>. ' 'Plugins can contain a virus/malware. ' 'Only install it if you got it from a trusted source.' ' Are you sure you want to proceed?'), show_copy_button=False): return from calibre.customize.ui import config installed_plugins = frozenset(config['plugins']) try: plugin = add_plugin(path) except NameConflict as e: return error_dialog(self, _('Already exists'), str(e), show=True) self._plugin_model.beginResetModel() self._plugin_model.populate() self._plugin_model.endResetModel() self.changed_signal.emit() self.check_for_add_to_toolbars(plugin, previously_installed=plugin.name in installed_plugins) info_dialog(self, _('Success'), _('Plugin <b>{0}</b> successfully installed under <b>' '{1}</b>. You may have to restart calibre ' 'for the plugin to take effect.').format( plugin.name, plugin.type), show=True, show_copy_button=False) idx = self._plugin_model.plugin_to_index_by_properties(plugin) if idx.isValid(): self.highlight_index(idx) else: error_dialog(self, _('No valid plugin path'), _('%s is not a valid plugin path') % path).exec()
def add_plugin(self): info = '' if iswindows else ' [.zip %s]'%_('files') path = choose_files(self, 'add a plugin dialog', _('Add plugin'), filters=[(_('Plugins') + info, ['zip'])], all_files=False, select_only_single_file=True) if not path: return path = path[0] if path and os.access(path, os.R_OK) and path.lower().endswith('.zip'): if not question_dialog(self, _('Are you sure?'), '<p>' + _('Installing plugins is a <b>security risk</b>. ' 'Plugins can contain a virus/malware. ' 'Only install it if you got it from a trusted source.' ' Are you sure you want to proceed?'), show_copy_button=False): return from calibre.customize.ui import config installed_plugins = frozenset(config['plugins']) try: plugin = add_plugin(path) except NameConflict as e: return error_dialog(self, _('Already exists'), unicode(e), show=True) self._plugin_model.beginResetModel() self._plugin_model.populate() self._plugin_model.endResetModel() self.changed_signal.emit() self.check_for_add_to_toolbars(plugin, previously_installed=plugin.name in installed_plugins) info_dialog(self, _('Success'), _('Plugin <b>{0}</b> successfully installed under <b>' '{1} plugins</b>. You may have to restart calibre ' 'for the plugin to take effect.').format(plugin.name, plugin.type), show=True, show_copy_button=False) idx = self._plugin_model.plugin_to_index_by_properties(plugin) if idx.isValid(): self.highlight_index(idx) else: error_dialog(self, _('No valid plugin path'), _('%s is not a valid plugin path')%path).exec_()
def _install_clicked(self): display_plugin = self._selected_display_plugin() if not question_dialog(self, _('Install %s')%display_plugin.name, '<p>' + _('Installing plugins is a <b>security risk</b>. ' 'Plugins can contain a virus/malware. ' 'Only install it if you got it from a trusted source.' ' Are you sure you want to proceed?'), show_copy_button=False): return if display_plugin.uninstall_plugins: uninstall_names = list(display_plugin.uninstall_plugins) if DEBUG: prints('Uninstalling plugin: ', ', '.join(uninstall_names)) for name_to_remove in uninstall_names: self._uninstall_plugin(name_to_remove) plugin_zip_url = display_plugin.zip_url if DEBUG: prints('Downloading plugin ZIP attachment: ', plugin_zip_url) self.gui.status_bar.showMessage(_('Downloading plugin ZIP attachment: %s') % plugin_zip_url) zip_path = self._download_zip(plugin_zip_url) if DEBUG: prints('Installing plugin: ', zip_path) self.gui.status_bar.showMessage(_('Installing plugin: %s') % zip_path) do_restart = False try: from calibre.customize.ui import config installed_plugins = frozenset(config['plugins']) try: plugin = add_plugin(zip_path) except NameConflict as e: return error_dialog(self.gui, _('Already exists'), unicode_type(e), show=True) # Check for any toolbars to add to. widget = ConfigWidget(self.gui) widget.gui = self.gui widget.check_for_add_to_toolbars(plugin, previously_installed=plugin.name in installed_plugins) self.gui.status_bar.showMessage(_('Plugin installed: %s') % display_plugin.name) d = info_dialog(self.gui, _('Success'), _('Plugin <b>{0}</b> successfully installed under <b>' ' {1} plugins</b>. You may have to restart calibre ' 'for the plugin to take effect.').format(plugin.name, plugin.type), show_copy_button=False) b = d.bb.addButton(_('&Restart calibre now'), d.bb.AcceptRole) b.setIcon(QIcon(I('lt.png'))) d.do_restart = False def rf(): d.do_restart = True b.clicked.connect(rf) d.set_details('') d.exec_() b.clicked.disconnect() do_restart = d.do_restart display_plugin.plugin = plugin # We cannot read the 'actual' version information as the plugin will not be loaded yet display_plugin.installed_version = display_plugin.available_version except: if DEBUG: prints('ERROR occurred while installing plugin: %s'%display_plugin.name) traceback.print_exc() error_dialog(self.gui, _('Install plugin failed'), _('A problem occurred while installing this plugin.' ' This plugin will now be uninstalled.' ' Please post the error message in details below into' ' the forum thread for this plugin and restart calibre.'), det_msg=traceback.format_exc(), show=True) if DEBUG: prints('Due to error now uninstalling plugin: %s'%display_plugin.name) remove_plugin(display_plugin.name) display_plugin.plugin = None display_plugin.uninstall_plugins = [] if self.proxy_model.filter_criteria in [FILTER_NOT_INSTALLED, FILTER_UPDATE_AVAILABLE]: self.model.beginResetModel(), self.model.endResetModel() self._select_and_focus_view() else: self.model.refresh_plugin(display_plugin) self._select_and_focus_view(change_selection=False) if do_restart: self.do_restart = True self.accept()
% path_to_zip_file) with self._lock: self.loaded_plugins[plugin_name] = (path_to_zip_file, names) return plugin_name loader = PluginLoader() sys.meta_path.insert(0, loader) if __name__ == '__main__': from tempfile import NamedTemporaryFile from calibre.customize.ui import add_plugin from calibre import CurrentDir path = sys.argv[-1] with NamedTemporaryFile(suffix='.zip') as f: with zipfile.ZipFile(f, 'w') as zf: with CurrentDir(path): for x in os.listdir('.'): if x[0] != '.': print ('Adding', x) zf.write(x) if os.path.isdir(x): for y in os.listdir(x): zf.write(os.path.join(x, y)) add_plugin(f.name) print ('Added plugin from', sys.argv[-1])
def _install_clicked(self): display_plugin = self._selected_display_plugin() if not question_dialog( self, _('Install %s') % display_plugin.name, '<p>' + _('Installing plugins is a <b>security risk</b>. ' 'Plugins can contain a virus/malware. ' 'Only install it if you got it from a trusted source.' ' Are you sure you want to proceed?'), show_copy_button=False): return if display_plugin.uninstall_plugins: uninstall_names = list(display_plugin.uninstall_plugins) if DEBUG: prints('Uninstalling plugin: ', ', '.join(uninstall_names)) for name_to_remove in uninstall_names: self._uninstall_plugin(name_to_remove) plugin_zip_url = display_plugin.zip_url if DEBUG: prints('Downloading plugin zip attachment: ', plugin_zip_url) self.gui.status_bar.showMessage( _('Downloading plugin zip attachment: %s') % plugin_zip_url) zip_path = self._download_zip(plugin_zip_url) if DEBUG: prints('Installing plugin: ', zip_path) self.gui.status_bar.showMessage(_('Installing plugin: %s') % zip_path) do_restart = False try: from calibre.customize.ui import config installed_plugins = frozenset(config['plugins']) try: plugin = add_plugin(zip_path) except NameConflict as e: return error_dialog(self.gui, _('Already exists'), unicode(e), show=True) # Check for any toolbars to add to. widget = ConfigWidget(self.gui) widget.gui = self.gui widget.check_for_add_to_toolbars(plugin, previously_installed=plugin.name in installed_plugins) self.gui.status_bar.showMessage( _('Plugin installed: %s') % display_plugin.name) d = info_dialog( self.gui, _('Success'), _('Plugin <b>{0}</b> successfully installed under <b>' ' {1} plugins</b>. You may have to restart calibre ' 'for the plugin to take effect.').format( plugin.name, plugin.type), show_copy_button=False) b = d.bb.addButton(_('Restart calibre now'), d.bb.AcceptRole) b.setIcon(QIcon(I('lt.png'))) d.do_restart = False def rf(): d.do_restart = True b.clicked.connect(rf) d.set_details('') d.exec_() b.clicked.disconnect() do_restart = d.do_restart display_plugin.plugin = plugin # We cannot read the 'actual' version information as the plugin will not be loaded yet display_plugin.installed_version = display_plugin.available_version except: if DEBUG: prints('ERROR occurred while installing plugin: %s' % display_plugin.name) traceback.print_exc() error_dialog( self.gui, _('Install Plugin Failed'), _('A problem occurred while installing this plugin.' ' This plugin will now be uninstalled.' ' Please post the error message in details below into' ' the forum thread for this plugin and restart Calibre.'), det_msg=traceback.format_exc(), show=True) if DEBUG: prints('Due to error now uninstalling plugin: %s' % display_plugin.name) remove_plugin(display_plugin.name) display_plugin.plugin = None display_plugin.uninstall_plugins = [] if self.proxy_model.filter_criteria in [ FILTER_NOT_INSTALLED, FILTER_UPDATE_AVAILABLE ]: self.model.beginResetModel(), self.model.endResetModel() self._select_and_focus_view() else: self.model.refresh_plugin(display_plugin) self._select_and_focus_view(change_selection=False) if do_restart: self.do_restart = True self.accept()
raise InvalidPlugin( ('The plugin in %r is invalid. It does not ' 'contain a top-level __init__.py file') % path_to_zip_file) with self._lock: self.loaded_plugins[plugin_name] = (path_to_zip_file, names) return plugin_name loader = PluginLoader() sys.meta_path.insert(0, loader) if __name__ == '__main__': from tempfile import NamedTemporaryFile from calibre.customize.ui import add_plugin from calibre import CurrentDir path = sys.argv[-1] with NamedTemporaryFile(suffix='.zip') as f: with zipfile.ZipFile(f, 'w') as zf: with CurrentDir(path): for x in os.listdir('.'): if x[0] != '.': print('Adding', x) zf.write(x) if os.path.isdir(x): for y in os.listdir(x): zf.write(os.path.join(x, y)) add_plugin(f.name) print('Added plugin from', sys.argv[-1])
def _install_clicked(self): display_plugin = self._selected_display_plugin() if not question_dialog(self, _('Install %s')%display_plugin.name, '<p>' + \ _('Installing plugins is a <b>security risk</b>. ' 'Plugins can contain a virus/malware. ' 'Only install it if you got it from a trusted source.' ' Are you sure you want to proceed?'), show_copy_button=False): return if display_plugin.uninstall_plugins: uninstall_names = list(display_plugin.uninstall_plugins) if DEBUG: prints('Uninstalling plugin: ', ', '.join(uninstall_names)) for name_to_remove in uninstall_names: self._uninstall_plugin(name_to_remove) if DEBUG: prints('Locating zip file for %s: %s'% (display_plugin.name, display_plugin.forum_link)) self.gui.status_bar.showMessage( _('Locating zip file for %(name)s: %(link)s') % dict( name=display_plugin.name, link=display_plugin.forum_link)) plugin_zip_url = self._read_zip_attachment_url(display_plugin.forum_link) if not plugin_zip_url: return error_dialog(self.gui, _('Install Plugin Failed'), _('Unable to locate a plugin zip file for <b>%s</b>') % display_plugin.name, det_msg=display_plugin.forum_link, show=True) if DEBUG: prints('Downloading plugin zip attachment: ', plugin_zip_url) self.gui.status_bar.showMessage(_('Downloading plugin zip attachment: %s') % plugin_zip_url) zip_path = self._download_zip(plugin_zip_url) if DEBUG: prints('Installing plugin: ', zip_path) self.gui.status_bar.showMessage(_('Installing plugin: %s') % zip_path) try: try: plugin = add_plugin(zip_path) except NameConflict as e: return error_dialog(self.gui, _('Already exists'), unicode(e), show=True) # Check for any toolbars to add to. widget = ConfigWidget(self.gui) widget.gui = self.gui widget.check_for_add_to_toolbars(plugin) self.gui.status_bar.showMessage(_('Plugin installed: %s') % display_plugin.name) info_dialog(self.gui, _('Success'), _('Plugin <b>{0}</b> successfully installed under <b>' ' {1} plugins</b>. You may have to restart calibre ' 'for the plugin to take effect.').format(plugin.name, plugin.type), show=True, show_copy_button=False) display_plugin.plugin = plugin # We cannot read the 'actual' version information as the plugin will not be loaded yet display_plugin.installed_version = display_plugin.available_version except: if DEBUG: prints('ERROR occurred while installing plugin: %s'%display_plugin.name) traceback.print_exc() error_dialog(self.gui, _('Install Plugin Failed'), _('A problem occurred while installing this plugin.' ' This plugin will now be uninstalled.' ' Please post the error message in details below into' ' the forum thread for this plugin and restart Calibre.'), det_msg=traceback.format_exc(), show=True) if DEBUG: prints('Due to error now uninstalling plugin: %s'%display_plugin.name) remove_plugin(display_plugin.name) display_plugin.plugin = None display_plugin.uninstall_plugins = [] if self.proxy_model.filter_criteria in [FILTER_NOT_INSTALLED, FILTER_UPDATE_AVAILABLE]: self.model.reset() self._select_and_focus_view() else: self.model.refresh_plugin(display_plugin) self._select_and_focus_view(change_selection=False)
def _install_clicked(self): display_plugin = self._selected_display_plugin() if not question_dialog( self, _("Install %s") % display_plugin.name, "<p>" + _( "Installing plugins is a <b>security risk</b>. " "Plugins can contain a virus/malware. " "Only install it if you got it from a trusted source." " Are you sure you want to proceed?" ), show_copy_button=False, ): return if display_plugin.uninstall_plugins: uninstall_names = list(display_plugin.uninstall_plugins) if DEBUG: prints("Uninstalling plugin: ", ", ".join(uninstall_names)) for name_to_remove in uninstall_names: self._uninstall_plugin(name_to_remove) if DEBUG: prints("Locating zip file for %s: %s" % (display_plugin.name, display_plugin.forum_link)) self.gui.status_bar.showMessage( _("Locating zip file for %(name)s: %(link)s") % dict(name=display_plugin.name, link=display_plugin.forum_link) ) plugin_zip_url = self._read_zip_attachment_url(display_plugin.forum_link) if not plugin_zip_url: return error_dialog( self.gui, _("Install Plugin Failed"), _("Unable to locate a plugin zip file for <b>%s</b>") % display_plugin.name, det_msg=display_plugin.forum_link, show=True, ) if DEBUG: prints("Downloading plugin zip attachment: ", plugin_zip_url) self.gui.status_bar.showMessage(_("Downloading plugin zip attachment: %s") % plugin_zip_url) zip_path = self._download_zip(plugin_zip_url) if DEBUG: prints("Installing plugin: ", zip_path) self.gui.status_bar.showMessage(_("Installing plugin: %s") % zip_path) do_restart = False try: try: plugin = add_plugin(zip_path) except NameConflict as e: return error_dialog(self.gui, _("Already exists"), unicode(e), show=True) # Check for any toolbars to add to. widget = ConfigWidget(self.gui) widget.gui = self.gui widget.check_for_add_to_toolbars(plugin) self.gui.status_bar.showMessage(_("Plugin installed: %s") % display_plugin.name) d = info_dialog( self.gui, _("Success"), _( "Plugin <b>{0}</b> successfully installed under <b>" " {1} plugins</b>. You may have to restart calibre " "for the plugin to take effect." ).format(plugin.name, plugin.type), show_copy_button=False, ) b = d.bb.addButton(_("Restart calibre now"), d.bb.AcceptRole) b.setIcon(QIcon(I("lt.png"))) d.do_restart = False def rf(): d.do_restart = True b.clicked.connect(rf) d.set_details("") d.exec_() b.clicked.disconnect() do_restart = d.do_restart display_plugin.plugin = plugin # We cannot read the 'actual' version information as the plugin will not be loaded yet display_plugin.installed_version = display_plugin.available_version except: if DEBUG: prints("ERROR occurred while installing plugin: %s" % display_plugin.name) traceback.print_exc() error_dialog( self.gui, _("Install Plugin Failed"), _( "A problem occurred while installing this plugin." " This plugin will now be uninstalled." " Please post the error message in details below into" " the forum thread for this plugin and restart Calibre." ), det_msg=traceback.format_exc(), show=True, ) if DEBUG: prints("Due to error now uninstalling plugin: %s" % display_plugin.name) remove_plugin(display_plugin.name) display_plugin.plugin = None display_plugin.uninstall_plugins = [] if self.proxy_model.filter_criteria in [FILTER_NOT_INSTALLED, FILTER_UPDATE_AVAILABLE]: self.model.reset() self._select_and_focus_view() else: self.model.refresh_plugin(display_plugin) self._select_and_focus_view(change_selection=False) if do_restart: self.do_restart = True self.accept()