def main(): # opens the collection log.info('open collection: %s', options.file) cwd = os.getcwd() col = anki.Collection(path=options.file) work_dir = os.getcwd() os.chdir(cwd) kd = KanjiDamage(col, log) # should update kanji damage deck? if options.reset_kd: kd.reset(options.kd_file) # finds the kanji damage deck and model if not kd.get_deck(): sys.exit('{0}: error: {1}'.format( sys.argv[0], 'couldn\'t find KanjiDamage[ Reordered] deck in the collection, try using option -r to import it' )) kd_model = kd.get_model() if not kd_model: sys.exit('{0}: error: {1}'.format( sys.argv[0], 'couldn\'t find KanjiDamage model in the collection, try using option -r to import it' )) # updates kanji damage deck if options.update_kd: log.info('updating %s...', kd_model['name']) # removes media files if options.force_download: shutil.rmtree(os.path.join(col.media.dir(), 'assets'), ignore_errors=True) shutil.rmtree(os.path.join(col.media.dir(), 'visualaids'), ignore_errors=True) kd.update() # recreates the kanji damage words deck _, kdw_deck = kdw_create(col, kd) log.info('writing output file %s...', options.output) exporter = AnkiPackageExporter(col) exporter.includeSched = False exporter.includeMedia = True exporter.includeTags = True exporter.did = kdw_deck['id'] out_path = os.path.join(os.getcwd(), options.output) os.chdir(work_dir) exporter.exportInto(out_path) log.info('all is well!') col.close()
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()