some filelocator caching fixes for ls-R and recursivedir
[PyX.git] / pyx / text.py
blob3776562d36516199c15b8cabf5db9424eedd18bd
1 # Copyright (C) 2002-2013 Jörg Lehmann <joergl@users.sourceforge.net>
2 # Copyright (C) 2003-2011 Michael Schindler <m-schindler@users.sourceforge.net>
3 # Copyright (C) 2002-2013 André Wobst <wobsta@users.sourceforge.net>
5 # This file is part of PyX (http://pyx.sourceforge.net/).
7 # PyX is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # PyX is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with PyX; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import atexit, errno, functools, glob, inspect, io, itertools, logging, os
22 import queue, re, shutil, sys, tempfile, textwrap, threading
24 from pyx import config, unit, box, baseclasses, trafo, version, attr, style, path
25 from pyx import bbox as bboxmodule
26 from pyx.dvi import dvifile
28 logger = logging.getLogger("pyx")
31 def indent_text(text):
32 "Prepends two spaces to each line in text."
33 return "".join(" " + line for line in text.splitlines(True))
36 def remove_string(p, s):
37 """Removes a string from a string.
39 The function removes the first occurrence of a string in another string.
41 :param str p: string to be removed
42 :param str s: string to be searched
43 :returns: tuple of the result string and a success boolean (``True`` when
44 the string was removed)
45 :rtype: tuple of str and bool
47 Example:
48 >>> remove_string("XXX", "abcXXXdefXXXghi")
49 ('abcdefXXXghi', True)
51 """
52 r = s.replace(p, '', 1)
53 return r, r != s
56 def remove_pattern(p, s, ignore_nl=True):
57 r"""Removes a pattern from a string.
59 The function removes the first occurence of the pattern from a string. It
60 returns a tuple of the resulting string and the matching object (or
61 ``None``, if the pattern did not match).
63 :param re.regex p: pattern to be removed
64 :param str s: string to be searched
65 :param bool ignore_nl: When ``True``, newlines in the string are ignored
66 during the pattern search. The returned string will still contain all
67 newline characters outside of the matching part of the string, whereas
68 the returned matching object will not contain the newline characters
69 inside of the matching part of the string.
70 :returns: the result string and the match object or ``None`` if
71 search failed
72 :rtype: tuple of str and (re.match or None)
74 Example:
75 >>> r, m = remove_pattern(re.compile("XXX"), 'ab\ncXX\nXdefXX\nX')
76 >>> r
77 'ab\ncdefXX\nX'
78 >>> m.string[m.start():m.end()]
79 'XXX'
81 """
82 if ignore_nl:
83 r = s.replace('\n', '')
84 has_nl = r != s
85 else:
86 r = s
87 has_nl = False
88 m = p.search(r)
89 if m:
90 s_start = r_start = m.start()
91 s_end = r_end = m.end()
92 if has_nl:
93 j = 0
94 for c in s:
95 if c == '\n':
96 if j < r_end:
97 s_end += 1
98 if j <= r_start:
99 s_start += 1
100 else:
101 j += 1
102 return s[:s_start] + s[s_end:], m
103 return s, None
106 def index_all(c, s):
107 """Return list of positions of a character in a string.
109 Example:
110 >>> index_all("X", "abXcdXef")
111 [2, 5]
114 assert len(c) == 1
115 return [i for i, x in enumerate(s) if x == c]
118 def pairwise(i):
119 """Returns iterator over pairs of data from an iterable.
121 Example:
122 >>> list(pairwise([1, 2, 3]))
123 [(1, 2), (2, 3)]
126 a, b = itertools.tee(i)
127 next(b, None)
128 return zip(a, b)
131 def remove_nested_brackets(s, openbracket="(", closebracket=")", quote='"'):
132 """Remove nested brackets
134 Return a modified string with all nested brackets 1 removed, i.e. only
135 keep the first bracket nesting level. In case an opening bracket is
136 immediately followed by a quote, the quoted string is left untouched,
137 even if it contains brackets. The use-case for that are files in the
138 folder "Program Files (x86)".
140 If the bracket nesting level is broken (unbalanced), the unmodified
141 string is returned.
143 Example:
144 >>> remove_nested_brackets('aaa("bb()bb" cc(dd(ee))ff)ggg'*2)
145 'aaa("bb()bb" ccff)gggaaa("bb()bb" ccff)ggg'
148 openpos = index_all(openbracket, s)
149 closepos = index_all(closebracket, s)
150 if quote is not None:
151 quotepos = index_all(quote, s)
152 for openquote, closequote in pairwise(quotepos):
153 if openquote-1 in openpos:
154 # ignore brackets in quoted string
155 openpos = [pos for pos in openpos
156 if not (openquote < pos < closequote)]
157 closepos = [pos for pos in closepos
158 if not (openquote < pos < closequote)]
159 if len(openpos) != len(closepos):
160 # unbalanced brackets
161 return s
163 # keep the original string in case we need to return due to broken nesting levels
164 r = s
166 level = 0
167 # Iterate over the bracket positions from the end.
168 # We go reversely to be able to immediately remove nested bracket levels
169 # without influencing bracket positions yet to come in the loop.
170 for pos, leveldelta in sorted(itertools.chain(zip(openpos, itertools.repeat(-1)),
171 zip(closepos, itertools.repeat(1))),
172 reverse=True):
173 # the current bracket nesting level
174 level += leveldelta
175 if level < 0:
176 # unbalanced brackets
177 return s
178 if leveldelta == 1 and level == 2:
179 # a closing bracket to cut after
180 endpos = pos+1
181 if leveldelta == -1 and level == 1:
182 # an opening bracket to cut at -> remove
183 r = r[:pos] + r[endpos:]
184 return r
187 class TexResultError(ValueError):
188 "Error raised by :class:`texmessage` parsers."
189 pass
192 class texmessage:
193 """Collection of TeX output parsers.
195 This class is not meant to be instanciated. Instead, it serves as a
196 namespace for TeX output parsers, which are functions receiving a TeX
197 output and returning parsed output.
199 In addition, this class also contains some generator functions (namely
200 :attr:`texmessage.no_file` and :attr:`texmessage.pattern`), which return a
201 function according to the given parameters. They are used to generate some
202 of the parsers in this class and can be used to create others as well.
205 start_pattern = re.compile(r"This is [-0-9a-zA-Z\s_]*TeX")
207 @staticmethod
208 def start(msg):
209 r"""Validate TeX/LaTeX startup message including scrollmode test.
211 Example:
212 >>> texmessage.start(r'''
213 ... This is e-TeX (version)
214 ... *! Undefined control sequence.
215 ... <*> \raiseerror
216 ... %
217 ... ''', 0)
221 # check for "This is e-TeX" etc.
222 if not texmessage.start_pattern.search(msg):
223 raise TexResultError("TeX startup failed")
225 # check for \raiseerror -- just to be sure that communication works
226 new = msg.split("*! Undefined control sequence.\n<*> \\raiseerror\n %\n", 1)[-1]
227 if msg == new:
228 raise TexResultError("TeX scrollmode check failed")
229 return new
231 @staticmethod
232 def no_file(fileending, qualname=None):
233 "Generator function to ignore the missing file message for fileending."
234 def check(msg):
235 "Ignore the missing {} file message."
236 return msg.replace("No file texput.%s." % fileending, "").replace("No file %s%stexput.%s." % (os.curdir, os.sep, fileending), "")
237 check.__doc__ = check.__doc__.format(fileending)
238 if qualname is not None:
239 check.__qualname__ = qualname
240 return check
242 no_aux = staticmethod(no_file.__func__("aux", "texmessage.no_aux"))
243 no_nav = staticmethod(no_file.__func__("nav", "texmessage.no_nav"))
245 aux_pattern = re.compile(r'\(([^()]+\.aux|"[^"]+\.aux")\)')
246 log_pattern = re.compile(r"Transcript written on .*texput\.log\.", re.DOTALL)
248 @staticmethod
249 def end(msg):
250 "Validate TeX shutdown message."
251 msg = re.sub(texmessage.aux_pattern, "", msg).replace("(see the transcript file for additional information)", "")
253 # check for "Transcript written on ...log."
254 msg, m = remove_pattern(texmessage.log_pattern, msg)
255 if not m:
256 raise TexResultError("TeX logfile message expected")
257 return msg
259 quoted_file_pattern = re.compile(r'\("(?P<filename>[^"]+)".*?\)')
260 file_pattern = re.compile(r'\((?P<filename>[^"][^ )]*).*?\)', re.DOTALL)
262 @staticmethod
263 def load(msg):
264 """Ignore file loading messages.
266 Removes text starting with a round bracket followed by a filename
267 ignoring all further text until the corresponding closing bracket.
268 Quotes and/or line breaks in the filename are handled as needed for TeX
269 output.
271 Without quoting the filename, the necessary removal of line breaks is
272 not well defined and the different possibilities are tested to check
273 whether one solution is ok. The last of the examples below checks this
274 behavior.
276 Examples:
277 >>> texmessage.load(r'''other (text.py) things''', 0)
278 'other things'
279 >>> texmessage.load(r'''other ("text.py") things''', 0)
280 'other things'
281 >>> texmessage.load(r'''other ("tex
282 ... t.py" further (ignored)
283 ... text) things''', 0)
284 'other things'
285 >>> texmessage.load(r'''other (t
286 ... ext
287 ... .py
288 ... fur
289 ... ther (ignored) text) things''', 0)
290 'other things'
293 r = remove_nested_brackets(msg)
294 r, m = remove_pattern(texmessage.quoted_file_pattern, r)
295 while m:
296 if not os.path.isfile(m.group("filename")):
297 return msg
298 r, m = remove_pattern(texmessage.quoted_file_pattern, r)
299 r, m = remove_pattern(texmessage.file_pattern, r, ignore_nl=False)
300 while m:
301 for filename in itertools.accumulate(m.group("filename").split("\n")):
302 if os.path.isfile(filename):
303 break
304 else:
305 return msg
306 r, m = remove_pattern(texmessage.file_pattern, r, ignore_nl=False)
307 return r
309 quoted_def_pattern = re.compile(r'\("(?P<filename>[^"]+\.(fd|def))"\)')
310 def_pattern = re.compile(r'\((?P<filename>[^"][^ )]*\.(fd|def))\)')
312 @staticmethod
313 def load_def(msg):
314 "Ignore font definition (``*.fd`` and ``*.def``) loading messages."
315 r = msg
316 for p in [texmessage.quoted_def_pattern, texmessage.def_pattern]:
317 r, m = remove_pattern(p, r)
318 while m:
319 if not os.path.isfile(m.group("filename")):
320 return msg
321 r, m = remove_pattern(texmessage.quoted_file_pattern, r)
322 return r
324 quoted_graphics_pattern = re.compile(r'<"(?P<filename>[^"]+\.eps)">')
325 graphics_pattern = re.compile(r'<(?P<filename>[^"][^>]*\.eps)>')
327 @staticmethod
328 def load_graphics(msg):
329 "Ignore graphics file (``*.eps``) loading messages."
330 r = msg
331 for p in [texmessage.quoted_graphics_pattern, texmessage.graphics_pattern]:
332 r, m = remove_pattern(p, r)
333 while m:
334 if not os.path.isfile(m.group("filename")):
335 return msg
336 r, m = remove_pattern(texmessage.quoted_file_pattern, r)
337 return r
339 @staticmethod
340 def ignore(msg):
341 """Ignore all messages.
343 Should be used as a last resort only. You should write a proper TeX
344 output parser function for the output you observe.
347 return ""
349 @staticmethod
350 def warn(msg):
351 """Warn about all messages.
353 Similar to :attr:`ignore`, but writing a warning to the logger about
354 the TeX output. This is considered to be better when you need to get it
355 working quickly as you will still be prompted about the unresolved
356 output, while the processing continues.
359 if msg:
360 logger.warning("ignoring TeX warnings:\n%s" % indent_text(msg.rstrip()))
361 return ""
363 @staticmethod
364 def pattern(p, warning, qualname=None):
365 "Warn by regular expression pattern matching."
366 def check(msg):
367 "Warn about {}."
368 msg, m = remove_pattern(p, msg, ignore_nl=False)
369 while m:
370 logger.warning("ignoring %s:\n%s" % (warning, m.string[m.start(): m.end()].rstrip()))
371 msg, m = remove_pattern(p, msg, ignore_nl=False)
372 return msg
373 check.__doc__ = check.__doc__.format(warning)
374 if qualname is not None:
375 check.__qualname__ = qualname
376 return check
378 box_warning = staticmethod(pattern.__func__(re.compile(r"^(Overfull|Underfull) \\[hv]box.*$(\n^..*$)*\n^$\n", re.MULTILINE),
379 "overfull/underfull box", qualname="texmessage.box_warning"))
380 font_warning = staticmethod(pattern.__func__(re.compile(r"^LaTeX Font Warning: .*$(\n^\(Font\).*$)*", re.MULTILINE),
381 "font substitutions of NFSS", qualname="texmessage.font_warning"))
382 package_warning = staticmethod(pattern.__func__(re.compile(r"^package\s+(?P<packagename>\S+)\s+warning\s*:[^\n]+(?:\n\(?(?P=packagename)\)?[^\n]*)*", re.MULTILINE | re.IGNORECASE),
383 "generic package messages", qualname="texmessage.package_warning"))
384 rerun_warning = staticmethod(pattern.__func__(re.compile(r"^(LaTeX Warning: Label\(s\) may have changed\. Rerun to get cross-references right\s*\.)$", re.MULTILINE),
385 "rerun required message", qualname="texmessage.rerun_warning"))
386 nobbl_warning = staticmethod(pattern.__func__(re.compile(r"^[\s\*]*(No file .*\.bbl.)\s*", re.MULTILINE),
387 "no-bbl message", qualname="texmessage.nobbl_warning"))
390 ###############################################################################
391 # textattrs
392 ###############################################################################
394 _textattrspreamble = ""
396 class textattr:
397 "a textattr defines a apply method, which modifies a (La)TeX expression"
399 class _localattr: pass
401 _textattrspreamble += r"""\gdef\PyXFlushHAlign{0}%
402 \def\PyXragged{%
403 \leftskip=0pt plus \PyXFlushHAlign fil%
404 \rightskip=0pt plus 1fil%
405 \advance\rightskip0pt plus -\PyXFlushHAlign fil%
406 \parfillskip=0pt%
407 \pretolerance=9999%
408 \tolerance=9999%
409 \parindent=0pt%
410 \hyphenpenalty=9999%
411 \exhyphenpenalty=9999}%
414 class boxhalign(attr.exclusiveattr, textattr, _localattr):
416 def __init__(self, aboxhalign):
417 self.boxhalign = aboxhalign
418 attr.exclusiveattr.__init__(self, boxhalign)
420 def apply(self, expr):
421 return r"\gdef\PyXBoxHAlign{%.5f}%s" % (self.boxhalign, expr)
423 boxhalign.left = boxhalign(0)
424 boxhalign.center = boxhalign(0.5)
425 boxhalign.right = boxhalign(1)
426 # boxhalign.clear = attr.clearclass(boxhalign) # we can't defined a clearclass for boxhalign since it can't clear a halign's boxhalign
429 class flushhalign(attr.exclusiveattr, textattr, _localattr):
431 def __init__(self, aflushhalign):
432 self.flushhalign = aflushhalign
433 attr.exclusiveattr.__init__(self, flushhalign)
435 def apply(self, expr):
436 return r"\gdef\PyXFlushHAlign{%.5f}\PyXragged{}%s" % (self.flushhalign, expr)
438 flushhalign.left = flushhalign(0)
439 flushhalign.center = flushhalign(0.5)
440 flushhalign.right = flushhalign(1)
441 # flushhalign.clear = attr.clearclass(flushhalign) # we can't defined a clearclass for flushhalign since it couldn't clear a halign's flushhalign
444 class halign(boxhalign, flushhalign, _localattr):
446 def __init__(self, aboxhalign, aflushhalign):
447 self.boxhalign = aboxhalign
448 self.flushhalign = aflushhalign
449 attr.exclusiveattr.__init__(self, halign)
451 def apply(self, expr):
452 return r"\gdef\PyXBoxHAlign{%.5f}\gdef\PyXFlushHAlign{%.5f}\PyXragged{}%s" % (self.boxhalign, self.flushhalign, expr)
454 halign.left = halign(0, 0)
455 halign.center = halign(0.5, 0.5)
456 halign.right = halign(1, 1)
457 halign.clear = attr.clearclass(halign)
458 halign.boxleft = boxhalign.left
459 halign.boxcenter = boxhalign.center
460 halign.boxright = boxhalign.right
461 halign.flushleft = halign.raggedright = flushhalign.left
462 halign.flushcenter = halign.raggedcenter = flushhalign.center
463 halign.flushright = halign.raggedleft = flushhalign.right
466 class _mathmode(attr.attr, textattr, _localattr):
467 "math mode"
469 def apply(self, expr):
470 return r"$\displaystyle{%s}$" % expr
472 mathmode = _mathmode()
473 clearmathmode = attr.clearclass(_mathmode)
476 class _phantom(attr.attr, textattr, _localattr):
477 "phantom text"
479 def apply(self, expr):
480 return r"\phantom{%s}" % expr
482 phantom = _phantom()
483 clearphantom = attr.clearclass(_phantom)
486 _textattrspreamble += "\\newbox\\PyXBoxVBox%\n\\newdimen\\PyXDimenVBox%\n"
488 class parbox_pt(attr.sortbeforeexclusiveattr, textattr):
490 top = 1
491 middle = 2
492 bottom = 3
494 def __init__(self, width, baseline=top):
495 self.width = width * 72.27 / (unit.scale["x"] * 72)
496 self.baseline = baseline
497 attr.sortbeforeexclusiveattr.__init__(self, parbox_pt, [_localattr])
499 def apply(self, expr):
500 if self.baseline == self.top:
501 return r"\linewidth=%.5ftruept\vtop{\hsize=\linewidth\textwidth=\linewidth{}%s}" % (self.width, expr)
502 elif self.baseline == self.middle:
503 return r"\linewidth=%.5ftruept\setbox\PyXBoxVBox=\hbox{{\vtop{\hsize=\linewidth\textwidth=\linewidth{}%s}}}\PyXDimenVBox=0.5\dp\PyXBoxVBox\setbox\PyXBoxVBox=\hbox{{\vbox{\hsize=\linewidth\textwidth=\linewidth{}%s}}}\advance\PyXDimenVBox by -0.5\dp\PyXBoxVBox\lower\PyXDimenVBox\box\PyXBoxVBox" % (self.width, expr, expr)
504 elif self.baseline == self.bottom:
505 return r"\linewidth=%.5ftruept\vbox{\hsize=\linewidth\textwidth=\linewidth{}%s}" % (self.width, expr)
506 else:
507 ValueError("invalid baseline argument")
509 parbox_pt.clear = attr.clearclass(parbox_pt)
511 class parbox(parbox_pt):
513 def __init__(self, width, **kwargs):
514 parbox_pt.__init__(self, unit.topt(width), **kwargs)
516 parbox.clear = parbox_pt.clear
519 _textattrspreamble += "\\newbox\\PyXBoxVAlign%\n\\newdimen\\PyXDimenVAlign%\n"
521 class valign(attr.sortbeforeexclusiveattr, textattr):
523 def __init__(self, avalign):
524 self.valign = avalign
525 attr.sortbeforeexclusiveattr.__init__(self, valign, [parbox_pt, _localattr])
527 def apply(self, expr):
528 return r"\setbox\PyXBoxVAlign=\hbox{{%s}}\PyXDimenVAlign=%.5f\ht\PyXBoxVAlign\advance\PyXDimenVAlign by -%.5f\dp\PyXBoxVAlign\lower\PyXDimenVAlign\box\PyXBoxVAlign" % (expr, 1-self.valign, self.valign)
530 valign.top = valign(0)
531 valign.middle = valign(0.5)
532 valign.bottom = valign(1)
533 valign.clear = valign.baseline = attr.clearclass(valign)
536 _textattrspreamble += "\\newdimen\\PyXDimenVShift%\n"
538 class _vshift(attr.sortbeforeattr, textattr):
540 def __init__(self):
541 attr.sortbeforeattr.__init__(self, [valign, parbox_pt, _localattr])
543 def apply(self, expr):
544 return r"%s\setbox0\hbox{{%s}}\lower\PyXDimenVShift\box0" % (self.setheightexpr(), expr)
546 class vshift(_vshift):
547 "vertical down shift by a fraction of a character height"
549 def __init__(self, lowerratio, heightstr="0"):
550 _vshift.__init__(self)
551 self.lowerratio = lowerratio
552 self.heightstr = heightstr
554 def setheightexpr(self):
555 return r"\setbox0\hbox{{%s}}\PyXDimenVShift=%.5f\ht0" % (self.heightstr, self.lowerratio)
557 class _vshiftmathaxis(_vshift):
558 "vertical down shift by the height of the math axis"
560 def setheightexpr(self):
561 return r"\setbox0\hbox{$\vcenter{\vrule width0pt}$}\PyXDimenVShift=\ht0"
564 vshift.bottomzero = vshift(0)
565 vshift.middlezero = vshift(0.5)
566 vshift.topzero = vshift(1)
567 vshift.mathaxis = _vshiftmathaxis()
568 vshift.clear = attr.clearclass(_vshift)
571 defaultsizelist = ["normalsize", "large", "Large", "LARGE", "huge", "Huge",
572 None, "tiny", "scriptsize", "footnotesize", "small"]
574 class size(attr.sortbeforeattr, textattr):
575 "font size"
577 def __init__(self, sizeindex=None, sizename=None, sizelist=defaultsizelist):
578 if (sizeindex is None and sizename is None) or (sizeindex is not None and sizename is not None):
579 raise ValueError("either specify sizeindex or sizename")
580 attr.sortbeforeattr.__init__(self, [_mathmode, _vshift])
581 if sizeindex is not None:
582 if sizeindex >= 0 and sizeindex < sizelist.index(None):
583 self.size = sizelist[sizeindex]
584 elif sizeindex < 0 and sizeindex + len(sizelist) > sizelist.index(None):
585 self.size = sizelist[sizeindex]
586 else:
587 raise IndexError("index out of sizelist range")
588 else:
589 self.size = sizename
591 def apply(self, expr):
592 return r"\%s{}%s" % (self.size, expr)
594 size.tiny = size(-4)
595 size.scriptsize = size.script = size(-3)
596 size.footnotesize = size.footnote = size(-2)
597 size.small = size(-1)
598 size.normalsize = size.normal = size(0)
599 size.large = size(1)
600 size.Large = size(2)
601 size.LARGE = size(3)
602 size.huge = size(4)
603 size.Huge = size(5)
604 size.clear = attr.clearclass(size)
607 ###############################################################################
608 # texrunner
609 ###############################################################################
612 class MonitorOutput(threading.Thread):
614 def __init__(self, name, output):
615 """Deadlock-safe output stream reader and monitor.
617 This method sets up a thread to continously read lines from a stream.
618 By that a deadlock due to a full pipe is prevented. In addition, the
619 stream content can be monitored for containing a certain string (see
620 :meth:`expect` and :meth:`wait`) and return all the collected output
621 (see :meth:`read`).
623 :param string name: name to be used while logging in :meth:`wait` and
624 :meth:`done`
625 :param file output: output stream
628 self.output = output
629 self._expect = queue.Queue(1)
630 self._received = threading.Event()
631 self._output = queue.Queue()
632 threading.Thread.__init__(self, name=name)
633 self.daemon = True
634 self.start()
636 def expect(self, s):
637 """Expect a string on a **single** line in the output.
639 This method must be called **before** the output occurs, i.e. before
640 the input is written to the TeX/LaTeX process.
642 :param s: expected string or ``None`` if output is expected to become
643 empty
644 :type s: str or None
647 self._expect.put_nowait(s)
649 def read(self):
650 """Read all output collected since its previous call.
652 The output reading should be synchronized by the :meth:`expect`
653 and :meth:`wait` methods.
655 :returns: collected output from the stream
656 :rtype: str
659 l = []
660 try:
661 while True:
662 l.append(self._output.get_nowait())
663 except queue.Empty:
664 pass
665 return "".join(l).replace("\r\n", "\n").replace("\r", "\n")
667 def _wait(self, waiter, checker):
668 """Helper method to implement :meth:`wait` and :meth:`done`.
670 Waits for an event using the *waiter* and *checker* functions while
671 providing user feedback to the ``pyx``-logger using the warning level
672 according to the ``wait`` and ``showwait`` from the ``text`` section of
673 the pyx :mod:`config`.
675 :param function waiter: callback to wait for (the function gets called
676 with a timeout parameter)
677 :param function checker: callback returing ``True`` if
678 waiting was successful
679 :returns: ``True`` when wait was successful
680 :rtype: bool
683 wait = config.getint("text", "wait", 60)
684 showwait = config.getint("text", "showwait", 5)
685 if showwait:
686 waited = 0
687 hasevent = False
688 while waited < wait and not hasevent:
689 if wait - waited > showwait:
690 waiter(showwait)
691 waited += showwait
692 else:
693 waiter(wait - waited)
694 waited += wait - waited
695 hasevent = checker()
696 if not hasevent:
697 if waited < wait:
698 logger.warning("Still waiting for {} "
699 "after {} (of {}) seconds..."
700 .format(self.name, waited, wait))
701 else:
702 logger.warning("The timeout of {} seconds expired "
703 "and {} did not respond."
704 .format(waited, self.name))
705 return hasevent
706 else:
707 waiter(wait)
708 return checker()
710 def wait(self):
711 """Wait for the expected output to happen.
713 Waits either until a line containing the string set by the previous
714 :meth:`expect` call is found, or a timeout occurs.
716 :returns: ``True`` when the expected string was found
717 :rtype: bool
720 r = self._wait(self._received.wait, self._received.isSet)
721 if r:
722 self._received.clear()
723 return r
725 def done(self):
726 """Waits until the output becomes empty.
728 Waits either until the output becomes empty, or a timeout occurs.
729 The generated output can still be catched by :meth:`read` after
730 :meth:`done` was successful.
732 In the proper workflow :meth:`expect` should be called with ``None``
733 before the output completes, as otherwise a ``ValueError`` is raised
734 in the :meth:`run`.
736 :returns: ``True`` when the output has become empty
737 :rtype: bool
740 return self._wait(self.join, lambda self=self: not self.is_alive())
742 def _readline(self):
743 """Read a line from the output.
745 To be used **inside** the thread routine only.
747 :returns: one line of the output as a string
748 :rtype: str
751 while True:
752 try:
753 return self.output.readline()
754 except IOError as e:
755 if e.errno != errno.EINTR:
756 raise
758 def run(self):
759 """Thread routine.
761 **Not** to be called from outside.
763 :raises ValueError: output becomes empty while some string is expected
766 expect = None
767 while True:
768 line = self._readline()
769 if expect is None:
770 try:
771 expect = self._expect.get_nowait()
772 except queue.Empty:
773 pass
774 if not line:
775 break
776 self._output.put(line)
777 if expect is not None:
778 found = line.find(expect)
779 if found != -1:
780 self._received.set()
781 expect = None
782 self.output.close()
783 if expect is not None:
784 raise ValueError("{} finished unexpectedly".format(self.name))
787 class textbox(box.rect, baseclasses.canvasitem):
788 """basically a box.rect, but it contains a text created by the texrunner
789 - texrunner._text and texrunner.text return such an object
790 - _textbox instances can be inserted into a canvas
791 - the output is contained in a page of the dvifile available thru the texrunner"""
792 # TODO: shouldn't all boxes become canvases? how about inserts then?
794 def __init__(self, x, y, left, right, height, depth, do_finish, fontmap, singlecharmode, attrs):
796 - do_finish is a method to be called to get the dvicanvas
797 (e.g. the do_finish calls the setdvicanvas method)
798 - attrs are fillstyles"""
799 self.left = left
800 self.right = right
801 self.width = left + right
802 self.height = height
803 self.depth = depth
804 self.do_finish = do_finish
805 self.fontmap = fontmap
806 self.singlecharmode = singlecharmode
807 self.attrs = attrs
809 self.texttrafo = trafo.scale(unit.scale["x"]).translated(x, y)
810 box.rect.__init__(self, x - left, y - depth, left + right, depth + height, abscenter = (left, depth))
812 self.dvicanvas = None
813 self.insertdvicanvas = False
815 def transform(self, *trafos):
816 box.rect.transform(self, *trafos)
817 for trafo in trafos:
818 self.texttrafo = trafo * self.texttrafo
819 if self.dvicanvas:
820 for trafo in trafos:
821 self.dvicanvas.trafo = trafo * self.dvicanvas.trafo
823 def readdvipage(self, dvifile, page):
824 self.dvicanvas = dvifile.readpage([ord("P"), ord("y"), ord("X"), page, 0, 0, 0, 0, 0, 0],
825 fontmap=self.fontmap, singlecharmode=self.singlecharmode, attrs=[self.texttrafo] + self.attrs)
827 def marker(self, marker):
828 self.do_finish()
829 return self.texttrafo.apply(*self.dvicanvas.markers[marker])
831 def textpath(self):
832 self.do_finish()
833 textpath = path.path()
834 for item in self.dvicanvas.items:
835 textpath += item.textpath()
836 return textpath.transformed(self.texttrafo)
838 def processPS(self, file, writer, context, registry, bbox):
839 self.do_finish()
840 abbox = bboxmodule.empty()
841 self.dvicanvas.processPS(file, writer, context, registry, abbox)
842 bbox += box.rect.bbox(self)
844 def processPDF(self, file, writer, context, registry, bbox):
845 self.do_finish()
846 abbox = bboxmodule.empty()
847 self.dvicanvas.processPDF(file, writer, context, registry, abbox)
848 bbox += box.rect.bbox(self)
851 class _marker:
852 pass
855 class errordetail:
856 "Constants defining the verbosity of the :exc:`TexResultError`."
857 none = 0 #: Without any input and output.
858 default = 1 #: Input and parsed output shortend to 5 lines.
859 full = 2 #: Full input and unparsed as well as parsed output.
862 class Tee(object):
864 def __init__(self, *files):
865 self.files = files
867 def write(self, data):
868 for file in self.files:
869 file.write(data)
871 def flush(self):
872 for file in self.files:
873 file.flush()
875 def close(self):
876 for file in self.files:
877 file.close()
879 # The texrunner state represents the next (or current) execute state.
880 STATE_START, STATE_PREAMBLE, STATE_TYPESET, STATE_DONE = range(4)
881 PyXBoxPattern = re.compile(r"PyXBox:page=(?P<page>\d+),lt=(?P<lt>-?\d*((\d\.?)|(\.?\d))\d*)pt,rt=(?P<rt>-?\d*((\d\.?)|(\.?\d))\d*)pt,ht=(?P<ht>-?\d*((\d\.?)|(\.?\d))\d*)pt,dp=(?P<dp>-?\d*((\d\.?)|(\.?\d))\d*)pt:")
882 dvi_pattern = re.compile(r"Output written on .*texput\.dvi \((?P<page>\d+) pages?, \d+ bytes\)\.", re.DOTALL)
884 class TexDoneError(Exception):
885 pass
888 class SingleRunner:
890 texmessages_start_default = [texmessage.start]
891 #: default :class:`texmessage` parsers for interpreter startup
892 texmessages_start_default = [texmessage.start]
893 #: default :class:`texmessage` parsers for interpreter shutdown
894 texmessages_end_default = [texmessage.end, texmessage.font_warning, texmessage.rerun_warning, texmessage.nobbl_warning]
895 #: default :class:`texmessage` parsers for preamble output
896 texmessages_preamble_default = [texmessage.load]
897 #: default :class:`texmessage` parsers for typeset output
898 texmessages_run_default = [texmessage.font_warning, texmessage.box_warning, texmessage.package_warning,
899 texmessage.load_def, texmessage.load_graphics]
901 def __init__(self, executable,
902 texenc="ascii",
903 usefiles=[],
904 texipc=config.getboolean("text", "texipc", 0),
905 copyinput=None,
906 dvitype=False,
907 errordetail=errordetail.default,
908 texmessages_start=[],
909 texmessages_end=[],
910 texmessages_preamble=[],
911 texmessages_run=[]):
912 """Base class for the TeX interface.
914 .. note:: This class cannot be used directly. It is the base class for
915 all texrunners and provides most of the implementation.
916 Still, to the end user the parameters except for *executable*
917 are important, as they are preserved in derived classes
918 usually.
920 :param str executable: command to start the TeX interpreter
921 :param str texenc: encoding to use in the communication with the TeX
922 interpreter
923 :param usefiles: list of supplementary files
924 :type usefiles: list of str
925 :param bool texipc: :ref:`texipc` flag.
926 :param copyinput: filename or file to be used to store a copy of all
927 the input passed to the TeX interpreter
928 :type copyinput: None or str or file
929 :param bool dvitype: flag to turn on dvitype-like output
930 :param errordetail: verbosity of the :exc:`TexResultError`
931 :type errordetail: :class:`errordetail`
932 :param texmessages_start: additional message parsers at interpreter
933 startup
934 :type texmessages_start: list of :class:`texmessage` parsers
935 :param texmessages_end: additional message parsers at interpreter
936 shutdown
937 :type texmessages_end: list of :class:`texmessage` parsers
938 :param texmessages_preamble: additional message parsers for preamble
939 output
940 :type texmessages_preamble: list of :class:`texmessage` parsers
941 :param texmessages_run: additional message parsers for typset output
942 :type texmessages_run: list of :class:`texmessage` parsers
944 self.executable = executable
945 self.texenc = texenc
946 self.usefiles = usefiles
947 self.texipc = texipc
948 self.copyinput = copyinput
949 self.dvitype = dvitype
950 self.errordetail = errordetail
951 self.texmessages_start = texmessages_start
952 self.texmessages_end = texmessages_end
953 self.texmessages_preamble = texmessages_preamble
954 self.texmessages_run = texmessages_run
956 self.state = STATE_START
957 self.executeid = 0
958 self.page = 0
960 self.needdvitextboxes = [] # when texipc-mode off
961 self.dvifile = None
963 self.tmpdir = None
965 def _cleanup(self):
966 """Clean-up TeX interpreter and tmp directory.
968 This funtion is hooked up in atexit to quit the TeX interpreter, to
969 save contents of usefiles, and to remove the temporary directory.
972 try:
973 if self.state > STATE_START:
974 if self.state < STATE_DONE:
975 self.do_finish()
976 if self.state < STATE_DONE: # cleanup while TeX is still running?
977 self.texoutput.expect(None)
978 self.force_done()
979 for f, msg in [(self.texinput.close, "We tried to communicate to %s to quit itself, but this seem to have failed. Trying by terminate signal now ...".format(self.name)),
980 (self.popen.terminate, "Failed, too. Trying by kill signal now ..."),
981 (self.popen.kill, "We tried very hard to get rid of the %s process, but we ultimately failed (as far as we can tell). Sorry.".format(self.name))]:
983 if self.texoutput.done():
984 break
985 logger.warning(msg)
986 for usefile in self.usefiles:
987 extpos = usefile.rfind(".")
988 try:
989 os.rename(os.path.join(self.tmpdir, "texput" + usefile[extpos:]), usefile)
990 except EnvironmentError:
991 logger.warning("Could not save '{}'.".format(usefile))
992 if os.path.isfile(usefile):
993 try:
994 os.unlink(usefile)
995 except EnvironmentError:
996 logger.warning("Failed to remove spurious file '{}'.".format(usefile))
997 finally:
998 shutil.rmtree(self.tmpdir, ignore_errors=True)
1000 def _execute(self, expr, texmessages, oldstate, newstate):
1001 """executes expr within TeX/LaTeX"""
1002 assert STATE_PREAMBLE <= oldstate <= STATE_TYPESET
1003 assert oldstate == self.state
1004 assert newstate >= oldstate
1005 if newstate == STATE_DONE:
1006 self.texoutput.expect(None)
1007 self.texinput.write(expr)
1008 else:
1009 if oldstate == newstate == STATE_TYPESET:
1010 self.page += 1
1011 expr = "\\ProcessPyXBox{%s%%\n}{%i}" % (expr, self.page)
1012 self.executeid += 1
1013 self.texoutput.expect("PyXInputMarker:executeid=%i:" % self.executeid)
1014 expr += "%%\n\\PyXInput{%i}%%\n" % self.executeid
1015 self.texinput.write(expr)
1016 self.texinput.flush()
1017 self.state = newstate
1018 if newstate == STATE_DONE:
1019 wait_ok = self.texoutput.done()
1020 else:
1021 wait_ok = self.texoutput.wait()
1022 try:
1023 parsed = unparsed = self.texoutput.read()
1024 if not wait_ok:
1025 raise TexResultError("TeX didn't respond as expected within the timeout period.")
1026 if newstate != STATE_DONE:
1027 parsed, m = remove_string("PyXInputMarker:executeid=%s:" % self.executeid, parsed)
1028 if not m:
1029 raise TexResultError("PyXInputMarker expected")
1030 if oldstate == newstate == STATE_TYPESET:
1031 parsed, m = remove_pattern(PyXBoxPattern, parsed, ignore_nl=False)
1032 if not m:
1033 raise TexResultError("PyXBox expected")
1034 if m.group("page") != str(self.page):
1035 raise TexResultError("Wrong page number in PyXBox")
1036 extent = [float(x)*72/72.27*unit.x_pt for x in m.group("lt", "rt", "ht", "dp")]
1037 parsed, m = remove_string("[80.121.88.%s]" % self.page, parsed)
1038 if not m:
1039 raise TexResultError("PyXPageOutMarker expected")
1040 else:
1041 # check for "Output written on ...dvi (1 page, 220 bytes)."
1042 if self.page:
1043 parsed, m = remove_pattern(dvi_pattern, parsed)
1044 if not m:
1045 raise TexResultError("TeX dvifile messages expected")
1046 if m.group("page") != str(self.page):
1047 raise TexResultError("wrong number of pages reported")
1048 else:
1049 parsed, m = remove_string("No pages of output.", parsed)
1050 if not m:
1051 raise TexResultError("no dvifile expected")
1053 for t in texmessages:
1054 parsed = t(parsed)
1055 if parsed.replace(r"(Please type a command or say `\end')", "").replace(" ", "").replace("*\n", "").replace("\n", ""):
1056 raise TexResultError("unhandled TeX response (might be an error)")
1057 except TexResultError as e:
1058 if self.errordetail > errordetail.none:
1059 def add(msg): e.args = (e.args[0] + msg,)
1060 add("\nThe expression passed to TeX was:\n{}".format(indent_text(expr.rstrip())))
1061 if self.errordetail == errordetail.full:
1062 add("\nThe return message from TeX was:\n{}".format(indent_text(unparsed.rstrip())))
1063 if self.errordetail == errordetail.default:
1064 if parsed.count('\n') > 6:
1065 parsed = "\n".join(parsed.split("\n")[:5] + ["(cut after 5 lines; use errordetail.full for all output)"])
1066 add("\nAfter parsing the return message from TeX, the following was left:\n{}".format(indent_text(parsed.rstrip())))
1067 raise e
1068 if oldstate == newstate == STATE_TYPESET:
1069 return extent
1071 def do_start(self):
1072 assert self.state == STATE_START
1073 self.state = STATE_PREAMBLE
1075 if self.tmpdir is None:
1076 self.tmpdir = tempfile.mkdtemp()
1077 atexit.register(self._cleanup)
1078 for usefile in self.usefiles:
1079 extpos = usefile.rfind(".")
1080 try:
1081 os.rename(usefile, os.path.join(self.tmpdir, "texput" + usefile[extpos:]))
1082 except OSError:
1083 pass
1084 cmd = [self.executable, '--output-directory', self.tmpdir]
1085 if self.texipc:
1086 cmd.append("--ipc")
1087 self.popen = config.Popen(cmd, stdin=config.PIPE, stdout=config.PIPE, stderr=config.STDOUT, bufsize=0)
1088 self.texinput = io.TextIOWrapper(self.popen.stdin, encoding=self.texenc)
1089 if self.copyinput:
1090 try:
1091 self.copyinput.write
1092 except AttributeError:
1093 self.texinput = Tee(open(self.copyinput, "w", encoding=self.texenc), self.texinput)
1094 else:
1095 self.texinput = Tee(self.copyinput, self.texinput)
1096 self.texoutput = MonitorOutput(self.name, io.TextIOWrapper(self.popen.stdout, encoding=self.texenc))
1097 self._execute("\\scrollmode\n\\raiseerror%\n" # switch to and check scrollmode
1098 "\\def\\PyX{P\\kern-.3em\\lower.5ex\hbox{Y}\kern-.18em X}%\n" # just the PyX Logo
1099 "\\gdef\\PyXBoxHAlign{0}%\n" # global PyXBoxHAlign (0.0-1.0) for the horizontal alignment, default to 0
1100 "\\newbox\\PyXBox%\n" # PyXBox will contain the output
1101 "\\newbox\\PyXBoxHAligned%\n" # PyXBox will contain the horizontal aligned output
1102 "\\newdimen\\PyXDimenHAlignLT%\n" # PyXDimenHAlignLT/RT will contain the left/right extent
1103 "\\newdimen\\PyXDimenHAlignRT%\n" +
1104 _textattrspreamble + # insert preambles for textattrs macros
1105 "\\long\\def\\ProcessPyXBox#1#2{%\n" # the ProcessPyXBox definition (#1 is expr, #2 is page number)
1106 "\\setbox\\PyXBox=\\hbox{{#1}}%\n" # push expression into PyXBox
1107 "\\PyXDimenHAlignLT=\\PyXBoxHAlign\\wd\\PyXBox%\n" # calculate the left/right extent
1108 "\\PyXDimenHAlignRT=\\wd\\PyXBox%\n"
1109 "\\advance\\PyXDimenHAlignRT by -\\PyXDimenHAlignLT%\n"
1110 "\\gdef\\PyXBoxHAlign{0}%\n" # reset the PyXBoxHAlign to the default 0
1111 "\\immediate\\write16{PyXBox:page=#2," # write page and extents of this box to stdout
1112 "lt=\\the\\PyXDimenHAlignLT,"
1113 "rt=\\the\\PyXDimenHAlignRT,"
1114 "ht=\\the\\ht\\PyXBox,"
1115 "dp=\\the\\dp\\PyXBox:}%\n"
1116 "\\setbox\\PyXBoxHAligned=\\hbox{\\kern-\\PyXDimenHAlignLT\\box\\PyXBox}%\n" # align horizontally
1117 "\\ht\\PyXBoxHAligned0pt%\n" # baseline alignment (hight to zero)
1118 "{\\count0=80\\count1=121\\count2=88\\count3=#2\\shipout\\box\\PyXBoxHAligned}}%\n" # shipout PyXBox to Page 80.121.88.<page number>
1119 "\\def\\PyXInput#1{\\immediate\\write16{PyXInputMarker:executeid=#1:}}%\n" # write PyXInputMarker to stdout
1120 "\\def\\PyXMarker#1{\\hskip0pt\\special{PyX:marker #1}}%", # write PyXMarker special into the dvi-file
1121 self.texmessages_start_default + self.texmessages_start, STATE_PREAMBLE, STATE_PREAMBLE)
1123 def do_preamble(self, expr, texmessages):
1124 if self.state < STATE_PREAMBLE:
1125 self.do_start()
1126 self._execute(expr, texmessages, STATE_PREAMBLE, STATE_PREAMBLE)
1128 def do_typeset(self, expr, texmessages):
1129 if self.state < STATE_PREAMBLE:
1130 self.do_start()
1131 if self.state < STATE_TYPESET:
1132 self.go_typeset()
1133 return self._execute(expr, texmessages, STATE_TYPESET, STATE_TYPESET)
1135 def do_finish(self):
1136 if self.state == STATE_DONE:
1137 return
1138 if self.state < STATE_TYPESET:
1139 self.go_typeset()
1140 self.go_finish()
1141 self.texinput.close() # close the input queue and
1142 self.texoutput.done() # wait for finish of the output
1144 if not self.texipc:
1145 dvifilename = os.path.join(self.tmpdir, "texput.dvi")
1146 self.dvifile = dvifile.DVIfile(dvifilename, debug=self.dvitype)
1147 page = 1
1148 for box in self.needdvitextboxes:
1149 box.readdvipage(self.dvifile, page)
1150 page += 1
1151 if self.dvifile.readpage(None) is not None:
1152 raise ValueError("end of dvifile expected but further pages follow")
1154 def preamble(self, expr, texmessages=[]):
1155 r"""Execute a preamble.
1157 :param str expr: expression to be executed
1158 :param texmessages: additional message parsers
1159 :type texmessages: list of :class:`texmessage` parsers
1161 Preambles must not generate output, but are used to load files, perform
1162 settings, define macros, *etc*. In LaTeX mode, preambles are executed
1163 before ``\begin{document}``. The method can be called multiple times,
1164 but only prior to :meth:`SingleRunner.text` and
1165 :meth:`SingleRunner.text_pt`.
1168 texmessages = self.texmessages_preamble_default + self.texmessages_preamble + texmessages
1169 self.do_preamble(expr, texmessages)
1171 def text(self, x, y, expr, textattrs=[], texmessages=[], fontmap=None, singlecharmode=False):
1172 """create text by passing expr to TeX/LaTeX
1173 - returns a textbox containing the result from running expr thru TeX/LaTeX
1174 - the box center is set to x, y
1175 - *args may contain attr parameters, namely:
1176 - textattr instances
1177 - texmessage instances
1178 - trafo._trafo instances
1179 - style.fillstyle instances"""
1180 if self.state == STATE_DONE:
1181 raise TexDoneError("typesetting process was terminated already")
1182 textattrs = attr.mergeattrs(textattrs) # perform cleans
1183 attr.checkattrs(textattrs, [textattr, trafo.trafo_pt, style.fillstyle])
1184 trafos = attr.getattrs(textattrs, [trafo.trafo_pt])
1185 fillstyles = attr.getattrs(textattrs, [style.fillstyle])
1186 textattrs = attr.getattrs(textattrs, [textattr])
1187 for ta in textattrs[::-1]:
1188 expr = ta.apply(expr)
1189 first = self.state < STATE_TYPESET
1190 left, right, height, depth = self.do_typeset(expr, self.texmessages_run_default + self.texmessages_run + texmessages)
1191 if self.texipc and first:
1192 self.dvifile = dvifile.DVIfile(os.path.join(self.tmpdir, "texput.dvi"), debug=self.dvitype)
1193 box = textbox(x, y, left, right, height, depth, self.do_finish, fontmap, singlecharmode, fillstyles)
1194 for t in trafos:
1195 box.reltransform(t) # TODO: should trafos really use reltransform???
1196 # this is quite different from what we do elsewhere!!!
1197 # see https://sourceforge.net/mailarchive/forum.php?thread_id=9137692&forum_id=23700
1198 if self.texipc:
1199 box.readdvipage(self.dvifile, self.page)
1200 else:
1201 self.needdvitextboxes.append(box)
1202 return box
1204 def text_pt(self, x, y, expr, *args, **kwargs):
1205 return self.text(x * unit.t_pt, y * unit.t_pt, expr, *args, **kwargs)
1208 class SingleTexRunner(SingleRunner):
1210 def __init__(self, executable=config.get("text", "tex", "tex"), lfs="10pt", **kwargs):
1211 super().__init__(executable=executable, **kwargs)
1212 self.lfs = lfs
1213 self.name = "TeX"
1215 def go_typeset(self):
1216 assert self.state == STATE_PREAMBLE
1217 self.state = STATE_TYPESET
1219 def go_finish(self):
1220 self._execute("\\end%\n", self.texmessages_end_default + self.texmessages_end, STATE_TYPESET, STATE_DONE)
1222 def force_done(self):
1223 self.texinput.write("\n\\end\n")
1225 def do_start(self):
1226 super().do_start()
1227 if self.lfs:
1228 if not self.lfs.endswith(".lfs"):
1229 self.lfs = "%s.lfs" % self.lfs
1230 with config.open(self.lfs, []) as lfsfile:
1231 lfsdef = lfsfile.read().decode("ascii")
1232 self._execute(lfsdef, [], STATE_PREAMBLE, STATE_PREAMBLE)
1233 self._execute("\\normalsize%\n", [], STATE_PREAMBLE, STATE_PREAMBLE)
1234 self._execute("\\newdimen\\linewidth\\newdimen\\textwidth%\n", [], STATE_PREAMBLE, STATE_PREAMBLE)
1237 class SingleLatexRunner(SingleRunner):
1239 texmessages_docclass_default = [texmessage.load]
1240 texmessages_begindoc_default = [texmessage.load, texmessage.no_aux]
1242 def __init__(self, executable=config.get("text", "latex", "latex"),
1243 docclass="article", docopt=None, pyxgraphics=True,
1244 texmessages_docclass=[], texmessages_begindoc=[], **kwargs):
1245 super().__init__(executable=executable, **kwargs)
1246 self.docclass = docclass
1247 self.docopt = docopt
1248 self.pyxgraphics = pyxgraphics
1249 self.texmessages_docclass = texmessages_docclass
1250 self.texmessages_begindoc = texmessages_begindoc
1251 self.name = "LaTeX"
1253 def go_typeset(self):
1254 self._execute("\\begin{document}", self.texmessages_begindoc_default + self.texmessages_begindoc, STATE_PREAMBLE, STATE_TYPESET)
1256 def go_finish(self):
1257 self._execute("\\end{document}%\n", self.texmessages_end_default + self.texmessages_end, STATE_TYPESET, STATE_DONE)
1259 def force_done(self):
1260 self.texinput.write("\n\\catcode`\\@11\\relax\\@@end\n")
1262 def do_start(self):
1263 super().do_start()
1264 if self.pyxgraphics:
1265 with config.open("pyx.def", []) as source, open(os.path.join(self.tmpdir, "pyx.def"), "wb") as dest:
1266 dest.write(source.read())
1267 self._execute("\\makeatletter%\n"
1268 "\\let\\saveProcessOptions=\\ProcessOptions%\n"
1269 "\\def\\ProcessOptions{%\n"
1270 "\\def\\Gin@driver{" + self.tmpdir.replace(os.sep, "/") + "/pyx.def}%\n"
1271 "\\def\\c@lor@namefile{dvipsnam.def}%\n"
1272 "\\saveProcessOptions}%\n"
1273 "\\makeatother",
1274 [], STATE_PREAMBLE, STATE_PREAMBLE)
1275 if self.docopt is not None:
1276 self._execute("\\documentclass[%s]{%s}" % (self.docopt, self.docclass),
1277 self.texmessages_docclass_default + self.texmessages_docclass, STATE_PREAMBLE, STATE_PREAMBLE)
1278 else:
1279 self._execute("\\documentclass{%s}" % self.docclass,
1280 self.texmessages_docclass_default + self.texmessages_docclass, STATE_PREAMBLE, STATE_PREAMBLE)
1283 def reset_for_tex_done(f):
1284 @functools.wraps(f)
1285 def wrapped(self, *args, **kwargs):
1286 try:
1287 return f(self, *args, **kwargs)
1288 except TexDoneError:
1289 self.reset(reinit=True)
1290 return f(self, *args, **kwargs)
1291 return wrapped
1294 class MultiRunner:
1296 def __init__(self, cls, *args, **kwargs):
1297 self.cls = cls
1298 self.args = args
1299 self.kwargs = kwargs
1300 self.reset()
1302 def preamble(self, expr, texmessages=[]):
1303 self.preambles.append((expr, texmessages))
1304 self.instance.preamble(expr, texmessages)
1306 @reset_for_tex_done
1307 def text_pt(self, *args, **kwargs):
1308 return self.instance.text_pt(*args, **kwargs)
1310 @reset_for_tex_done
1311 def text(self, *args, **kwargs):
1312 return self.instance.text(*args, **kwargs)
1314 def reset(self, reinit=False):
1315 "resets the tex runner to its initial state (upto its record to old dvi file(s))"
1316 self.instance = self.cls(*self.args, **self.kwargs)
1317 if reinit:
1318 for expr, texmessages in self.preambles:
1319 self.instance.preamble(expr, texmessages)
1320 else:
1321 self.preambles = []
1324 class TexRunner(MultiRunner):
1326 def __init__(self, **kwargs):
1327 super().__init__(SingleTexRunner, **kwargs)
1330 class LatexRunner(MultiRunner):
1332 def __init__(self, **kwargs):
1333 super().__init__(SingleLatexRunner, **kwargs)
1336 # old, deprecated names:
1337 texrunner = TexRunner
1338 latexrunner = LatexRunner
1340 def set(mode="tex", **kwargs):
1341 # note: defaulttexrunner is deprecated
1342 global default_runner, defaulttexrunner, reset, preamble, text, text_pt
1343 mode = mode.lower()
1344 if mode == "tex":
1345 default_runner = defaulttexrunner = TexRunner(**kwargs)
1346 elif mode == "latex":
1347 default_runner = defaulttexrunner = LatexRunner(**kwargs)
1348 else:
1349 raise ValueError("mode \"TeX\" or \"LaTeX\" expected")
1350 reset = default_runner.reset
1351 preamble = default_runner.preamble
1352 text = default_runner.text
1353 text_pt = default_runner.text_pt
1355 set()
1357 def escapestring(s, replace={" ": "~",
1358 "$": "\\$",
1359 "&": "\\&",
1360 "#": "\\#",
1361 "_": "\\_",
1362 "%": "\\%",
1363 "^": "\\string^",
1364 "~": "\\string~",
1365 "<": "{$<$}",
1366 ">": "{$>$}",
1367 "{": "{$\{$}",
1368 "}": "{$\}$}",
1369 "\\": "{$\setminus$}",
1370 "|": "{$\mid$}"}):
1371 "escape all ascii characters such that they are printable by TeX/LaTeX"
1372 i = 0
1373 while i < len(s):
1374 if not 32 <= ord(s[i]) < 127:
1375 raise ValueError("escapestring function handles ascii strings only")
1376 c = s[i]
1377 try:
1378 r = replace[c]
1379 except KeyError:
1380 i += 1
1381 else:
1382 s = s[:i] + r + s[i+1:]
1383 i += len(r)
1384 return s