Example #1
0
def copyLocalMedia(src, dst):
    srcDir = src.mediaDir()
    if not srcDir:
        return
    dstDir = dst.mediaDir(create=True)
    files = os.listdir(srcDir)
    # find media references; ignore latex cache
    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 (full, fname, repl) in mediaRefs(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
Example #2
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 (fullMatch, filename, replacementString) in mediaRefs(string):
            if fullMatch.startswith("["):
                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(re.escape(fullMatch), "", string)
            else:
                string = re.sub(re.escape(fullMatch),
                                '<img src="%(f)s">' % {'f': filename}, string)
        if getattr(self, "_disableMedia", None):
            return string
        self.played = toPlay
        at = AudioThread(toPlay=toPlay)
        at.start()
        return string
Example #3
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 (fullMatch, filename, replacementString) in mediaRefs(string):
         if fullMatch.startswith("["):
             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(re.escape(fullMatch), "", string)
         else:
             string = re.sub(
                 re.escape(fullMatch), '<img src="%(f)s">' %
                 {'f': filename}, string)
     if getattr(self, "_disableMedia", None):
         return string
     self.played = toPlay
     at = AudioThread(toPlay=toPlay)
     at.start()
     return string
Example #4
0
 def prepareMedia(self, string, auto=True):
     for (fullMatch, filename, replacementString) in mediaRefs(string):
         if fullMatch.startswith("["):
             if auto and (filename.lower().endswith(".mp3") or filename.lower().endswith(".wav")):
                 if not self.played and deck.mediaDir():
                     subprocess.Popen([config.get("PLAY_COMMAND"), os.path.join(deck.mediaDir(), filename)])
                     self.played = True
             string = re.sub(re.escape(fullMatch), "", string)
         else:
             pass
             # string = re.sub(re.escape(fullMatch), '''
             # <img src="%(f)s">''' % {'f': relativeMediaPath(filename)}, string)
             # return string
     return string
Example #5
0
 def prepareMedia(self, string, auto=True):
     for (fullMatch, filename, replacementString) in mediaRefs(string):
         if fullMatch.startswith("["):
             try:
                 if (filename.lower().endswith(".mp3") or filename.lower().endswith(".wav")) and auto:
                     subprocess.Popen(["mplayer", os.path.join(self.deck.mediaDir(), filename)])
             except:
                 pass
             string = re.sub(re.escape(fullMatch), "", string)
         else:
             pass
             # string = re.sub(re.escape(fullMatch), '''
             # <img src="%(f)s">''' % {'f': relativeMediaPath(filename)}, string)
             # return string
     return string
Example #6
0
def bulkrename():
    # rename files
    deck = mw.deck
    mediaDir = deck.mediaDir()
    dirs = [mediaDir]
    renamedFiles = 0
    while 1:
        if not dirs:
            break
        dir = dirs.pop()
        for fname in os.listdir(unicode(dir)):
            path = os.path.join(dir, fname)
            if os.path.isdir(path):
                dirs.append(path)
                continue
            else:
                relpath = path[len(mediaDir)+1:]
                newrel = re.sub(fromStr, toStr, relpath)
                if relpath != newrel:
                    os.rename(os.path.join(mediaDir, relpath),
                              os.path.join(mediaDir, newrel))
                    renamedFiles += 1
    # rename fields
    modifiedFacts = {}
    updateFields = []
    for (id, fid, val) in deck.s.all(
        "select id, factId, value from fields"):
        oldval = val
        for (full, fname, repl) in mediaRefs(val):
            tmp = re.sub(fromStr, toStr, fname)
            if tmp != fname:
                val = re.sub(re.escape(full), repl % tmp, val)
        if oldval != val:
            updateFields.append({'id': id, 'val': val})
            modifiedFacts[fid] = 1
    if modifiedFacts:
        _modifyFields(deck, updateFields, modifiedFacts, True)
    ui.utils.showInfo("%d files renamed.\n%d facts modified." %
                      (renamedFiles, len(modifiedFacts)))
Example #7
0
def bulkrename():
    # rename files
    deck = mw.deck
    mediaDir = deck.mediaDir()
    dirs = [mediaDir]
    renamedFiles = 0
    while 1:
        if not dirs:
            break
        dir = dirs.pop()
        for fname in os.listdir(unicode(dir)):
            path = os.path.join(dir, fname)
            if os.path.isdir(path):
                dirs.append(path)
                continue
            else:
                relpath = path[len(mediaDir) + 1:]
                newrel = re.sub(fromStr, toStr, relpath)
                if relpath != newrel:
                    os.rename(os.path.join(mediaDir, relpath),
                              os.path.join(mediaDir, newrel))
                    renamedFiles += 1
    # rename fields
    modifiedFacts = {}
    updateFields = []
    for (id, fid, val) in deck.s.all("select id, factId, value from fields"):
        oldval = val
        for (full, fname, repl) in mediaRefs(val):
            tmp = re.sub(fromStr, toStr, fname)
            if tmp != fname:
                val = re.sub(re.escape(full), repl % tmp, val)
        if oldval != val:
            updateFields.append({'id': id, 'val': val})
            modifiedFacts[fid] = 1
    if modifiedFacts:
        _modifyFields(deck, updateFields, modifiedFacts, True)
    ui.utils.showInfo("%d files renamed.\n%d facts modified." %
                      (renamedFiles, len(modifiedFacts)))