mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
rename()
This commit is contained in:
parent
47c30f172f
commit
0d6064b933
3 changed files with 34 additions and 3 deletions
15
anki/deck.py
15
anki/deck.py
|
@ -2,7 +2,7 @@
|
|||
# Copyright: Damien Elmes <anki@ichi2.net>
|
||||
# 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.utils import parseTags, ids2str, hexifyID, \
|
||||
checksum, fieldChecksum, addTags, delTags, stripHTML, intTime, \
|
||||
|
@ -178,6 +178,19 @@ qconf=?, conf=?, data=?""",
|
|||
self.sched.onClose()
|
||||
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
|
||||
##########################################################################
|
||||
|
||||
|
|
|
@ -3,19 +3,20 @@
|
|||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
import os, shutil, re, urllib, urllib2, time, unicodedata, \
|
||||
urllib, sys
|
||||
urllib, sys, shutil
|
||||
from anki.utils import checksum, intTime, namedtmp, isWin
|
||||
from anki.lang import _
|
||||
|
||||
class MediaRegistry(object):
|
||||
|
||||
# can be altered at the class level for dropbox, etc
|
||||
mediaPrefix = ""
|
||||
# other code depends on this order, so don't reorder
|
||||
regexps = ("(?i)(\[sound:([^]]+)\])",
|
||||
"(?i)(<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>)")
|
||||
|
||||
def __init__(self, deck):
|
||||
self.deck = deck
|
||||
self.mediaPrefix = ""
|
||||
self._dir = None
|
||||
self._updateDir()
|
||||
|
||||
|
@ -48,6 +49,14 @@ class MediaRegistry(object):
|
|||
os.chdir(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
|
||||
##########################################################################
|
||||
|
||||
|
|
|
@ -59,3 +59,12 @@ def test_deckIntegration():
|
|||
ret = d.media.check()
|
||||
assert ret[0] == ["fake2.png"]
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue