Esempio n. 1
0
    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))
Esempio n. 2
0
 def run(self):
     u = Upgrader()
     # check
     if not u.check(self.file):
         self.log.append(_(
             "File is old or damaged; please run Tools>Advanced>Check DB "
             "in Anki 1.2 first."))
         raise Exception("invalidFile")
     # upgrade
     try:
         deck = u.upgrade(self.file)
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = self.file.replace(".anki", ".media")
     self.deckPrefix = os.path.basename(self.file).replace(".anki", "")
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Esempio n. 3
0
    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))
Esempio n. 4
0
 def run(self):
     u = Upgrader()
     # check
     if not u.check(self.file):
         self.log.append(_(
             "File is old or damaged; please run Tools>Advanced>Check DB "
             "in Anki 1.2 first."))
         raise Exception("invalidFile")
     # upgrade
     try:
         deck = u.upgrade(self.file)
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media2",  self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Esempio n. 5
0
 def run(self):
     u = Upgrader()
     # check
     res = u.check(self.file)
     if res == "invalid":
         self.log.append(_(
             "File is invalid. Please restore from backup."))
         raise Exception("invalidFile")
     # upgrade
     if res != "ok":
         self.log.append(
             "Problems fixed during upgrade:\n***\n%s\n***\n" % res)
     try:
         deck = u.upgrade()
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media",  self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Esempio n. 6
0
 def run(self):
     u = Upgrader()
     # check
     res = u.check(self.file)
     if res == "invalid":
         self.log.append(_("File is invalid. Please restore from backup."))
         raise Exception("invalidFile")
     # upgrade
     if res != "ok":
         self.log.append("Problems fixed during upgrade:\n***\n%s\n***\n" %
                         res)
     try:
         deck = u.upgrade()
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media", self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Esempio n. 7
0
 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))
Esempio n. 8
0
 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))
Esempio n. 9
0
 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))
Esempio n. 10
0
File: apkg.py Progetto: exic/libanki
 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))