mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
refactor deck/backup location
This commit is contained in:
parent
cc2952f28b
commit
420fd4ec77
1 changed files with 21 additions and 12 deletions
33
anki/deck.py
33
anki/deck.py
|
@ -1526,20 +1526,30 @@ mapper(Deck, decksTable, properties={
|
||||||
# Deck storage
|
# Deck storage
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
numBackups = 100
|
||||||
|
|
||||||
|
# anki dir
|
||||||
|
if sys.platform.startswith("darwin"):
|
||||||
|
ankiDir = os.path.expanduser("~/Library/Application Support/Anki")
|
||||||
|
else:
|
||||||
|
ankiDir = os.path.expanduser("~/.anki/")
|
||||||
|
newDeckDir = ankiDir
|
||||||
|
if not os.path.exists(ankiDir):
|
||||||
|
os.mkdir(ankiDir)
|
||||||
|
# backup
|
||||||
|
backupDir = os.path.join(ankiDir, "backups")
|
||||||
|
if not os.path.exists(backupDir):
|
||||||
|
os.mkdir(backupDir)
|
||||||
|
|
||||||
class DeckStorage(object):
|
class DeckStorage(object):
|
||||||
|
|
||||||
backupDir = os.path.expanduser("~/.anki/backups")
|
|
||||||
numBackups = 100
|
|
||||||
newDeckDir = "~/.anki/"
|
|
||||||
|
|
||||||
def newDeckPath():
|
def newDeckPath():
|
||||||
# create ~/mydeck(N).anki
|
|
||||||
n = 2
|
n = 2
|
||||||
path = os.path.expanduser(
|
path = os.path.expanduser(
|
||||||
os.path.join(DeckStorage.newDeckDir, "mydeck.anki"))
|
os.path.join(newDeckDir, "mydeck.anki"))
|
||||||
while os.path.exists(path):
|
while os.path.exists(path):
|
||||||
path = os.path.expanduser(
|
path = os.path.expanduser(
|
||||||
os.path.join(DeckStorage.newDeckDir, "mydeck%d.anki") % n)
|
os.path.join(newDeckDir, "mydeck%d.anki") % n)
|
||||||
n += 1
|
n += 1
|
||||||
return path
|
return path
|
||||||
newDeckPath = staticmethod(newDeckPath)
|
newDeckPath = staticmethod(newDeckPath)
|
||||||
|
@ -1921,18 +1931,17 @@ where interval < 1""")
|
||||||
def backup(modified, path):
|
def backup(modified, path):
|
||||||
# need a non-unicode path
|
# need a non-unicode path
|
||||||
path = path.encode(sys.getfilesystemencoding())
|
path = path.encode(sys.getfilesystemencoding())
|
||||||
backupDir = DeckStorage.backupDir.encode(sys.getfilesystemencoding())
|
bdir = backupDir.encode(sys.getfilesystemencoding())
|
||||||
numBackups = DeckStorage.numBackups
|
|
||||||
def backupName(path, num):
|
def backupName(path, num):
|
||||||
path = os.path.abspath(path)
|
path = os.path.abspath(path)
|
||||||
path = path.replace("\\", "!")
|
path = path.replace("\\", "!")
|
||||||
path = path.replace("/", "!")
|
path = path.replace("/", "!")
|
||||||
path = path.replace(":", "")
|
path = path.replace(":", "")
|
||||||
path = os.path.join(backupDir, path)
|
path = os.path.join(bdir, path)
|
||||||
path = re.sub("\.anki$", ".backup-%d.anki" % num, path)
|
path = re.sub("\.anki$", ".backup-%d.anki" % num, path)
|
||||||
return path
|
return path
|
||||||
if not os.path.exists(backupDir):
|
if not os.path.exists(bdir):
|
||||||
os.makedirs(backupDir)
|
os.makedirs(bdir)
|
||||||
# if the mod time is identical, don't make a new backup
|
# if the mod time is identical, don't make a new backup
|
||||||
firstBack = backupName(path, 0)
|
firstBack = backupName(path, 0)
|
||||||
if os.path.exists(firstBack):
|
if os.path.exists(firstBack):
|
||||||
|
|
Loading…
Reference in a new issue