def accept(self): self.exporter.includeSched = ( self.frm.includeSched.isChecked()) self.exporter.includeMedia = ( self.frm.includeMedia.isChecked()) self.exporter.includeTags = ( self.frm.includeTags.isChecked()) if not self.frm.deck.currentIndex(): self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if (self.isApkg and self.exporter.includeSched and not self.exporter.did): verbatim = True # it's a verbatim apkg export, so place on desktop instead of # choosing file file = os.path.join(QDesktopServices.storageLocation( QDesktopServices.DesktopLocation), "collection.apkg") if os.path.exists(file): if not askUser( _("%s already exists on your desktop. Overwrite it?")% "collection.apkg"): return else: verbatim = False # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name) filename = os.path.join(aqt.mw.pm.base, u'{0}{1}'.format(deck_name, self.exporter.ext)) while 1: file = getSaveFile(self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError), e: showWarning(_("Couldn't save file: %s") % unicode(e)) else: os.unlink(file) self.exporter.exportInto(file) if verbatim: msg = _("A file called collection.apkg was saved on your desktop.") period = 5000 else: period = 3000 msg = ngettext("%d card exported.", "%d cards exported.", \ self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally:
def accept(self): self.exporter.includeSched = ( self.frm.includeSched.isChecked()) self.exporter.includeMedia = ( self.frm.includeMedia.isChecked()) self.exporter.includeTags = ( self.frm.includeTags.isChecked()) if not self.frm.deck.currentIndex(): self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if (self.isApkg and self.exporter.includeSched and not self.exporter.did): verbatim = True # it's a verbatim apkg export, so place on desktop instead of # choosing file file = os.path.join(QDesktopServices.storageLocation( QDesktopServices.DesktopLocation), "collection.apkg") if os.path.exists(file): if not askUser( _("%s already exists on your desktop. Overwrite it?")% "collection.apkg"): return else: verbatim = False while 1: file = getSaveFile( self, _("Export"), "export", self.exporter.key, self.exporter.ext) if not file: return if checkInvalidFilename(file, dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError), e: showWarning(_("Couldn't save file: %s") % unicode(e)) else: os.unlink(file) self.exporter.exportInto(file) if verbatim: msg = _("A file called collection.apkg was saved on your desktop.") period = 5000 else: period = 3000 msg = ngettext("%d card exported.", "%d cards exported.", \ self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally:
def export_deck(did): # inspired by aqt.exporting.ExportDialog.accept exporter = AnkiPackageExporter(mw.col) exporter.includeSched = False exporter.includeMedia = gc("include media that starts with underscore" ) # get _styles_for_syntax_highlighting.css exporter.includeTags = False exporter.includeHTML = False exporter.did = did now = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) while 1: file = getSaveFile( mw, # parent _("Export"), # windowtitle "debug_export_all_notetypes", # dir_description - used to remember last user choice for dir exporter.key, # key exporter.ext, # ext 'export_all_notetypes__%s.apkg' % now) # filename if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break if file: mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: mw.progress.update(label=ngettext( "Exported %d media file", "Exported %d media files", cnt) % cnt ) addHook("exportedMediaFiles", exportedMedia) exporter.exportInto(file) remHook("exportedMediaFiles", exportedMedia) tooltip("Exporting finished", 3000) finally: mw.progress.finish()
def profileNameOk(self, str): return not checkInvalidFilename(str)
def accept(self) -> None: self.exporter.includeSched = self.frm.includeSched.isChecked() self.exporter.includeMedia = self.frm.includeMedia.isChecked() self.exporter.includeTags = self.frm.includeTags.isChecked() self.exporter.includeHTML = self.frm.includeHTML.isChecked() idx = self.frm.deck.currentIndex() if self.cids is not None: # Browser Selection self.exporter.cids = self.cids self.exporter.did = None elif idx == 0: # All decks self.exporter.did = None self.exporter.cids = None else: # Deck idx-1 in the list of decks self.exporter.cids = None name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if self.isVerbatim: name = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) deck_name = tr(TR.EXPORTING_COLLECTION) + name else: # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', "_", deck_name) filename = "{0}{1}".format(deck_name, self.exporter.ext) if callable(self.exporter.key): key_str = self.exporter.key(self.col) else: key_str = self.exporter.key while 1: file = getSaveFile( self, tr(TR.ACTIONS_EXPORT), "export", key_str, self.exporter.ext, fname=filename, ) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue if os.path.commonprefix([self.mw.pm.base, file]) == self.mw.pm.base: showWarning("Please choose a different export location.") continue break self.hide() if file: # check we can write to file try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(tr(TR.EXPORTING_COULDNT_SAVE_FILE, val=str(e))) else: os.unlink(file) # progress handler def exported_media(cnt) -> None: self.mw.taskman.run_on_main(lambda: self.mw.progress.update( label=tr(TR.EXPORTING_EXPORTED_MEDIA_FILE, count=cnt))) def do_export() -> None: self.exporter.exportInto(file) def on_done(future: Future) -> None: self.mw.progress.finish() hooks.media_files_did_export.remove(exported_media) # raises if exporter failed future.result() self.on_export_finished() self.mw.progress.start() hooks.media_files_did_export.append(exported_media) self.mw.taskman.run_in_background(do_export, on_done)
def profileNameOk(self, str): from anki.utils import invalidFilename, invalidFilenameChars return not checkInvalidFilename(str)
def accept(self): self.exporter.includeSched = (self.frm.includeSched.isChecked()) self.exporter.includeMedia = (self.frm.includeMedia.isChecked()) self.exporter.includeTags = (self.frm.includeTags.isChecked()) if not self.frm.deck.currentIndex(): #position 0 means: all decks. self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if self.isVerbatim: name = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) deck_name = _("collection") + name else: # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name) if not self.isVerbatim and self.isApkg and self.exporter.includeSched and self.col.schedVer( ) == 2: showInfo( "Please switch to the regular scheduler before exporting a single deck .apkg with scheduling." ) return filename = '{0}{1}'.format(deck_name, self.exporter.ext) while 1: file = getSaveFile(self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: self.mw.progress.update( label=ngettext("Exported %d media file", "Exported %d media files", cnt) % cnt) addHook("exportedMediaFiles", exportedMedia) self.exporter.exportInto(file) remHook("exportedMediaFiles", exportedMedia) period = 3000 if self.isVerbatim: msg = _("Collection exported.") else: if self.isTextNote: msg = ngettext( "%d note exported.", "%d notes exported.", self.exporter.count) % self.exporter.count else: msg = ngettext( "%d card exported.", "%d cards exported.", self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally: self.mw.progress.finish() QDialog.accept(self)
def accept(self): self.exporter.includeSched = ( self.frm.includeSched.isChecked()) self.exporter.includeMedia = ( self.frm.includeMedia.isChecked()) self.exporter.includeTags = ( self.frm.includeTags.isChecked()) self.exporter.includeHTML = ( self.frm.includeHTML.isChecked()) if not self.frm.deck.currentIndex(): self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if self.isVerbatim: name = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) deck_name = _("collection")+name else: # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name) if not self.isVerbatim and self.isApkg and self.exporter.includeSched and self.col.schedVer() == 2: showInfo("Please switch to the regular scheduler before exporting a single deck .apkg with scheduling.") return filename = '{0}{1}'.format(deck_name, self.exporter.ext) while 1: file = getSaveFile(self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: self.mw.progress.update( label=ngettext("Exported %d media file", "Exported %d media files", cnt) % cnt ) addHook("exportedMediaFiles", exportedMedia) self.exporter.exportInto(file) remHook("exportedMediaFiles", exportedMedia) period = 3000 if self.isVerbatim: msg = _("Collection exported.") else: if self.isTextNote: msg = ngettext("%d note exported.", "%d notes exported.", self.exporter.count) % self.exporter.count else: msg = ngettext("%d card exported.", "%d cards exported.", self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally: self.mw.progress.finish() QDialog.accept(self)
def accept(self): self.exporter.includeSched = ( self.frm.includeSched.isChecked()) self.exporter.includeMedia = ( self.frm.includeMedia.isChecked()) self.exporter.includeTags = ( self.frm.includeTags.isChecked()) if not self.frm.deck.currentIndex(): self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if (self.isApkg and self.exporter.includeSched and not self.exporter.did): verbatim = True # it's a verbatim apkg export, so place on desktop instead of # choosing file; use homedir if no desktop usingHomedir = False file = os.path.join(QStandardPaths.writableLocation( QStandardPaths.DesktopLocation), "collection.apkg") if not os.path.exists(os.path.dirname(file)): usingHomedir = True file = os.path.join(QStandardPaths.writableLocation( QStandardPaths.HomeLocation), "collection.apkg") if os.path.exists(file): if usingHomedir: question = _("%s already exists in your home directory. Overwrite it?") else: question = _("%s already exists on your desktop. Overwrite it?") if not askUser(question % "collection.apkg"): return else: verbatim = False # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name) filename = '{0}{1}'.format(deck_name, self.exporter.ext) while 1: file = getSaveFile(self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: self.mw.progress.update( label=ngettext("Exported %d media file", "Exported %d media files", cnt) % cnt ) addHook("exportedMediaFiles", exportedMedia) self.exporter.exportInto(file) remHook("exportedMediaFiles", exportedMedia) if verbatim: if usingHomedir: msg = _("A file called %s was saved in your home directory.") else: msg = _("A file called %s was saved on your desktop.") msg = msg % "collection.apkg" period = 5000 else: period = 3000 if self.isTextNote: msg = ngettext("%d note exported.", "%d notes exported.", self.exporter.count) % self.exporter.count else: msg = ngettext("%d card exported.", "%d cards exported.", self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally: self.mw.progress.finish() QDialog.accept(self)
def accept(self): self.exporter.includeSched = self.frm.includeSched.isChecked() self.exporter.includeMedia = self.frm.includeMedia.isChecked() self.exporter.includeTags = self.frm.includeTags.isChecked() self.exporter.includeHTML = self.frm.includeHTML.isChecked() idx = self.frm.deck.currentIndex() if self.cids is not None: # Browser Selection self.exporter.cids = self.cids self.exporter.did = None elif idx == 0: # All decks self.exporter.did = None self.exporter.cids = None else: # Deck idx-1 in the list of decks self.exporter.cids = None name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if self.isVerbatim: name = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) deck_name = _("collection") + name else: # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', "_", deck_name) filename = "{0}{1}".format(deck_name, self.exporter.ext) while 1: file = getSaveFile( self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename, ) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue if os.path.commonprefix([self.mw.pm.base, file]) == self.mw.pm.base: showWarning("Please choose a different export location.") continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: self.mw.progress.update( label=ngettext("Exported %d media file", "Exported %d media files", cnt) % cnt) hooks.media_files_did_export.append(exportedMedia) self.exporter.exportInto(file) hooks.media_files_did_export.remove(exportedMedia) period = 3000 if self.isVerbatim: msg = _("Collection exported.") else: if self.isTextNote: msg = (ngettext( "%d note exported.", "%d notes exported.", self.exporter.count, ) % self.exporter.count) else: msg = (ngettext( "%d card exported.", "%d cards exported.", self.exporter.count, ) % self.exporter.count) tooltip(msg, period=period) finally: self.mw.progress.finish() QDialog.accept(self)
def accept(self): self.exporter.includeSched = (self.frm.includeSched.isChecked()) self.exporter.includeMedia = (self.frm.includeMedia.isChecked()) self.exporter.includeTags = (self.frm.includeTags.isChecked()) if not self.frm.deck.currentIndex(): self.exporter.did = None else: name = self.decks[self.frm.deck.currentIndex()] self.exporter.did = self.col.decks.id(name) if (self.isApkg and self.exporter.includeSched and not self.exporter.did): verbatim = True # it's a verbatim apkg export, so place on desktop instead of # choosing file; use homedir if no desktop usingHomedir = False file = os.path.join( QDesktopServices.storageLocation( QDesktopServices.DesktopLocation), "collection.apkg") if not os.path.exists(os.path.dirname(file)): usingHomedir = True file = os.path.join( QDesktopServices.storageLocation( QDesktopServices.HomeLocation), "collection.apkg") if os.path.exists(file): if usingHomedir: question = _( "%s already exists in your home directory. Overwrite it?" ) else: question = _( "%s already exists on your desktop. Overwrite it?") if not askUser(question % "collection.apkg"): return else: verbatim = False # Get deck name and remove invalid filename characters deck_name = self.decks[self.frm.deck.currentIndex()] deck_name = re.sub('[\\\\/?<>:*|"^]', '_', deck_name) filename = os.path.join( aqt.mw.pm.base, u'{0}{1}'.format(deck_name, self.exporter.ext)) while 1: file = getSaveFile(self, _("Export"), "export", self.exporter.key, self.exporter.ext, fname=filename) if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break self.hide() if file: self.mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError), e: showWarning(_("Couldn't save file: %s") % unicode(e)) else: os.unlink(file) self.exporter.exportInto(file) if verbatim: if usingHomedir: msg = _( "A file called %s was saved in your home directory." ) else: msg = _("A file called %s was saved on your desktop.") msg = msg % "collection.apkg" period = 5000 else: period = 3000 if self.isTextNote: msg = ngettext( "%d note exported.", "%d notes exported.", self.exporter.count) % self.exporter.count else: msg = ngettext( "%d card exported.", "%d cards exported.", self.exporter.count) % self.exporter.count tooltip(msg, period=period) finally: