This commit is contained in:
Damien Elmes 2011-04-24 10:01:03 +09:00
parent 47c30f172f
commit 0d6064b933
3 changed files with 34 additions and 3 deletions

View file

@ -2,7 +2,7 @@
# Copyright: Damien Elmes <anki@ichi2.net> # Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import time, os, random, re, stat, simplejson, datetime, copy import time, os, random, re, stat, simplejson, datetime, copy, shutil
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.utils import parseTags, ids2str, hexifyID, \ from anki.utils import parseTags, ids2str, hexifyID, \
checksum, fieldChecksum, addTags, delTags, stripHTML, intTime, \ checksum, fieldChecksum, addTags, delTags, stripHTML, intTime, \
@ -178,6 +178,19 @@ qconf=?, conf=?, data=?""",
self.sched.onClose() self.sched.onClose()
self.dty = False self.dty = False
def rename(self, path):
# close our DB connection
self.close()
# move to new path
shutil.copy2(self.path, path)
os.unlink(self.path)
# record old dir
olddir = self.media.dir()
# reconnect & move media
self.path = path
self.reopen()
self.media.move(olddir)
# Object creation helpers # Object creation helpers
########################################################################## ##########################################################################

View file

@ -3,19 +3,20 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import os, shutil, re, urllib, urllib2, time, unicodedata, \ import os, shutil, re, urllib, urllib2, time, unicodedata, \
urllib, sys urllib, sys, shutil
from anki.utils import checksum, intTime, namedtmp, isWin from anki.utils import checksum, intTime, namedtmp, isWin
from anki.lang import _ from anki.lang import _
class MediaRegistry(object): class MediaRegistry(object):
# can be altered at the class level for dropbox, etc
mediaPrefix = ""
# other code depends on this order, so don't reorder # other code depends on this order, so don't reorder
regexps = ("(?i)(\[sound:([^]]+)\])", regexps = ("(?i)(\[sound:([^]]+)\])",
"(?i)(<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)") "(?i)(<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)")
def __init__(self, deck): def __init__(self, deck):
self.deck = deck self.deck = deck
self.mediaPrefix = ""
self._dir = None self._dir = None
self._updateDir() self._updateDir()
@ -48,6 +49,14 @@ class MediaRegistry(object):
os.chdir(dir) os.chdir(dir)
self._dir = dir self._dir = dir
def move(self, old):
if not old:
return
self._dir = None
new = self.dir(create=None)
shutil.copytree(old, new)
shutil.rmtree(old)
# Adding media # Adding media
########################################################################## ##########################################################################

View file

@ -59,3 +59,12 @@ def test_deckIntegration():
ret = d.media.check() ret = d.media.check()
assert ret[0] == ["fake2.png"] assert ret[0] == ["fake2.png"]
assert ret[1] == ["foo.jpg"] assert ret[1] == ["foo.jpg"]
def test_rename():
d = getEmptyDeck()
# put a file into it
file = unicode(os.path.join(testDir, "support/fake.png"))
d.media.addFile(file)
new = d.path.replace(".anki", "2.anki")
d.rename(new)