Anonymous avatar Anonymous committed e24873d

Document remote-sagetex code; nuke trailing whitespace; fix typos

Comments (0)

Files changed (3)

 \ST@pausedfalse
 %    \end{macrocode}
 % Set up the file stuff, which will get run at the beginning of the
-% document, after we know what's happening with the |final| option. 
+% document, after we know what's happening with the |final| option.
 % First, we open the |.sage| file:
 %    \begin{macrocode}
 \AtBeginDocument{\@ifundefined{ST@final}{%
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{\percent} 
+% \begin{macro}{\percent}
 % A macro that inserts a percent sign. This is more-or-less stolen from the
 % \textsf{Docstrip} manual; there they change the catcode inside a group
 % and use |gdef|, but here we try to be more \LaTeX y and use
 %\end{center}
 % Observe that we are using a Python script to write \LTX code which
 % writes Python code which writes \LTX code. Crazy!
-% 
+%
 % Here's the wrapper command which does whatever magic we need to get
 % two optional arguments.
 %    \begin{macrocode}
      \gdef\ST@rerun{x}}
 \fi}
 %    \end{macrocode}
-% \autoref{f:stig} makes this a bit clearer. 
+% \autoref{f:stig} makes this a bit clearer.
 % \begin{figure}
 %   \centering
 %   \begin{tikzpicture}
 % \end{macro}
 %
 % \begin{macro}{\ST@endsfbl}
