pyst2/html/_modules/asterisk/agitb.html

334 lines
39 KiB
HTML
Raw Normal View History

2016-03-08 18:36:00 +03:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>asterisk.agitb &mdash; pyst2 0.4.9 documentation</title>
<link rel="stylesheet" href="../../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: '0.4.9',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../_static/jquery.js"></script>
<script type="text/javascript" src="../../_static/underscore.js"></script>
<script type="text/javascript" src="../../_static/doctools.js"></script>
<link rel="top" title="pyst2 0.4.9 documentation" href="../../index.html" />
<link rel="up" title="Module code" href="../index.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for asterisk.agitb</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">.. module:: agi</span>
<span class="sd"> :synopsis: More comprehensive traceback formatting for Python scripts.</span>
<span class="sd">Example</span>
<span class="sd">-------</span>
<span class="sd">To enable this module, do:</span>
<span class="sd">.. code-block:: python</span>
<span class="sd"> import asterisk.agitb, asterisk.agi</span>
<span class="sd"> asterisk.agitb.enable(display = False, logdir = &#39;/var/log/asterisk/&#39;)</span>
<span class="sd"> agi = asterisk.agi.AGI()</span>
<span class="sd"> asterisk.agitb.enable(agi, False, &#39;/var/log/asterisk&#39;)</span>
<span class="sd">at the top of your script. The optional arguments to enable() are:</span>
<span class="sd">* agi - the agi handle to write verbose messages to</span>
<span class="sd">* display - if true, tracebacks are displayed on the asterisk console</span>
<span class="sd"> (used with the agi option)</span>
<span class="sd">* logdir - if set, tracebacks are written to files in this directory</span>
<span class="sd">* context - number of lines of source code to show for each stack frame</span>
<span class="sd">By default, tracebacks are displayed but not saved, and the context is 5 lines.</span>
<span class="sd">You may want to add a logdir if you call agitb.enable() before you have</span>
<span class="sd">an agi.AGI() handle.</span>
<span class="sd">Alternatively, if you have caught an exception and want agitb to display it</span>
<span class="sd">for you, call agitb.handler(). The optional argument to handler() is a</span>
<span class="sd">3-item tuple (etype, evalue, etb) just like the value of sys.exc_info().</span>
<span class="sd">If you do not pass anything to handler() it will use sys.exc_info().</span>
<span class="sd">This script was adapted from Ka-Ping Yee&#39;s cgitb.</span>
<span class="sd">Specification</span>
<span class="sd">-------------</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;Matthew Nicholson&#39;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;0.1.0&#39;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">__UNDEF__</span> <span class="o">=</span> <span class="p">[]</span> <span class="c1"># a special sentinel object</span>
<div class="viewcode-block" id="lookup"><a class="viewcode-back" href="../../agitb.html#asterisk.agitb.lookup">[docs]</a><span class="k">def</span> <span class="nf">lookup</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">frame</span><span class="p">,</span> <span class="nb">locals</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Find the value for a given name in the given environment.&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">locals</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;local&#39;</span><span class="p">,</span> <span class="nb">locals</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
<span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_globals</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;global&#39;</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_globals</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
<span class="k">if</span> <span class="s1">&#39;__builtins__&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_globals</span><span class="p">:</span>
<span class="n">builtins</span> <span class="o">=</span> <span class="n">frame</span><span class="o">.</span><span class="n">f_globals</span><span class="p">[</span><span class="s1">&#39;__builtins__&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">builtins</span><span class="p">,</span> <span class="nb">type</span><span class="p">({})):</span>
<span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">builtins</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;builtin&#39;</span><span class="p">,</span> <span class="n">builtins</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">builtins</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="s1">&#39;builtin&#39;</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">builtins</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">None</span><span class="p">,</span> <span class="n">__UNDEF__</span></div>
<div class="viewcode-block" id="scanvars"><a class="viewcode-back" href="../../agitb.html#asterisk.agitb.scanvars">[docs]</a><span class="k">def</span> <span class="nf">scanvars</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">frame</span><span class="p">,</span> <span class="nb">locals</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Scan one logical line of Python and look up values of variables used.&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">tokenize</span>
<span class="kn">import</span> <span class="nn">keyword</span>
<span class="nb">vars</span><span class="p">,</span> <span class="n">lasttoken</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="p">[],</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">__UNDEF__</span>
<span class="k">for</span> <span class="n">ttype</span><span class="p">,</span> <span class="n">token</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">tokenize</span><span class="o">.</span><span class="n">generate_tokens</span><span class="p">(</span><span class="n">reader</span><span class="p">):</span>
<span class="k">if</span> <span class="n">ttype</span> <span class="o">==</span> <span class="n">tokenize</span><span class="o">.</span><span class="n">NEWLINE</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">if</span> <span class="n">ttype</span> <span class="o">==</span> <span class="n">tokenize</span><span class="o">.</span><span class="n">NAME</span> <span class="ow">and</span> <span class="n">token</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span><span class="p">:</span>
<span class="k">if</span> <span class="n">lasttoken</span> <span class="o">==</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
<span class="k">if</span> <span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">__UNDEF__</span><span class="p">:</span>
<span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">token</span><span class="p">,</span> <span class="n">__UNDEF__</span><span class="p">)</span>
<span class="nb">vars</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">token</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">where</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">lookup</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">frame</span><span class="p">,</span> <span class="nb">locals</span><span class="p">)</span>
<span class="nb">vars</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">token</span><span class="p">,</span> <span class="n">where</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="k">elif</span> <span class="n">token</span> <span class="o">==</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
<span class="n">prefix</span> <span class="o">+=</span> <span class="n">lasttoken</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span>
<span class="n">parent</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">parent</span><span class="p">,</span> <span class="n">prefix</span> <span class="o">=</span> <span class="bp">None</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
<span class="n">lasttoken</span> <span class="o">=</span> <span class="n">token</span>
<span class="k">return</span> <span class="nb">vars</span></div>
<div class="viewcode-block" id="text"><a class="viewcode-back" href="../../agitb.html#asterisk.agitb.text">[docs]</a><span class="k">def</span> <span class="nf">text</span><span class="p">(</span><span class="n">eparams</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a plain text document describing a given traceback.&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">types</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="kn">import</span> <span class="nn">linecache</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">pydoc</span>
<span class="n">etype</span><span class="p">,</span> <span class="n">evalue</span><span class="p">,</span> <span class="n">etb</span> <span class="o">=</span> <span class="n">eparams</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">etype</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">ClassType</span><span class="p">):</span>
<span class="n">etype</span> <span class="o">=</span> <span class="n">etype</span><span class="o">.</span><span class="n">__name__</span>
<span class="n">pyver</span> <span class="o">=</span> <span class="s1">&#39;Python &#39;</span> <span class="o">+</span> <span class="n">sys</span><span class="o">.</span><span class="n">version</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="n">sys</span><span class="o">.</span><span class="n">executable</span>
<span class="n">date</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="n">head</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">etype</span><span class="p">),</span> <span class="n">pyver</span><span class="p">,</span> <span class="n">date</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;&#39;&#39;</span>
<span class="s1">A problem occurred in a Python script. Here is the sequence of</span>
<span class="s1">function calls leading up to the error, in the order they occurred.</span>
<span class="s1">&#39;&#39;&#39;</span>
<span class="n">frames</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">records</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getinnerframes</span><span class="p">(</span><span class="n">etb</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
<span class="k">for</span> <span class="n">frame</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">lnum</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">lines</span><span class="p">,</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">records</span><span class="p">:</span>
<span class="nb">file</span> <span class="o">=</span> <span class="nb">file</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;?&#39;</span>
<span class="n">args</span><span class="p">,</span> <span class="n">varargs</span><span class="p">,</span> <span class="n">varkw</span><span class="p">,</span> <span class="nb">locals</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargvalues</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
<span class="n">call</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="k">if</span> <span class="n">func</span> <span class="o">!=</span> <span class="s1">&#39;?&#39;</span><span class="p">:</span>
<span class="n">call</span> <span class="o">=</span> <span class="s1">&#39;in &#39;</span> <span class="o">+</span> <span class="n">func</span> <span class="o">+</span> \
<span class="n">inspect</span><span class="o">.</span><span class="n">formatargvalues</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">varargs</span><span class="p">,</span> <span class="n">varkw</span><span class="p">,</span> <span class="nb">locals</span><span class="p">,</span>
<span class="n">formatvalue</span><span class="o">=</span><span class="k">lambda</span> <span class="n">value</span><span class="p">:</span> <span class="s1">&#39;=&#39;</span> <span class="o">+</span> <span class="n">pydoc</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">repr</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="n">highlight</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">def</span> <span class="nf">reader</span><span class="p">(</span><span class="n">lnum</span><span class="o">=</span><span class="p">[</span><span class="n">lnum</span><span class="p">]):</span>
<span class="n">highlight</span><span class="p">[</span><span class="n">lnum</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">return</span> <span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">lnum</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">lnum</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="nb">vars</span> <span class="o">=</span> <span class="n">scanvars</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">frame</span><span class="p">,</span> <span class="nb">locals</span><span class="p">)</span>
<span class="n">rows</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39; </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">call</span><span class="p">)]</span>
<span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">lnum</span> <span class="o">-</span> <span class="n">index</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
<span class="n">num</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%5d</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="n">i</span>
<span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">num</span> <span class="o">+</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">())</span>
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">done</span><span class="p">,</span> <span class="n">dump</span> <span class="o">=</span> <span class="p">{},</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">where</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">:</span>
<span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">done</span><span class="p">:</span>
<span class="k">continue</span>
<span class="n">done</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">__UNDEF__</span><span class="p">:</span>
<span class="k">if</span> <span class="n">where</span> <span class="o">==</span> <span class="s1">&#39;global&#39;</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;global &#39;</span> <span class="o">+</span> <span class="n">name</span>
<span class="k">elif</span> <span class="n">where</span> <span class="o">==</span> <span class="s1">&#39;local&#39;</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">where</span> <span class="o">+</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">dump</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">pydoc</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">repr</span><span class="p">(</span><span class="n">value</span><span class="p">)))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">dump</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s1">&#39; undefined&#39;</span><span class="p">)</span>
<span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dump</span><span class="p">))</span>
<span class="n">frames</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rows</span><span class="p">))</span>
<span class="n">exception</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">etype</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">evalue</span><span class="p">))]</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">evalue</span><span class="p">,</span> <span class="n">types</span><span class="o">.</span><span class="n">InstanceType</span><span class="p">):</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">evalue</span><span class="p">):</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">pydoc</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">repr</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">evalue</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
<span class="n">exception</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="si">%s%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="mi">4</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="k">return</span> <span class="n">head</span> <span class="o">+</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">frames</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">exception</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;&#39;&#39;</span>
<span class="s1">The above is a description of an error in a Python program. Here is</span>
<span class="s1">the original traceback:</span>
<span class="si">%s</span><span class="s1"></span>
<span class="s1">&#39;&#39;&#39;</span> <span class="o">%</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception</span><span class="p">(</span><span class="n">etype</span><span class="p">,</span> <span class="n">evalue</span><span class="p">,</span> <span class="n">etb</span><span class="p">))</span></div>
<div class="viewcode-block" id="Hook"><a class="viewcode-back" href="../../agitb.html#asterisk.agitb.Hook">[docs]</a><span class="k">class</span> <span class="nc">Hook</span><span class="p">:</span>
<span class="sd">&quot;&quot;&quot;A hook to replace sys.excepthook that shows tracebacks in HTML.&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">logdir</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
<span class="n">agi</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">display</span> <span class="o">=</span> <span class="n">display</span> <span class="c1"># send tracebacks to browser if true</span>
<span class="bp">self</span><span class="o">.</span><span class="n">logdir</span> <span class="o">=</span> <span class="n">logdir</span> <span class="c1"># log tracebacks to files if not None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">context</span> <span class="o">=</span> <span class="n">context</span> <span class="c1"># number of source code lines per frame</span>
<span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">file</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span> <span class="c1"># place to send the output</span>
<span class="bp">self</span><span class="o">.</span><span class="n">agi</span> <span class="o">=</span> <span class="n">agi</span>
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">etype</span><span class="p">,</span> <span class="n">evalue</span><span class="p">,</span> <span class="n">etb</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">handle</span><span class="p">((</span><span class="n">etype</span><span class="p">,</span> <span class="n">evalue</span><span class="p">,</span> <span class="n">etb</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">handle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">info</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="n">info</span> <span class="o">=</span> <span class="n">info</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">doc</span> <span class="o">=</span> <span class="n">text</span><span class="p">(</span><span class="n">info</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span> <span class="c1"># just in case something goes wrong</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="n">doc</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception</span><span class="p">(</span><span class="o">*</span><span class="n">info</span><span class="p">))</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">display</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="p">:</span> <span class="c1"># print to agi</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">doc</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="o">.</span><span class="n">verbose</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">doc</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="o">.</span><span class="n">verbose</span><span class="p">(</span><span class="s1">&#39;A problem occured in a python script&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;A problem occured in a python script</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">logdir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkstemp</span><span class="p">(</span><span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;.txt&#39;</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">logdir</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="nb">file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fdopen</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> contains the description of this error.&#39;</span> <span class="o">%</span> <span class="n">path</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Tried to save traceback to </span><span class="si">%s</span><span class="s1">, but failed.&#39;</span> <span class="o">%</span> <span class="n">path</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">agi</span><span class="o">.</span><span class="n">verbose</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span></div>
<span class="n">handler</span> <span class="o">=</span> <span class="n">Hook</span><span class="p">()</span><span class="o">.</span><span class="n">handle</span>
<div class="viewcode-block" id="enable"><a class="viewcode-back" href="../../agitb.html#asterisk.agitb.enable">[docs]</a><span class="k">def</span> <span class="nf">enable</span><span class="p">(</span><span class="n">agi</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">logdir</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Install an exception handler that formats tracebacks as HTML.</span>
<span class="sd"> The optional argument &#39;display&#39; can be set to 0 to suppress sending the</span>
<span class="sd"> traceback to the browser, and &#39;logdir&#39; can be set to a directory to cause</span>
<span class="sd"> tracebacks to be written to files there.&quot;&quot;&quot;</span>
<span class="n">except_hook</span> <span class="o">=</span> <span class="n">Hook</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="n">display</span><span class="p">,</span> <span class="n">logdir</span><span class="o">=</span><span class="n">logdir</span><span class="p">,</span>
<span class="n">context</span><span class="o">=</span><span class="n">context</span><span class="p">,</span> <span class="n">agi</span><span class="o">=</span><span class="n">agi</span><span class="p">)</span>
<span class="n">sys</span><span class="o">.</span><span class="n">excepthook</span> <span class="o">=</span> <span class="n">except_hook</span>
<span class="k">global</span> <span class="n">handler</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">except_hook</span><span class="o">.</span><span class="n">handle</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../../index.html">Documentation overview</a><ul>
<li><a href="../index.html">Module code</a><ul>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2016, Randall Degges.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.3.6</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.7</a>
</div>
</body>
</html>