revert to old style file-only latex handling

This commit is contained in:
Damien Elmes 2009-07-08 12:20:56 +09:00
parent 0011489220
commit 6f27ae52cc
2 changed files with 21 additions and 31 deletions

View file

@ -8,9 +8,8 @@ Latex support
"""
__docformat__ = 'restructuredtext'
import re, tempfile, os, sys, subprocess, stat, time
import re, tempfile, os, sys, subprocess, stat, time, shutil
from anki.utils import genID, checksum
from anki.media import copyToMedia
from htmlentitydefs import entitydefs
latexPreamble = ("\\documentclass[12pt]{article}\n"
@ -77,8 +76,7 @@ def call(argv, wait=True, **kwargs):
def latexImgFile(deck, latexCode):
key = checksum(latexCode)
return deck.s.scalar("select filename from media where originalPath = :k",
k=key)
return "latex-%s.png" % key
def latexImgPath(deck, file):
"Return the path to the cache file in system encoding format."
@ -137,8 +135,9 @@ def buildImg(deck, latex):
stdout=log, stderr=log, startupinfo=si):
return (False, errmsg)
# add to media
path = copyToMedia(deck, "tmp.png", latex=checksum(latex))
return (True, path)
target = latexImgPath(deck, latexImgFile(deck, latex))
shutil.copy2("tmp.png", target)
return (True, target)
finally:
os.chdir(oldcwd)

View file

@ -51,15 +51,11 @@ def mediaFilename(path):
ext = os.path.splitext(path)[1].lower()
return "%s%s" % (new, ext)
def copyToMedia(deck, path, latex=None):
def copyToMedia(deck, path):
"""Copy PATH to MEDIADIR, and return new filename.
Update media table. If file already exists, don't copy."""
if latex:
origPath = latex
description = "latex"
else:
origPath = path
description = os.path.splitext(os.path.basename(path))[0]
origPath = path
description = os.path.splitext(os.path.basename(path))[0]
newBase = mediaFilename(path)
new = os.path.join(deck.mediaDir(create=True), newBase)
# copy if not existing
@ -73,17 +69,14 @@ Update media table. If file already exists, don't copy."""
if not deck.s.scalar(
"select 1 from media where filename = :f",
f=newBase):
if description != "latex":
# if the user has modified a hashed file, try to remember the old
# filename
old = deck.s.scalar(
"select originalPath from media where filename = :s",
s=os.path.basename(origPath))
if old:
origPath = old
description = os.path.splitext(os.path.basename(origPath))[0]
print "orig", old
print "desc", description
# if the user has modified a hashed file, try to remember the old
# filename
old = deck.s.scalar(
"select originalPath from media where filename = :s",
s=os.path.basename(origPath))
if old:
origPath = old
description = os.path.splitext(os.path.basename(origPath))[0]
try:
path = unicode(path, sys.getfilesystemencoding())
except TypeError:
@ -150,6 +143,8 @@ def rebuildMediaDir(deck, deleteRefs=False, dirty=True):
for c, oldBase in enumerate(files):
if mod and not c % mod:
deck.updateProgress()
if oldBase.startswith("latex-"):
continue
oldPath = os.path.join(deck.mediaDir(), oldBase)
if oldBase.startswith("."):
continue
@ -203,14 +198,12 @@ def rebuildMediaDir(deck, deleteRefs=False, dirty=True):
# build cache of db records
deck.updateProgress(_("Delete unused files..."))
mediaIds = dict(deck.s.all("select filename, id from media"))
# assume latex files exist
for f in deck.s.column0(
"select filename from media where description = 'latex'"):
usedFiles[f] = 1
# look through the media dir for any unused files, and delete
for f in os.listdir(unicode(deck.mediaDir())):
if f.startswith("."):
continue
if f.startswith("latex-"):
continue
path = os.path.join(deck.mediaDir(), f)
if os.path.isdir(path):
shutil.rmtree(path)
@ -282,9 +275,7 @@ def exportOriginalFiles(deck):
except (IOError, OSError):
pass
cnt = 0
for row in deck.s.all("""
select filename, originalPath from media
where description != 'latex'"""):
for row in deck.s.all("select filename, originalPath from media"):
(fname, path) = row
base = os.path.basename(path)
if base == fname: