def run(self): # extract the deck from the zip file self.zip = z = zipfile.ZipFile(self.file) # v2 scheduler? try: z.getinfo("collection.anki21") suffix = ".anki21" except KeyError: suffix = ".anki2" col = z.read("collection" + suffix) colpath = tmpfile(suffix=suffix) with open(colpath, "wb") as f: f.write(col) self.file = colpath # we need the media dict in advance, and we'll need a map of fname -> # number to use during the import self.nameToNum = {} dir = self.col.media.dir() for k, v in list(json.loads(z.read("media").decode("utf8")).items()): path = os.path.abspath(os.path.join(dir, v)) if os.path.commonprefix([path, dir]) != dir: raise Exception("Invalid file") self.nameToNum[unicodedata.normalize("NFC", v)] = k # run anki2 importer Anki2Importer.run(self) # import static media for file, c in list(self.nameToNum.items()): if not file.startswith("_") and not file.startswith("latex-"): continue path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): with open(path, "wb") as f: f.write(z.read(c))
def run(self): # extract the deck from the zip file self.zip = z = zipfile.ZipFile(self.file) # v2 scheduler? try: z.getinfo("collection.anki21") suffix = ".anki21" except KeyError: suffix = ".anki2" col = z.read("collection"+suffix) colpath = tmpfile(suffix=suffix) with open(colpath, "wb") as f: f.write(col) self.file = colpath # we need the media dict in advance, and we'll need a map of fname -> # number to use during the import self.nameToNum = {} dir = self.col.media.dir() for k, v in list(json.loads(z.read("media").decode("utf8")).items()): path = os.path.abspath(os.path.join(dir, v)) if os.path.commonprefix([path, dir]) != dir: raise Exception("Invalid file") self.nameToNum[unicodedata.normalize("NFC",v)] = k # run anki2 importer Anki2Importer.run(self) # import static media for file, c in list(self.nameToNum.items()): if not file.startswith("_") and not file.startswith("latex-"): continue path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): with open(path, "wb") as f: f.write(z.read(c))
def download(url): src = urllib.urlopen(url) dst = tmpfile() with open(dst, "wb") as f: f.write(src.read()) return dst
def run(self): # extract the deck from the zip file z = zipfile.ZipFile(self.file) col = z.read("collection.anki2") colpath = tmpfile(suffix=".anki2") open(colpath, "wb").write(col) # pass it to the anki2 importer self.file = colpath Anki2Importer.run(self) # import media media = simplejson.loads(z.read("media")) for c, file in media.items(): path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): open(path, "wb").write(z.read(c))
def run(self): # extract the deck from the zip file z = zipfile.ZipFile(self.file) col = z.read("collection.anki2") colpath = tmpfile(suffix=".anki2") open(colpath, "wb").write(col) # pass it to the anki2 importer self.file = colpath Anki2Importer.run(self) # import media media = json.loads(z.read("media")) for c, file in media.items(): path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): open(path, "wb").write(z.read(c))
def check(self, path): "Returns 'ok', 'invalid', or log of fixes applied." # copy into a temp file before we open self.tmppath = tmpfile(suffix=".anki2") shutil.copy(path, self.tmppath) # run initial check with DB(self.tmppath) as db: res = self._check(db) # needs fixing? if res not in ("ok", "invalid"): res = self._fix(self.tmppath) # don't allow .upgrade() if invalid if res == "invalid": os.unlink(self.tmppath) self.tmppath = None return res
def run(self): # extract the deck from the zip file self.zip = z = zipfile.ZipFile(self.file) col = z.read("collection.anki2") colpath = tmpfile(suffix=".anki2") open(colpath, "wb").write(col) self.file = colpath # we need the media dict in advance, and we'll need a map of fname -> # number to use during the import self.nameToNum = {} for k, v in json.loads(z.read("media")).items(): self.nameToNum[v] = k # run anki2 importer Anki2Importer.run(self) # import static media for file, c in self.nameToNum.items(): if not file.startswith("_") and not file.startswith("latex-"): continue path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): open(path, "wb").write(z.read(c))
def run(self): # extract the deck from the zip file self.zip = z = zipfile.ZipFile(self.file) col = z.read("collection.anki2") colpath = tmpfile(suffix=".anki2") open(colpath, "wb").write(col) self.file = colpath # we need the media dict in advance, and we'll need a map of fname -> # number to use during the import self.nameToNum = {} for k, v in json.loads(z.read("media")).items(): self.nameToNum[v] = k # run anki2 importer Anki2Importer.run(self) # import static media for file, c in self.nameToNum.items(): if not file.startswith("_"): continue path = os.path.join(self.col.media.dir(), file) if not os.path.exists(path): open(path, "wb").write(z.read(c))
# --- Imports: --- from anki.hooks import addHook, wrap from anki.lang import _ from anki.utils import call, checksum, stripHTML, tmpfile from aqt import mw from aqt.qt import * from aqt.utils import getOnlyText, showInfo from htmlentitydefs import entitydefs import cgi, os, re, shutil # --- Globals: --- # http://abcnotation.com/ abcFile = tmpfile("abc", ".abc") midFile = tmpfile("abc", ".mid") wavFile = tmpfile("abc", ".wav") mp3File = tmpfile("abc", ".mp3") abc2midiCmd = ["abc2midi", abcFile, "-o", midFile] timidityCmd = [ "timidity", midFile, "--output-24bit", "-A120", "-Ow", "-o", wavFile ] lameCmd = ["lame", wavFile, "-b64", mp3File] abcPattern = "%ANKI%" abcSplit = "%%%" abcTemplate = u"""X:1 M:4/4 L:1/4 K:C %s
def _openDB(self, path): self.tmppath = tmpfile(suffix=".anki2") shutil.copy(path, self.tmppath) self.db = DB(self.tmppath)
# --- Imports: --- from anki.hooks import addHook, wrap from anki.lang import _ from anki.utils import call, checksum, stripHTML, tmpfile from aqt import mw from aqt.qt import * from aqt.utils import getOnlyText, showInfo from htmlentitydefs import entitydefs import cgi, os, re, shutil # --- Globals: --- # http://lilypond.org/doc/v2.14/Documentation/usage/lilypond-output-in-other-programs#inserting-lilypond-output-into-other-programs lilypondFile = tmpfile("lilypond", ".ly") lilypondCmd = [ "lilypond", "-dbackend=eps", "-dno-gs-load-fonts", "-dinclude-eps-fonts", "--o", lilypondFile, "--png", lilypondFile ] lilypondPattern = "%ANKI%" lilypondSplit = "%%%" lilypondTemplate = u""" \\paper{ indent=0\\mm line-width=120\\mm oddFooterMarkup=##f oddHeaderMarkup=##f bookTitleMarkup = ##f scoreTitleMarkup = ##f }
def _openDB(self, path): self.tmppath = tmpfile(suffix=".anki2") shutil.copy(path, self._utf8(self.tmppath)) self.db = DB(self.tmppath)
from anki.utils import checksum from anki.utils import stripHTML from anki.utils import tmpfile from aqt import gui_hooks from aqt import mw from aqt.editor import Editor from aqt.qt import * from aqt.utils import getOnlyText from aqt.utils import showInfo # --- Globals: --- # Load configuration options _config = mw.addonManager.getConfig(__name__) TEMP_FILE = tmpfile("lilypond", ".ly") LILYPOND_CMD = [ _config['executable'], ] + _config['command_line_params'] + [ "--o", TEMP_FILE, TEMP_FILE, ] OUTPUT_FILE_EXT = _config["output_file_ext"] DEFAULT_TEMPLATE = _config['default_template'] USER_FILES_DIR = os.path.join(mw.pm.addonFolder(), __name__, "user_files") # Template directory # TODO Extract these to config file? LILYPOND_PATTERN = "%ANKI%" # Substitution targets in templates LILYPOND_SPLIT = "%%%" # LilyPond code section delimiter
# --- Imports: --- from anki.hooks import addHook, wrap from anki.lang import _ from anki.utils import call, checksum, stripHTML, tmpfile from aqt import mw from aqt.qt import * from aqt.utils import getOnlyText, showInfo from htmlentitydefs import entitydefs import cgi, os, re, shutil # --- Globals: --- # http://lilypond.org/doc/v2.14/Documentation/usage/lilypond-output-in-other-programs#inserting-lilypond-output-into-other-programs lilypondFile = tmpfile("lilypond", ".ly") lilypondCmd = ["lilypond", "-dbackend=eps", "-dno-gs-load-fonts", "-dinclude-eps-fonts", "--o", lilypondFile, "--png", lilypondFile] lilypondPattern = "%ANKI%" lilypondSplit = "%%%" lilypondTemplate = u""" \\paper{ indent=0\\mm line-width=120\\mm oddFooterMarkup=##f oddHeaderMarkup=##f bookTitleMarkup = ##f scoreTitleMarkup = ##f } \\relative c'' { %s } """ % (lilypondPattern,)
# --- Imports: --- from anki.hooks import addHook, wrap from anki.lang import _ from anki.utils import call, checksum, stripHTML, tmpfile from aqt import mw from aqt.qt import * from aqt.utils import getOnlyText, showInfo from htmlentitydefs import entitydefs import cgi, os, re, shutil # --- Globals: --- # http://abcnotation.com/ abcFile = tmpfile("abc", ".abc") midFile = tmpfile("abc", ".mid") wavFile = tmpfile("abc", ".wav") mp3File = tmpfile("abc", ".mp3") abc2midiCmd = ["abc2midi", abcFile, "-o", midFile] timidityCmd = ["timidity", midFile, "--output-24bit", "-A120", "-Ow", "-o", wavFile] lameCmd = ["lame", wavFile, "-b64", mp3File] abcPattern = "%ANKI%" abcSplit = "%%%" abcTemplate = u"""X:1 M:4/4 L:1/4 K:C %s """ % (abcPattern,) abcTemplates = {}