def save_image(self): filters=[('Images', ['png', 'jpeg', 'jpg'])] f = choose_save_file(self, 'viewer image view save dialog', _('Choose a file to save to'), filters=filters, all_files=False) if f: self.current_img.save(f)
def export_urls(self): path = choose_save_file( self, 'search-net-urls', _('Choose URLs file'), filters=[(_('URL files'), ['json'])], initial_filename='search-urls.json') if path: with lopen(path, 'wb') as f: f.write(self.serialized_urls)
def start_download(self, request): if not self.gui: return url = unicode(request.url().toString(NO_URL_FORMATTING)) cf = self.get_cookies() filename = get_download_filename(url, cf) ext = os.path.splitext(filename)[1][1:].lower() filename = ascii_filename(filename[:60] + '.' + ext) if ext not in BOOK_EXTENSIONS: if ext == 'acsm': from calibre.gui2.dialogs.confirm_delete import confirm if not confirm('<p>' + _('This ebook is a DRMed EPUB file. ' 'You will be prompted to save this file to your ' 'computer. Once it is saved, open it with ' '<a href="https://www.adobe.com/products/digitaleditions/">' 'Adobe Digital Editions</a> (ADE).<p>ADE, in turn ' 'will download the actual ebook, which will be a ' '.epub file. You can add this book to calibre ' 'using "Add Books" and selecting the file from ' 'the ADE library folder.'), 'acsm_download', self): return name = choose_save_file(self, 'web-store-download-unknown', _('File is not a supported ebook type. Save to disk?'), initial_filename=filename) if name: self.gui.download_ebook(url, cf, name, name, False, create_browser=self.create_browser) else: show_download_info(filename, self) self.gui.download_ebook(url, cf, filename, tags=self.tags, create_browser=self.create_browser)
def export_bookmarks(self): filename = choose_save_file( self, 'export-viewer-bookmarks', _('Export bookmarks'), filters=[(_('Saved bookmarks'), ['calibre-bookmarks'])], all_files=False, initial_filename='bookmarks.calibre-bookmarks') if filename: with lopen(filename, 'wb') as fileobj: fileobj.write(json.dumps(self.get_bookmarks(), indent=True))
def export_key(self): if not self.listy.currentItem(): errmsg = "No keyfile selected to export. Highlight a keyfile first." r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION), _(errmsg), show=True, show_copy_button=False) return keyname = str(self.listy.currentItem().text()) unique_dlg_name = PLUGIN_NAME + "export {0} keys".format(self.key_type_name).replace(' ', '_') #takes care of automatically remembering last directory caption = "Save {0} File as...".format(self.key_type_name) filters = [("{0} Files".format(self.key_type_name), ["{0}".format(self.keyfile_ext)])] defaultname = "{0}.{1}".format(keyname, self.keyfile_ext) filename = choose_save_file(self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname) if filename: if self.binary_file: with open(filename, 'wb') as fname: fname.write(codecs.decode(self.plugin_keys[keyname],'hex')) elif self.json_file: with open(filename, 'w') as fname: fname.write(json.dumps(self.plugin_keys[keyname])) elif self.android_file: with open(filename, 'w') as fname: for key in self.plugin_keys[keyname]: fname.write(key) fname.write('\n') else: with open(filename, 'w') as fname: fname.write(self.plugin_keys[keyname])
def save_copy(self): c = current_container() ext = c.path_to_ebook.rpartition(".")[-1] path = choose_save_file( self.gui, "tweak_book_save_copy", _("Choose path"), filters=[(_("Book (%s)") % ext.upper(), [ext.lower()])], all_files=False, ) if not path: return tdir = self.mkdtemp(prefix="save-copy-") container = clone_container(c, tdir) for name, ed in editors.iteritems(): if ed.is_modified or not ed.is_synced_to_container: self.commit_editor_to_container(name, container) def do_save(c, path, tdir): save_container(c, path) shutil.rmtree(tdir, ignore_errors=True) return path self.gui.blocking_job( "save_copy", _("Saving copy, please wait..."), self.copy_saved, do_save, container, path, tdir )
def choose_destination(self): path = choose_save_file(self, 'edit-book-destination-for-generated-epub', _('Choose destination'), filters=[ (_('EPUB files'), ['epub'])], all_files=False) if path: if not path.lower().endswith('.epub'): path += '.epub' self.dest.setText(path)
def export_bookmarks(self): filename = choose_save_file( self, 'export-viewer-bookmarks', _('Export bookmarks'), filters=[(_('Saved bookmarks'), ['pickle'])], all_files=False, initial_filename='bookmarks.pickle') if filename: with open(filename, 'wb') as fileobj: cPickle.dump(self.get_bookmarks(), fileobj, -1)
def export_key(self): if not self.listy.currentItem(): errmsg = u"No keyfile selected to export. Highlight a keyfile first." r = error_dialog( None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION), _(errmsg), show=True, show_copy_button=False ) return keyname = unicode(self.listy.currentItem().text()) unique_dlg_name = PLUGIN_NAME + u"export {0} keys".format(self.key_type_name).replace( " ", "_" ) # takes care of automatically remembering last directory caption = u"Save {0} File as...".format(self.key_type_name) filters = [(u"{0} Files".format(self.key_type_name), [u"{0}".format(self.keyfile_ext)])] defaultname = u"{0}.{1}".format(keyname, self.keyfile_ext) filename = choose_save_file( self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname ) if filename: with file(filename, "wb") as fname: if self.binary_file: fname.write(self.plugin_keys[keyname].decode("hex")) elif self.json_file: fname.write(json.dumps(self.plugin_keys[keyname])) elif self.android_file: for key in self.plugin_keys[keyname]: fname.write(key) fname.write("\n") else: fname.write(self.plugin_keys[keyname])
def export_bookmarks(self): filename = choose_save_file( self, 'export-viewer-bookmarks', _('Export Bookmarks'), filters=[(_('Saved Bookmarks'), ['pickle'])], all_files=False, initial_filename='bookmarks.pickle') if filename: with open(filename, 'wb') as fileobj: cPickle.dump(self.get_bookmarks(), fileobj, -1)
def export_template(self): path = choose_save_file( self, 'custom-list-template', _('Choose template file'), filters=[(_('Template files'), ['json'])], initial_filename='custom-list-template.json') if path: raw = self.serialize(self.current_template) with lopen(path, 'wb') as f: f.write(raw)
def export_template(self): path = choose_save_file( self, 'custom-list-template', _('Choose template file'), filters=[(_('Template files'), ['json'])], initial_filename='custom-list-template.json') if path: raw = self.serialize(self.current_template) with lopen(path, 'wb') as f: f.write(as_bytes(raw))
def save_image(self): filters=[('Images', ['png', 'jpeg', 'jpg'])] f = choose_save_file(self, 'viewer image view save dialog', _('Choose a file to save to'), filters=filters, all_files=False) if f: from calibre.utils.img import save_image save_image(self.current_img.toImage(), f)
def save_template(self): filename = choose_save_file(self, 'template_dialog_save_templates', _('Save template to file'), filters=[(_('Template file'), ['txt'])]) if filename: with open(filename, 'w') as f: f.write(unicode_type(self.textbox.toPlainText()))
def choose_file(self): ans = choose_save_file(self, self.OUTPUT_NAME, _('PDF file'), filters=[(_('PDF file'), ['pdf'])], all_files=False, initial_filename=self.default_file_name) if ans: self.file_name.setText(ans)
def choose_file(self): ans = choose_save_file(self, 'print-to-pdf-choose-file', _('PDF file'), filters=[(_('PDF file'), 'pdf')], all_files=False, initial_filename=self.default_file_name) if ans: self.file_name.setText(ans)
def export(self, name): path = choose_save_file(self, 'tweak_book_export_file', _('Choose location'), filters=[(_('Files'), [name.rpartition('.')[-1].lower()])], all_files=False) if path: self.export_requested.emit(name, path)
def export(self, name): path = choose_save_file( self, "tweak_book_export_file", _("Choose location"), filters=[(_("Files"), [name.rpartition(".")[-1].lower()])], all_files=False, ) if path: self.export_requested.emit(name, path)
def export_rules(self): rules = self.rules_widget.rules if not rules: return error_dialog(self, _('No rules'), _( 'There are no rules to export'), show=True) path = choose_save_file(self, 'export-style-transform-rules', _('Choose file for exported rules'), initial_filename='rules.txt') if path: raw = export_rules(rules) with open(path, 'wb') as f: f.write(raw)
def export_bookmarks(self): filename = choose_save_file( self, 'export-viewer-bookmarks', _('Export bookmarks'), filters=[(_('Saved bookmarks'), ['calibre-bookmarks'])], all_files=False, initial_filename='bookmarks.calibre-bookmarks') if filename: data = json.dumps(self.get_bookmarks(), indent=True) if not isinstance(data, bytes): data = data.encode('utf-8') with lopen(filename, 'wb') as fileobj: fileobj.write(data)
def sr_save_clicked(self): filename = choose_save_file( self, 'sr_saved_patterns', _('Save Calibre Search-Replace definitions file'), filters=[(_('Calibre Search-Replace definitions file'), ['csr'])]) if filename: with codecs.open(filename, 'w', 'utf-8') as f: for search, replace in self.get_definitions(): f.write(search + u'\n' + replace + u'\n\n')
def sr_save_clicked(self): filename = choose_save_file(self, 'sr_saved_patterns', _('Save Calibre Search-Replace definitions file'), filters=[ (_('Calibre Search-Replace definitions file'), ['csr']) ]) if filename: with codecs.open(filename, 'w', 'utf-8') as f: for search, replace in self.get_definitions(): f.write(search + u'\n' + replace + u'\n\n')
def sr_save_clicked(self): from calibre.ebooks.conversion.cli import escape_sr_pattern as escape filename = choose_save_file( self, 'sr_saved_patterns', _('Save calibre search-replace definitions file'), filters=[(_('calibre search-replace definitions file'), ['csr'])]) if filename: with codecs.open(filename, 'w', 'utf-8') as f: for search, replace in self.get_definitions(): f.write(escape(search) + '\n' + escape(replace) + '\n\n')
def save_to_file(self): filters = [(self.export_format.currentText(), self.export_format.currentData())] path = choose_save_file( self, 'annots-export-save', _('File for exports'), filters=filters, initial_filename=self.initial_filename() + '.' + filters[0][1]) if path: data = self.exported_data().encode('utf-8') with open(path, 'wb') as f: f.write(codecs.BOM_UTF8) f.write(data) self.accept()
def export_rules(self): rules = self.rules_widget.rules if not rules: return error_dialog(self, _('No rules'), _( 'There are no rules to export'), show=True) path = choose_save_file(self, self.DIR_SAVE_NAME, _('Choose file for exported rules'), initial_filename=self.INITIAL_FILE_NAME) if path: f = self.__class__.export_func raw = f(rules) with open(path, 'wb') as f: f.write(raw)
def save_cover(self): from calibre.gui2.ui import get_gui book_id = self.data.get('id') db = get_gui().current_db.new_api path = choose_save_file( self, 'save-cover-from-book-details', _('Choose cover save location'), filters=[(_('JPEG images'), ['jpg', 'jpeg'])], all_files=False, initial_filename='{}.jpeg'.format(sanitize_file_name(db.field_for('title', book_id, default_value='cover'))) ) if path: db.copy_cover_to(book_id, path)
def export_rules(self): path = choose_save_file(self, 'export-coloring-rules', _('Choose file to export to'), filters=[(_('Rules'), ['rules'])], all_files=False, initial_filename=self.pref_name + '.rules') if path: rules = { 'version': self.model.EXIM_VERSION, 'type': self.model.pref_name, 'rules': self.model.rules_as_list(for_export=True) } with lopen(path, 'wb') as f: f.write(json.dumps(rules, indent=2))
def sr_save_clicked(self): from calibre.ebooks.conversion.cli import escape_sr_pattern as escape filename = choose_save_file(self, 'sr_saved_patterns', _('Save calibre search-replace definitions file'), filters=[ (_('calibre search-replace definitions file'), ['csr']) ]) if filename: with codecs.open(filename, 'w', 'utf-8') as f: for search, replace in self.get_definitions(): f.write(escape(search) + u'\n' + escape(replace) + u'\n\n')
def save_cover(self): from calibre.gui2.ui import get_gui book_id = self.data.get('id') db = get_gui().current_db.new_api path = choose_save_file( self, 'save-cover-from-book-details', _('Choose cover save location'), filters=[(_('JPEG images'), ['jpg', 'jpeg'])], all_files=False, initial_filename='{}.jpeg'.format(db.field_for('title', book_id, default_value='cover')) ) if path: db.copy_cover_to(book_id, path)
def to_csv(self): w = self.stack.currentWidget() category = self.reports.currentItem().data(Qt.DisplayRole) if not hasattr(w, 'to_csv'): return error_dialog(self, _('Not supported'), _( 'Export of %s data is not supported') % category, show=True) data = w.to_csv() fname = choose_save_file(self, 'report-csv-export', _('Choose a filename for the data'), filters=[ (_('CSV files'), ['csv'])], all_files=False, initial_filename='%s.csv' % category) if fname: with open(fname, 'wb') as f: f.write(data)
def choose_destination(self): path = choose_save_file( self, "edit-book-destination-for-generated-epub", _("Choose destination"), filters=[(_("EPUB files"), ["epub"])], all_files=False, ) if path: if not path.lower().endswith(".epub"): path += ".epub" self.dest.setText(path)
def new_book(self): if not self._check_before_open(): return d = NewBook(self.gui) if d.exec_() == d.Accepted: fmt = d.fmt path = choose_save_file(self.gui, 'edit-book-new-book', _('Choose file location'), filters=[(fmt.upper(), (fmt,))], all_files=False) if path is not None: from calibre.ebooks.oeb.polish.create import create_book create_book(d.mi, path, fmt=fmt) self.open_book(path=path)
def save_recipe(self): idx = self.view.currentIndex() if idx.isValid(): src = self.model.script(idx) if src is not None: path = choose_save_file( self, 'save-custom-recipe', _('Save recipe'), filters=[(_('Recipes'), ['recipe'])], all_files=False, initial_filename='{}.recipe'.format(self.model.title(idx)) ) if path: with open(path, 'wb') as f: f.write(as_bytes(src))
def download_finished(self, download_id): self.central.download_progress.remove_item(download_id) download_item = self.download_data.pop(download_id) path = download_item.path() fname = os.path.basename(path) if download_item.state() == download_item.DownloadInterrupted: error_dialog(self, _('Download failed'), _( 'Download of {0} failed with error: {1}').format(fname, download_item.interruptReasonString()), show=True) return ext = fname.rpartition('.')[-1].lower() if ext not in BOOK_EXTENSIONS: if ext == 'acsm': if not confirm('<p>' + _( 'This e-book is a DRMed EPUB file. ' 'You will be prompted to save this file to your ' 'computer. Once it is saved, open it with ' '<a href="https://www.adobe.com/solutions/ebook/digital-editions.html">' 'Adobe Digital Editions</a> (ADE).<p>ADE, in turn ' 'will download the actual e-book, which will be a ' '.epub file. You can add this book to calibre ' 'using "Add Books" and selecting the file from ' 'the ADE library folder.'), 'acsm_download', self): return name = choose_save_file(self, 'web-store-download-unknown', _( 'File is not a supported e-book type. Save to disk?'), initial_filename=fname) if name: shutil.copyfile(path, name) os.remove(path) return t = RC(print_error=False) t.start() t.join(3.0) if t.conn is None: error_dialog(self, _('No running calibre'), _( 'No running calibre instance found. Please start calibre before trying to' ' download books.'), show=True) return tags = self.data['tags'] if isinstance(tags, string_or_bytes): tags = list(filter(None, [x.strip() for x in tags.split(',')])) data = json.dumps({'path': path, 'tags': tags}) if not isinstance(data, bytes): data = data.encode('utf-8') t.conn.send(b'web-store:' + data) t.conn.close() info_dialog(self, _('Download completed'), _( 'Download of {0} has been completed, the book was added to' ' your calibre library').format(fname), show=True)
def em_export_layout(self): filename = choose_save_file(self, 'em_import_export_field_list', _('Save column list to file'), filters=[(_('Column list'), ['json'])]) if filename: try: with open(filename, 'w') as f: json.dump(self.em_display_model.fields, f, indent=1) except Exception as err: error_dialog(self, _('Export field layout'), _('<p>Could not write field list. Error:<br>%s') % err, show=True)
def export_button_clicked(self): filename = choose_save_file(self, 'Exec Macro: Export macros', _('Export macros')) if not filename: return try: with open(filename, 'w') as fd: json.dump(prefs['macros'], fd, sort_keys=True, indent=4) info_dialog(self, _('Macros exported'), _('%d macros exported.') % len(prefs['macros']), show=True) except: error_dialog(self, _('Failed to export macros'), _( 'Failed to export macros, click "Show details" for more information.'), det_msg=traceback.format_exc(), show=True)
def create_theme(folder=None, parent=None): if folder is None: folder = choose_dir(parent, 'create-icon-theme-folder', _( 'Choose a folder from which to read the icons')) if not folder: return report = read_theme_from_folder(folder) d = ThemeCreateDialog(parent, report) if d.exec_() != d.Accepted: return d.save_metadata() raw = create_themeball(d.report) dest = choose_save_file(parent, 'create-icon-theme-dest', _( 'Choose destination for icon theme'), [(_('ZIP files'), ['zip'])], initial_filename='my-calibre-icon-theme.zip') if dest: with open(dest, 'wb') as f: f.write(raw)
def create_theme(folder=None, parent=None): if folder is None: folder = choose_dir(parent, 'create-icon-theme-folder', _( 'Choose a folder from which to read the icons')) if not folder: return report = read_theme_from_folder(folder) d = ThemeCreateDialog(parent, report) if d.exec_() != d.Accepted: return d.save_metadata() raw, prefix = create_themeball(d.report) dest = choose_save_file(parent, 'create-icon-theme-dest', _( 'Choose destination for icon theme'), [(_('ZIP files'), ['zip'])], initial_filename=prefix + '.zip') if dest: with open(dest, 'wb') as f: f.write(raw)
def create_theme(folder=None, parent=None): if folder is None: folder = choose_dir(parent, 'create-icon-theme-folder', _( 'Choose a folder from which to read the icons')) if not folder: return report = read_theme_from_folder(folder) d = ThemeCreateDialog(parent, report) if d.exec_() != d.Accepted: return d.save_metadata() d = Compress(d.report, parent=parent) d.exec_() if d.wasCanceled() or d.raw is None: return raw, prefix = d.raw, d.prefix dest = choose_save_file(parent, 'create-icon-theme-dest', _( 'Choose destination for icon theme'), [(_('ZIP files'), ['zip'])], initial_filename=prefix + '.zip') if dest: with lopen(dest, 'wb') as f: f.write(raw)
def export_searches(self, all=True): if all: searches = copy.deepcopy(tprefs['saved_searches']) if not searches: return error_dialog(self, _('No searches'), _( 'No searches available to be saved'), show=True) else: searches = [] for index in self.searches.selectionModel().selectedIndexes(): search = index.data(Qt.UserRole)[-1] searches.append(search.copy()) if not searches: return error_dialog(self, _('No searches'), _( 'No searches selected'), show=True) [s.__setitem__('mode', s.get('mode', 'regex')) for s in searches] path = choose_save_file(self, 'export-saved-searches', _('Choose file'), filters=[ (_('Saved Searches'), ['json'])], all_files=False) if path: if not path.lower().endswith('.json'): path += '.json' raw = json.dumps({'version':1, 'searches':searches}, ensure_ascii=False, indent=2, sort_keys=True) with open(path, 'wb') as f: f.write(raw.encode('utf-8'))
def save_copy(self): c = current_container() ext = c.path_to_ebook.rpartition('.')[-1] path = choose_save_file(self.gui, 'tweak_book_save_copy', _('Choose path'), filters=[(_('Book (%s)') % ext.upper(), [ext.lower()])], all_files=False) if not path: return tdir = self.mkdtemp(prefix='save-copy-') container = clone_container(c, tdir) for name, ed in editors.iteritems(): if ed.is_modified or not ed.is_synced_to_container: self.commit_editor_to_container(name, container) def do_save(c, path, tdir): save_container(c, path) shutil.rmtree(tdir, ignore_errors=True) return path self.gui.blocking_job('save_copy', _('Saving copy, please wait...'), self.copy_saved, do_save, container, path, tdir)
def export(self, name): path = choose_save_file(self, 'tweak_book_export_file', _('Choose location'), filters=[ (_('Files'), [name.rpartition('.')[-1].lower()])], all_files=False, initial_filename=name.split('/')[-1]) if path: self.export_requested.emit(name, path)
def choose_file(self): ans = choose_save_file(self, self.OUTPUT_NAME, _('PDF file'), filters=[(_('PDF file'), 'pdf')], all_files=False, initial_filename=self.default_file_name) if ans: self.file_name.setText(ans)