-% The companion to |\ST@beginsfbl|. 
+% The companion to |\ST@beginsfbl|.
 %    \begin{macrocode}
 \newcommand{\ST@endsfbl}{%
 \ST@wsf{except:^^J
   def blockend(self):
     self.progress('end')
 %    \end{macrocode}
-% \end{macro} 
-% \end{macro} 
+% \end{macro}
+% \end{macro}
 %
 % \begin{macro}{plot}
 % \phantomsection\label{plotfn}
 % \end{macro}
 %
 % \begin{macro}{goboom}
+% \phantomsection\label{macro:goboom}
 % When a chunk of Sage code blows up, this function bears the bad news
 % to the user. Normally in Python the traceback is good enough for
 % this, but in this case, we start with a |.sage| file (which is
 % \#2861}.
 % \changes{v2.0.2}{2008/04/21}{Make sure sys.exit sees a Python integer}
 % \end{macro}
-% 
+%
 % \begin{macro}{endofdoc}
 % When we're done processing, we have some cleanup tasks. We
 % want to put the MD5 sum of the |.sage| file that produced the |.sout|
 % changed.
 %
 % How do we include line numbers for our error messages but still allow
-% a program to discover a ``genuine'' change to the |.sage| file? 
+% a program to discover a ``genuine'' change to the |.sage| file?
 %
 % The answer is to only find the MD5 sum of \emph{part} of the |.sage|
 % file. By design, the source file line numbers only appear in calls to
 
 % \endinput
 %</python>
-% Local Variables: 
+% Local Variables:
 % mode: doctex
 % TeX-master: "sagetexpackage"
-% End: 
+% End:

remote-sagetex.dtx

 % \section{The \texttt{remote-sagetex} script}
 % \label{sec:remote-sagetex-code}
 %
-% Here we describe the Python code for |remote-sagetex|
+% Here we describe the Python code for |remote-sagetex.py|. Since its
+% job is to replicate the functionality of using Sage and |sagetex.py|,
+% there is some overlap with the Python module.
 %
 % \iffalse
 %<*remotesagetex>
 % \fi
 %
-% First, |makestatic.py| script. It's about the most basic, generic
-% Python script taking command-line arguments that you'll find. The
-% |#!/usr/bin/env python| line is provided for us by the |.ins| file's
-% preamble, so we don't put it here.
+% The |#!/usr/bin/env python| line is provided for us by the |.ins|
+% file's preamble, so we don't put it here.
 %    \begin{macrocode}
-# You can fill in your information here, or you can set any of these to
-# None and the script will ask you to provide them.
-server = 'http://localhost:8000'
-username = 'admin'
-password = 'password'
-
+from __future__ import print_function
 import sys
 try:
     import json
 except ImportError:
-    print 'You need Python 2.6 or later to run this! Exiting.'
+    print('You need Python 2.6 or later to run this! Exiting.')
     sys.exit(1)
 import time
 import re
 import os.path
 import shutil
 import getopt
-from contextlib import contextmanager
+from contextlib import closing
 
-def usage():
-    print("""Process a SageTeX-generated .sage file using a remote Sage server.
+#########################################################################
+# You can fill in your information here, or you can set any of these to #
+# None and the script will ask you to provide them if you don't specify #
+# them as command-line arguments.                                       #
+#########################################################################
+server = None       # 'http://foo.com:8000', https works too
+username = None     # 'my_name'
+password = None     # 's33krit'
+
+usage = """Process a SageTeX-generated .sage file using a remote Sage server.
 
 Usage: {0} [options] inputfile.sage
 
 them.
 
 See the SageTeX documentation for more details on usage and limitations
-of remote-sagetex.""".format(sys.argv[0]))
+of remote-sagetex.""".format(sys.argv[0])
 
 try:
     opts, args = getopt.getopt(sys.argv[1:], 'hs:u:p:',
                                ['help', 'server=', 'user=', 'password='])
-except getopt.GetoptError, err:
-    print(str(err))
-    usage()
+except getopt.GetoptError as err:
+    print(str(err), usage, sep='\n\n')
+    #    usage()
     sys.exit(2)
 
 for o, a in opts:
     if o in ('-h', '--help'):
-        usage()
+        print(usage)
         sys.exit()
     elif o in ('-s', '--server'):
         server = a
         password = a
 
 if len(args) != 1:
-    print('Error: must specify exactly one file. Please specify options first.\n')
-    usage()
+    print('Error: must specify exactly one file. Please specify options first.',
+          usage, sep='\n\n')
     sys.exit(2)
 
 jobname = os.path.splitext(args[0])[0]
-
+%    \end{macrocode}
+% When we send things to the server, we get everything back as a string,
+% including tracebacks. We can search through output using regexps, but
+% there's a more robust way: put in a special string that changes every
+% time, and look for that. Then it is massively unlikely that a user's
+% code could produce output that we'll mistake for an actual traceback.
+% System time will work well enough for these purposes. We produce this
+% string now, and we it when parsing the |.sage| file (we insert it into
+% code blocks) and when parsing the output that the remote server gives
+% us.
+%    \begin{macrocode}
+traceback_str = 'Exception in SageTeX session {0}:'.format(time.time())
+%    \end{macrocode}
+% \begin{macro}{parsedotsage}
+% To figure out what commands to send the remote server, we actually
+% read in the |.sage| file as strings and parse it. This seems a bit
+% strange, but since we know exactly what the format of that file is, we
+% can parse it with a couple flags and a handful of regexps.
+%    \begin{macrocode}
 def parsedotsage(fn):
     with open(fn, 'r') as f:
-        ignore = re.compile(r"""(\#\#.This.file.*was.\*autogenerated)|
-                                (import.sagetex)|
-                                (_st_.=.sagetex)|
-                                (_st_.blockend\(\))|
-                                (_st_.useimagemagick = True)|
-                                (_st_.useepstopdf = True)|
-                                (try:)|
-                                (except:)""", re.VERBOSE)
+%    \end{macrocode}
+% Here are the regexps we use to snarf the interesting bits out of the
+% |.sage| file. Below we'll use the |re| module's |match| function so we
+% needn't anchor any of these at the beginning of the line.
+%    \begin{macrocode}
+        inline = re.compile(r" _st_.inline\((?P<num>\d+), (?P<code>.*)\)")
+        plot = re.compile(r" _st_.plot\((?P<num>\d+), (?P<code>.*)\)")
         goboom = re.compile(r" _st_.goboom\((?P<num>\d+)\)")
-        pausemsg = re.compile(r"print '(?P<msg>SageTeX (un)?paused.*)'")
-        inline = re.compile(r" _st_.inline\((?P<num>\d+), (?P<code>.*)\)")
+        pausemsg = re.compile(r"print.'(?P<msg>SageTeX (un)?paused.*)'")
         blockbegin = re.compile(r"_st_.blockbegin\(\)")
-        ws = re.compile(r"(?P<indent>\s+)")
-        plot = re.compile(r" _st_.plot\((?P<num>\d+), (?P<code>.*)\)")
+        ignore = re.compile(r"(try:)|(except):")
         in_comment = False
         in_block = False
         cmds = []
+%    \end{macrocode}
+% Okay, let's go through the file. We're going to make a list of
+% dictionaries. Each dictionary corresponds to something we have to do
+% with the remote server, except for the pause/unpause ones, which we
+% only use to print out information for the user. All the dictionaries
+% have a |type| key, which obviously tells you type they are. The
+% pause/unpause dictionaries then just have a |msg| which we toss out to
+% the user. The ``real'' dictionaries all have the following keys:
+% \begin{itemize}
+% \item |type|: one of |inline|, |plot|, and |block|.
+% \item |goboom|: used to help the user pinpoint errors, just like the
+% |goboom| function (\pageref{macro:goboom}) does.
+% \item |code|: the code to be executed.
+% \end{itemize}
+% Additionally, the |inline| and |plot| dicts have a |num| key for the
+% label we write to the |.sout| file.
+%
+% Here's the whole parser loop. The interesting bits are for parsing
+% blocks because there we need to accumulate several lines of code.
+%    \begin{macrocode}
         for line in f.readlines():
-            if not ignore.match(line):
-                if line[:-1] == '"""':
-                    in_comment = not in_comment
-                elif not in_comment:
-                    m = pausemsg.match(line)
-                    if m:
-                        cmds.append({'type': 'pause',
-                                     'msg': m.group('msg')})
-                    m = inline.match(line)
-                    if m:
-                        cmds.append({'type': 'inline',
-                                     'num': m.group('num'),
-                                     'code': m.group('code')})
-                    m = plot.match(line)
-                    if m:
-                        cmds.append({'type': 'plot',
-                                     'num': m.group('num'),
-                                     'code': m.group('code')})
-                    m = goboom.match(line)
-                    if m:
-                        cmds[-1]['goboom'] = m.group('num')
-                        if in_block:
-                            in_block = False
+            if line.startswith('"""'):
+                in_comment = not in_comment
+            elif not in_comment:
+                m = pausemsg.match(line)
+                if m:
+                    cmds.append({'type': 'pause',
+                                 'msg': m.group('msg')})
+                m = inline.match(line)
+                if m:
+                    cmds.append({'type': 'inline',
+                                 'num': m.group('num'),
+                                 'code': m.group('code')})
+                m = plot.match(line)
+                if m:
+                    cmds.append({'type': 'plot',
+                                 'num': m.group('num'),
+                                 'code': m.group('code')})
+%    \end{macrocode}
+% The order of the next three ``if''s is important, since we need the
+% ``goboom'' line and the ``blockbegin'' line to \emph{not} get included
+% into the block's code. Note that the lines in the |.sage| file already
+% have some indentation, which we'll use when sending the block to the
+% server---we wrap the text in a try/except.
+%    \begin{macrocode}
+                m = goboom.match(line)
+                if m:
+                    cmds[-1]['goboom'] = m.group('num')
                     if in_block:
-                        if cmds[-1]['indent'] == 0:
-                            # this is the first line of the block, so
-                            # establish indentation to remove. We know it
-                            # must be at least 1.
-                            cmds[-1]['indent'] = len(ws.match(line).group('indent'))
-                        cmds[-1]['cell'] += line[cmds[-1]['indent']:]
-                    if blockbegin.match(line):
-                        cmds.append({'type': 'block',
-                                     'cell': '',
-                                     'indent': 0})
-                        in_block = True
+                        in_block = False
+                if in_block and not ignore.match(line):
+                    cmds[-1]['code'] += line
+                if blockbegin.match(line):
+                    cmds.append({'type': 'block',
+                                 'code': '',
+                                 'indent': 0})
+                    in_block = True
     return cmds
-
+%    \end{macrocode}
+% \end{macro}
+% Parsing the |.sage| file is simple enough so that we can write one
+% function and just do it. Interacting with the remote server is a bit
+% more complicated, and requires us to carry some state, so let's make a
+% class.
+%
+% \begin{macro}{RemoteSage}
+% This is pretty simple; it's more or less a translation of the examples
+% in \texttt{sage/server/simple/twist.py}.
+%    \begin{macrocode}
 debug = False
-
 class RemoteSage:
     def __init__(self, server, user, password):
-        self.srv = server
+        self._srv = server
         sep = '___S_A_G_E___'
-        self.response = re.compile('(?P<header>.*)' + sep +
+        self._response = re.compile('(?P<header>.*)' + sep +
                                    '\n*(?P<output>.*)', re.DOTALL)
-        self.session = self.get_url('login',
+        self._session = self._get_url('login',
                                     urllib.urlencode({'username': user,
                                     'password':
                                     password}))['session']
-        self.did_plot_setup = False
+%    \end{macrocode}
+% In the string below, we want to do ``partial formatting'': we format
+% in the traceback string now, and want to be able to format in the code
+% later. The double braces get ignored by |format()| now, and are picked
+% up by |format()| when we use this later.
+%    \begin{macrocode}
+        self._codewrap = """try:
+{{0}}
+except:
+    print('{0}')
+    traceback.print_exc()""".format(traceback_str)
+        self.do_block("""
+    import traceback
+    def __st_plot__(counter, _p_, format='notprovided', **kwargs):
+        if format == 'notprovided':
+            formats = ['eps', 'pdf']
+        else:
+            formats = [format]
+        for fmt in formats:
+            plotfilename = 'plot-%s.%s' % (counter, fmt)
+            _p_.save(filename=plotfilename, **kwargs)""")
 
+    def _encode(self, d):
+        return 'session={0}&'.format(self._session) + urllib.urlencode(d)
 
-    def encode(self, d):
-        return 'session={0}&'.format(self.session) + urllib.urlencode(d)
-
-    def get_url(self, action, u):
-        with closing(urllib.urlopen(self.srv + '/simple/' + action + '?' + u)) as h:
-            data = self.response.match(h.read())
+    def _get_url(self, action, u):
+        with closing(urllib.urlopen(self._srv + '/simple/' + action +
+                                    '?' + u)) as h:
+            data = self._response.match(h.read())
             result = json.loads(data.group('header'))
             result['output'] = data.group('output').rstrip()
         return result
 
-    def get_file(self, fn, cell, ofn=None):
+    def _get_file(self, fn, cell, ofn=None):
         with closing(urllib.urlopen(server + '/simple/' + 'file' + '?' +
-                     self.encode({'cell': cell, 'file': fn}))) as h:
+                     self._encode({'cell': cell, 'file': fn}))) as h:
             myfn = ofn if ofn else fn
             with open(myfn, 'w') as f:
                 f.write(h.read())
-
-    def do_cell(self, code):
-        result = self.get_url('compute', self.encode({'code': code}))
+%    \end{macrocode}
+% The |code| below gets stuffed between a try/except, so make sure it's
+% indented!
+%    \begin{macrocode}
+    def _do_cell(self, code):
+        realcode = self._codewrap.format(code)
+        result = self._get_url('compute', self._encode({'code': realcode}))
         if result['status'] == 'computing':
             cell = result['cell_id']
             while result['status'] == 'computing':
                 sys.stdout.write('working...')
                 sys.stdout.flush()
                 time.sleep(10)
-                result = self.get_url('status', self.encode({'cell': cell}))
+                result = self._get_url('status', self._encode({'cell': cell}))
         if debug:
-            print('cell: <<<')
-            print(code)
-            print('>>>')
-            print('result: <<<')
-            print(result['output'])
-            print('>>>')
+            print('cell: <<<', realcode, '>>>', 'result: <<<',
+                  result['output'], '>>>', sep='\n')
         return result
 
     def do_inline(self, code):
-        return self.do_cell('latex({0})'.format(code))
+        return self._do_cell(' latex({0})'.format(code))
 
     def do_block(self, code):
-        result = self.do_cell(code)
+        result = self._do_cell(code)
         for fn in result['files']:
-            self.get_file(fn, result['cell_id'])
+            self._get_file(fn, result['cell_id'])
         return result
 
     def do_plot(self, num, code, plotdir):
-        if not self.did_plot_setup:
-            self.do_block("""
-def __st_plot__(counter, _p_, format='notprovided', **kwargs):
-    if format == 'notprovided':
-        formats = ['eps', 'pdf']
-    else:
-        formats = [format]
-    for fmt in formats:
-        plotfilename = 'plot-%s.%s' % (counter, fmt)
-        _p_.save(filename=plotfilename, **kwargs)""")
-            self.did_plot_setup = True
-        result = self.do_cell('__st_plot__({0}, {1})'.format(num, code))
+        result = self._do_cell(' __st_plot__({0}, {1})'.format(num, code))
         for fn in result['files']:
-            self.get_file(fn, result['cell_id'], os.path.join(plotdir, fn))
+            self._get_file(fn, result['cell_id'], os.path.join(plotdir, fn))
         return result
-
+%    \end{macrocode}
+% When using the simple server API, it's important to log out so the
+% server doesn't accumulate idle sessions that take up lots of memory.
+% We define a |close()| method and use this class with the |closing|
+% context manager that always calls |close()| on the way out.
+%    \begin{macrocode}
     def close(self):
         sys.stdout.write('Logging out of {0}...'.format(server))
         sys.stdout.flush()
-        self.get_url('logout', self.encode({}))
+        self._get_url('logout', self._encode({}))
         print('done')
-
-# is it just me, or does the function definition read like something by
-# Dr. Seuss?
-@contextmanager
-def closing(thing):
-    try:
-        yield thing
-    finally:
-        thing.close()
-
+%    \end{macrocode}
+% \end{macro}
+% Next we have a little pile of miscellaneous functions and variables
+% that we want to have at hand while doing our work. Note that we again
+% use the traceback string in the error-finding regular expression.
+%    \begin{macrocode}
 def do_plot_setup(plotdir):
     print('Initializing plots directory')
     if os.path.isdir(plotdir):
     os.mkdir(plotdir)
     return True
 
+did_plot_setup = False
+plotdir = 'sage-plots-for-' + jobname + '.tex'
+
 def labelline(n, s):
     return r'\newlabel{@sageinline' + str(n) + '}{{' + s  + '}{}{}{}{}}\n'
 
-def progress(s, linebreak=True):
-    if linebreak:
-        print(s)
-    else:
-        sys.stdout.write(s)
-        sys.stdout.flush()
+def printc(s):
+    print(s, end='')
+    sys.stdout.flush()
 
-did_plot_setup = False
-plotdir = 'sage-plots-for-' + jobname + '.tex'
+error = re.compile("(^" + traceback_str + ")|(^Syntax Error:)", re.MULTILINE)
 
-error = re.compile(r"(^Traceback \(most recent call last\):)|" +
-                   r"(^SyntaxError:)", re.MULTILINE)
 def check_for_error(string, line):
     if error.search(string):
-        print('\n**** Error in Sage code on line {0} of {1}.tex!'.format(
-                line, jobname))
-        print(string)
-        print('\n**** Running Sage on {0}.sage failed! Fix {0}.tex and try again.'.format(jobname))
+        print("""
+**** Error in Sage code on line {0} of {1}.tex!
+{2}
+**** Running Sage on {1}.sage failed! Fix {1}.tex and try again.""".format(
+              line, jobname, string))
         sys.exit(1)
-
-# okay, now we start actually doing stuff
-
-progress('Processing Sage code for {0}.tex using remote Sage server.'.format(
-    jobname))
+%    \end{macrocode}
+% Now let's actually start doing stuff.
+%    \begin{macrocode}
+print('Processing Sage code for {0}.tex using remote Sage server.'.format(
+      jobname))
 
 if not server:
     server = raw_input('Enter server: ')
     password = getpass('Please enter password for user {0} on {1}: '.format(
         username, server))
 
-if server[:4] != 'http':
+if not server.startswith('http'):
     server = 'https://' + server
 
-progress('Parsing {0}.sage...'.format(jobname), False)
+printc('Parsing {0}.sage...'.format(jobname))
 cmds = parsedotsage(jobname + '.sage')
-progress('done.')
+print('done.')
 
 sout = '% This file was *autogenerated* from the file {0}.sage.\n'.format(
     os.path.splitext(jobname)[0])
 
-progress('Logging into {0} and starting session...'.format(server), False)
+printc('Logging into {0} and starting session...'.format(server))
 with closing(RemoteSage(server, username, password)) as sage:
-    progress('done.')
+    print('done.')
     for cmd in cmds:
         if cmd['type'] == 'inline':
-            progress('Inline formula {0}...'.format(cmd['num']), False)
+            printc('Inline formula {0}...'.format(cmd['num']))
             result = sage.do_inline(cmd['code'])
             check_for_error(result['output'], cmd['goboom'])
             sout += labelline(cmd['num'], result['output'])
-            progress('done.')
+            print('done.')
         if cmd['type'] == 'block':
-            progress('Code block begin...'.format(cmd['goboom']), False)
-            result = sage.do_block(cmd['cell'])
+            printc('Code block begin...')
+            result = sage.do_block(cmd['code'])
             check_for_error(result['output'], cmd['goboom'])
-            progress('end.')
+            print('end.')
         if cmd['type'] == 'plot':
-            progress('Plot {0}...'.format(cmd['num']), False)
+            printc('Plot {0}...'.format(cmd['num']))
             if not did_plot_setup:
                 did_plot_setup = do_plot_setup(plotdir)
             result = sage.do_plot(cmd['num'], cmd['code'], plotdir)
             check_for_error(result['output'], cmd['goboom'])
-            progress('done.')
+            print('end.')
         if cmd['type'] == 'pause':
-            progress(cmd['msg'])
+            print(cmd['msg'])
         if int(time.time()) % 2280 == 0:
-            progress('Unscheduled offworld activation; closing iris...', False)
+            printc('Unscheduled offworld activation; closing iris...')
             time.sleep(1)
-            progress('done.')
+            print('end.')
 
 with open(jobname + '.sage', 'r') as sagef:
     h = hashlib.md5()
     for line in sagef:
-        if line[:12] != ' _st_.goboom' and line[:12] != "print 'SageT":
+        if (not line.startswith(' _st_.goboom') and 
+            not line.startswith("print 'SageT")):
             h.update(line)
     sout += """%{0}% md5sum of corresponding .sage file
 % (minus "goboom" and pause/unpause lines)
 """.format(h.hexdigest())
 
-progress('Writing .sout file...', False)
+printc('Writing .sout file...')
 with open(jobname + '.sout', 'w') as soutf:
     soutf.write(sout)
-    progress('done.')
-progress('Sage processing complete. Run LaTeX on {0}.tex again.'.format(jobname))
+    print('done.')
+print('Sage processing complete. Run LaTeX on {0}.tex again.'.format(jobname))
 %    \end{macrocode}
 
 % \endinput
 %</remotesagetex>
-% Local Variables: 
+% Local Variables:
 % mode: doctex
 % TeX-master: "sagetexpackage"
-% End: 
+% End:

sagetexpackage.dtx

 \begin{document}
 \DocInput{sagetexpackage.dtx}
 \StopEventually{
-\section{Credits and acknowledgements}
+\section{Credits and acknowledgments}
 
 According to the original README file, this system was originally done
 by Gonzalo Tornaria and Joe Wetherell. Later Harald Schilly made some
  clearpage below makes the index start nicely in the middle of a page
  (until my change history gets too long!) and solves all those
  problems. It can be removed/pulled into this comment if you're
- confident the pagebreak problems won't occur.
+ confident the page break problems won't occur.
 \clearpage
 \fi
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.