示例#1
0
文件: apkg.py 项目: bbugyi200/anki
    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))
示例#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)
示例#3
0
文件: apkg.py 项目: solarmist/anki
    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))
示例#4
0
文件: anki1.py 项目: won21kr/libanki
 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)
示例#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)
示例#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)
示例#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))
示例#8
0
文件: apkg.py 项目: won21kr/libanki
 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))
示例#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))
示例#10
0
文件: apkg.py 项目: 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))