<s> from __future__ import print_function , division <EOL> from os . path import join <EOL> import tempfile <EOL> import shutil <EOL> import io <EOL> from io import BytesIO <EOL> try : <EOL> from subprocess import STDOUT , CalledProcessError , check_output <EOL> except ImportError : <EOL> pass <EOL> from sympy . core . compatibility import unicode , u_decode <EOL> from sympy . utilities . exceptions import SymPyDeprecationWarning <EOL> from sympy . utilities . misc import find_executable <EOL> from . latex import latex <EOL> from sympy . utilities . decorator import doctest_depends_on <EOL> @ doctest_depends_on ( exe = ( '<STR_LIT>' , '<STR_LIT>' ) , modules = ( '<STR_LIT>' , ) , <EOL> disable_viewers = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def preview ( expr , output = '<STR_LIT>' , viewer = None , euler = True , packages = ( ) , <EOL> filename = None , outputbuffer = None , preamble = None , dvioptions = None , <EOL> outputTexFile = None , ** latex_settings ) : <EOL> r"""<STR_LIT>""" <EOL> special = [ '<STR_LIT>' ] <EOL> if viewer is None : <EOL> if output == "<STR_LIT>" : <EOL> viewer = "<STR_LIT>" <EOL> else : <EOL> candidates = { <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> } <EOL> try : <EOL> for candidate in candidates [ output ] : <EOL> path = find_executable ( candidate ) <EOL> if path is not None : <EOL> viewer = path <EOL> break <EOL> else : <EOL> raise SystemError ( <EOL> "<STR_LIT>" % output ) <EOL> except KeyError : <EOL> raise SystemError ( "<STR_LIT>" % output ) <EOL> else : <EOL> if viewer == "<STR_LIT:file>" : <EOL> if filename is None : <EOL> SymPyDeprecationWarning ( feature = "<STR_LIT>" <EOL> "<STR_LIT>" , deprecated_since_version = "<STR_LIT>" , <EOL> useinstead = "<STR_LIT>" , <EOL> issue = <NUM_LIT> ) . warn ( ) <EOL> elif viewer == "<STR_LIT>" : <EOL> SymPyDeprecationWarning ( feature = "<STR_LIT>" , <EOL> useinstead = "<STR_LIT>" , deprecated_since_version = "<STR_LIT>" , <EOL> issue = <NUM_LIT> ) . warn ( ) <EOL> viewer = "<STR_LIT>" <EOL> if outputbuffer is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif viewer == "<STR_LIT>" : <EOL> if outputbuffer is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif viewer not in special and not find_executable ( viewer ) : <EOL> raise SystemError ( "<STR_LIT>" % viewer ) <EOL> if preamble is None : <EOL> actual_packages = packages + ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if euler : <EOL> actual_packages += ( "<STR_LIT>" , ) <EOL> package_includes = "<STR_LIT:\n>" + "<STR_LIT:\n>" . join ( [ "<STR_LIT>" % p <EOL> for p in actual_packages ] ) <EOL> preamble = r"""<STR_LIT>""" % ( package_includes ) <EOL> else : <EOL> if len ( packages ) > <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> latex_main = preamble + '<STR_LIT>' + r"<STR_LIT>" <EOL> if isinstance ( expr , str ) : <EOL> latex_string = expr <EOL> else : <EOL> latex_string = latex ( expr , mode = '<STR_LIT>' , ** latex_settings ) <EOL> try : <EOL> workdir = tempfile . mkdtemp ( ) <EOL> with io . open ( join ( workdir , '<STR_LIT>' ) , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as fh : <EOL> fh . write ( unicode ( latex_main ) % u_decode ( latex_string ) ) <EOL> if outputTexFile is not None : <EOL> shutil . copyfile ( join ( workdir , '<STR_LIT>' ) , outputTexFile ) <EOL> if not find_executable ( '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> try : <EOL> check_output ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , cwd = workdir , stderr = STDOUT ) <EOL> except CalledProcessError as e : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % <EOL> e . output ) <EOL> if output != "<STR_LIT>" : <EOL> defaultoptions = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> } <EOL> commandend = { <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> } <EOL> if output == "<STR_LIT>" : <EOL> cmd = [ "<STR_LIT>" ] <EOL> else : <EOL> cmd = [ "<STR_LIT>" + output ] <EOL> if not find_executable ( cmd [ <NUM_LIT:0> ] ) : <EOL> raise RuntimeError ( "<STR_LIT>" % cmd [ <NUM_LIT:0> ] ) <EOL> try : <EOL> if dvioptions is not None : <EOL> cmd . extend ( dvioptions ) <EOL> else : <EOL> cmd . extend ( defaultoptions [ output ] ) <EOL> cmd . extend ( commandend [ output ] ) <EOL> except KeyError : <EOL> raise SystemError ( "<STR_LIT>" % output ) <EOL> try : <EOL> check_output ( cmd , cwd = workdir , stderr = STDOUT ) <EOL> except CalledProcessError as e : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % <EOL> ( '<STR_LIT:U+0020>' . join ( cmd ) , e . output ) ) <EOL> src = "<STR_LIT>" % ( output ) <EOL> if viewer == "<STR_LIT:file>" : <EOL> if filename is None : <EOL> buffer = BytesIO ( ) <EOL> with open ( join ( workdir , src ) , '<STR_LIT:rb>' ) as fh : <EOL> buffer . write ( fh . read ( ) ) <EOL> return buffer <EOL> else : <EOL> shutil . move ( join ( workdir , src ) , filename ) <EOL> elif viewer == "<STR_LIT>" : <EOL> with open ( join ( workdir , src ) , '<STR_LIT:rb>' ) as fh : <EOL> outputbuffer . write ( fh . read ( ) ) <EOL> elif viewer == "<STR_LIT>" : <EOL> try : <EOL> from pyglet import window , image , gl <EOL> from pyglet . window import key <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if output == "<STR_LIT>" : <EOL> from pyglet . image . codecs . png import PNGImageDecoder <EOL> img = image . load ( join ( workdir , src ) , decoder = PNGImageDecoder ( ) ) <EOL> else : <EOL> raise SystemError ( "<STR_LIT>" ) <EOL> offset = <NUM_LIT> <EOL> config = gl . Config ( double_buffer = False ) <EOL> win = window . Window ( <EOL> width = img . width + <NUM_LIT:2> * offset , <EOL> height = img . height + <NUM_LIT:2> * offset , <EOL> caption = "<STR_LIT>" , <EOL> resizable = False , <EOL> config = config <EOL> ) <EOL> win . set_vsync ( False ) <EOL> try : <EOL> def on_close ( ) : <EOL> win . has_exit = True <EOL> win . on_close = on_close <EOL> def on_key_press ( symbol , modifiers ) : <EOL> if symbol in [ key . Q , key . ESCAPE ] : <EOL> on_close ( ) <EOL> win . on_key_press = on_key_press <EOL> def on_expose ( ) : <EOL> gl . glClearColor ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> gl . glClear ( gl . GL_COLOR_BUFFER_BIT ) <EOL> img . blit ( <EOL> ( win . width - img . width ) / <NUM_LIT:2> , <EOL> ( win . height - img . height ) / <NUM_LIT:2> <EOL> ) <EOL> win . on_expose = on_expose <EOL> while not win . has_exit : <EOL> win . dispatch_events ( ) <EOL> win . flip ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> win . close ( ) <EOL> else : <EOL> try : <EOL> check_output ( [ viewer , src ] , cwd = workdir , stderr = STDOUT ) <EOL> except CalledProcessError as e : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % <EOL> ( viewer , src , e . output ) ) <EOL> finally : <EOL> try : <EOL> shutil . rmtree ( workdir ) <EOL> except OSError as e : <EOL> if e . errno != <NUM_LIT:2> : <EOL> raise </s>
