예제 #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()
예제 #2
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()
예제 #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
예제 #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
예제 #5
0
 def prepareMedia(self, string, auto=True):
     class AudioThread(threading.Thread):
         def __init__(self, *args, **kwargs):
             self.toPlay = kwargs['toPlay']
             del kwargs['toPlay']
             threading.Thread.__init__(self, *args, **kwargs)
         def run(self):
             for f in self.toPlay:
                 subprocess.Popen([config.get('PLAY_COMMAND'), f]).wait()
     toPlay = []
     for filename in mediaFiles(string):
         if auto and (filename.lower().endswith(".mp3") or
                      filename.lower().endswith(".wav")):
             if deck.mediaDir():
                 toPlay.append(os.path.join(deck.mediaDir(), filename))
     string = re.sub("(?i)(\[sound:([^]]+)\])", "", string)
     if getattr(self, "_disableMedia", None):
         return string
     self.played = toPlay
     at = AudioThread(toPlay=toPlay)
     at.start()
     return string