~ubuntu-branches/ubuntu/lucid/anki/lucid-updates

« back to all changes in this revision

Viewing changes to libanki/anki/latex.py

  • Committer: Bazaar Package Importer
  • Author(s): Mackenzie Morgan
  • Date: 2010-05-31 15:55:50 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100531155550-wj3tag8bvp6fwhpo
Tags: 0.9.9.8.6-2~lucid1
Backport from maverick to fix FTBFS (LP: #550145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
"""
9
9
__docformat__ = 'restructuredtext'
10
10
 
11
 
import re, tempfile, os, sys, subprocess, stat, time
 
11
import re, tempfile, os, sys, subprocess, stat, time, shutil
12
12
from anki.utils import genID, checksum
13
 
from anki.media import copyToMedia
14
13
from htmlentitydefs import entitydefs
15
14
 
16
15
latexPreamble = ("\\documentclass[12pt]{article}\n"
77
76
 
78
77
def latexImgFile(deck, latexCode):
79
78
    key = checksum(latexCode)
80
 
    return deck.s.scalar("select filename from media where originalPath = :k",
81
 
                         k=key)
82
 
 
83
 
def latexImgPath(deck, file):
84
 
    "Return the path to the cache file in system encoding format."
85
 
    path = os.path.join(deck.mediaDir(create=True), file)
86
 
    return path.encode(sys.getfilesystemencoding())
 
79
    return "latex-%s.png" % key
87
80
 
88
81
def mungeLatex(latex):
89
82
    "Convert entities, fix newlines, and convert to utf8."
95
88
    return latex
96
89
 
97
90
def deleteAllLatexImages(deck):
98
 
    for f in deck.s.column0(
99
 
        "select filename from media where description = 'latex'"):
100
 
        path = latexImgPath(deck, f)
101
 
        try:
102
 
            os.unlink(path)
103
 
        except (OSError, IOError):
104
 
            pass
105
 
    deck.s.statement("delete from media where description = 'latex'")
106
 
    deck.flushMod()
 
91
    mdir = deck.mediaDir()
 
92
    if not mdir:
 
93
        return
 
94
    deck.startProgress()
 
95
    for c, f in enumerate(os.listdir(mdir)):
 
96
        if f.startswith("latex-"):
 
97
            os.unlink(os.path.join(mdir, f))
 
98
        if c % 100 == 0:
 
99
            deck.updateProgress()
 
100
    deck.finishProgress()
107
101
 
108
102
def cacheAllLatexImages(deck):
 
103
    deck.startProgress()
109
104
    fields = deck.s.column0("select value from fields")
110
 
    for field in fields:
 
105
    for c, field in enumerate(fields):
 
106
        if c % 10 == 0:
 
107
            deck.updateProgress()
111
108
        renderLatex(deck, field)
 
109
    deck.finishProgress()
112
110
 
113
111
def buildImg(deck, latex):
114
112
    log = open(os.path.join(tmpdir, "latex_log.txt"), "w+")
119
117
    texfile.write(latexPostamble + "\n")
120
118
    texfile.close()
121
119
    texpath = texpath.encode(sys.getfilesystemencoding())
 
120
    # make sure we have a valid mediaDir
 
121
    deck.mediaDir(create=True)
122
122
    oldcwd = os.getcwd()
123
123
    if sys.platform == "win32":
124
124
        si = subprocess.STARTUPINFO()
127
127
        si = None
128
128
    try:
129
129
        os.chdir(tmpdir)
130
 
        errmsg = _(
131
 
            "Error executing 'latex' or 'dvipng'.\n"
 
130
        errmsg = _("Error executing %s.\n") + _(
132
131
            "A log file is available here:\n%s") % tmpdir
133
132
        if call(["latex", "-interaction=nonstopmode",
134
133
                 texpath], stdout=log, stderr=log, startupinfo=si):
135
 
            return (False, errmsg)
 
134
            return (False, errmsg % "latex")
136
135
        if call(latexDviPngCmd + ["tmp.dvi", "-o", "tmp.png"],
137
136
                stdout=log, stderr=log, startupinfo=si):
138
 
            return (False, errmsg)
 
137
            return (False, errmsg % "dvipng")
139
138
        # add to media
140
 
        path = copyToMedia(deck, "tmp.png", latex=checksum(latex))
141
 
        return (True, path)
 
139
        target = latexImgFile(deck, latex)
 
140
        shutil.copy2("tmp.png", os.path.join(deck.mediaDir(), target))
 
141
        return (True, target)
142
142
    finally:
143
143
        os.chdir(oldcwd)
144
144
 
145
145
def imageForLatex(deck, latex, build=True):
146
146
    "Return an image that represents 'latex', building if necessary."
147
147
    imageFile = latexImgFile(deck, latex)
148
 
    if imageFile:
149
 
        path = latexImgPath(deck, imageFile)
150
148
    ok = True
151
 
    if build and (not imageFile or not os.path.exists(path)):
 
149
    if build and (not imageFile or not os.path.exists(imageFile)):
152
150
        (ok, imageFile) = buildImg(deck, latex)
153
151
    if not ok:
154
152
        return (False, imageFile)