Ejemplo n.º 1
0
 def rebuildQA(self, deck, media=True):
     # format qa
     d = {}
     for f in self.fact.model.fieldModels:
         d[f.name] = (f.id, self.fact[f.name])
     qa = formatQA(None, self.fact.modelId, d, self.splitTags(),
                   self.cardModel, deck)
     # find old media references
     files = {}
     for type in ("question", "answer"):
         for f in mediaFiles(getattr(self, type) or ""):
             if f in files:
                 files[f] -= 1
             else:
                 files[f] = -1
     # update q/a
     self.question = qa['question']
     self.answer = qa['answer']
     # determine media delta
     for type in ("question", "answer"):
         for f in mediaFiles(getattr(self, type)):
             if f in files:
                 files[f] += 1
             else:
                 files[f] = 1
     # update media counts if we're attached to deck
     # if media:
     #     for (f, cnt) in files.items():
     #         updateMediaCount(deck, f, cnt)
     self.setModified()
 def rebuildQA(self, deck, media=True):
     # format qa
     d = {}
     for f in self.fact.model.fieldModels:
         d[f.name] = (f.id, self.fact[f.name])
     qa = formatQA(None, self.fact.modelId, d, self.splitTags(),
                   self.cardModel, deck)
     # find old media references
     files = {}
     for type in ("question", "answer"):
         for f in mediaFiles(getattr(self, type) or ""):
             if f in files:
                 files[f] -= 1
             else:
                 files[f] = -1
     # update q/a
     self.question = qa['question']
     self.answer = qa['answer']
     # determine media delta
     for type in ("question", "answer"):
         for f in mediaFiles(getattr(self, type)):
             if f in files:
                 files[f] += 1
             else:
                 files[f] = 1
     # update media counts if we're attached to deck
     # if media:
     #     for (f, cnt) in files.items():
     #         updateMediaCount(deck, f, cnt)
     self.setModified()
Ejemplo n.º 3
0
def copyLocalMedia(src, dst):
    srcDir = src.mediaDir()
    if not srcDir:
        return
    dstDir = dst.mediaDir(create=True)
    files = os.listdir(srcDir)
    # find media references
    used = {}
    for col in ("question", "answer"):
        txt = dst.s.column0("""
select %(c)s from cards where
%(c)s like '%%<img %%'
or %(c)s like '%%[sound:%%'""" % {'c': col})
        for entry in txt:
            for fname in mediaFiles(entry):
                used[fname] = True
    # copy only used media
    for file in files:
        if file not in used:
            continue
        srcfile = os.path.join(srcDir, file)
        dstfile = os.path.join(dstDir, file)
        if not os.path.exists(dstfile):
            try:
                shutil.copy2(srcfile, dstfile)
            except IOError, OSError:
                pass
Ejemplo n.º 4
0
def copyLocalMedia(src, dst):
    srcDir = src.mediaDir()
    if not srcDir:
        return
    dstDir = dst.mediaDir(create=True)
    files = os.listdir(srcDir)
    # find media references
    used = {}
    for col in ("question", "answer"):
        txt = dst.s.column0(
            """
select %(c)s from cards where
%(c)s like '%%<img %%'
or %(c)s like '%%[sound:%%'"""
            % {"c": col}
        )
        for entry in txt:
            for fname in mediaFiles(entry):
                used[fname] = True
    # copy only used media
    for file in files:
        if file not in used:
            continue
        srcfile = os.path.join(srcDir, file)
        dstfile = os.path.join(dstDir, file)
        if not os.path.exists(dstfile):
            try:
                shutil.copy2(srcfile, dstfile)
            except IOError, OSError:
                pass