]> hydra-www.ietfng.org Git - dyna2/commitdiff
pygments is now optional (if it's missing you are warned if you run or import
authorTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 20:39:46 +0000 (16:39 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 20:39:46 +0000 (16:39 -0400)
debug somewhere.)

extended dyna compiler wrapper (dynac)

src/Dyna/Backend/Python/debug.py
src/Dyna/Backend/Python/utils.py

index f74f50a1d1d3b761eb1ea212509a77caba907707..8c7c212f326e05f08b5ff20f2a50c37ad0cf5f69 100644 (file)
@@ -5,22 +5,48 @@ normalization process.
 """
 
 import re, os, shutil, webbrowser
-from collections import defaultdict, namedtuple
-from utils import magenta, red, green, yellow, white, read_anf
+from collections import defaultdict
+from utils import dynac, read_anf
 from config import dynahome
+from path import path
+from warnings import warn
+
+try:
+    from pygments import highlight
+    from pygments.lexers import get_lexer_by_name
+    from pygments.formatters import HtmlFormatter
+
+except ImportError as e:
+    warn('pygments not installed.')
+    def format_code(code):
+        return code, 0
+
+else:
+    def format_code(code):
+        lexer = get_lexer_by_name("haskell")
+        formatter = HtmlFormatter(linenos=False)
+        c = re.sub('%', '--', code)
+        pretty = highlight(c, lexer, formatter)
+        # Pygments seems to toss out blank lines on top...
+        offset = 0
+        for x in code.split('\n'):
+            if x.strip():  # stop of first non empty line
+                break
+            offset += 1
+        pretty = re.sub('--', '%', pretty)
+        return pretty, offset
 
-from pygments import highlight
-from pygments.lexers import get_lexer_by_name
-from pygments.formatters import HtmlFormatter
 
 cssfile = dynahome / 'src' / 'Dyna' / 'Backend' / 'Python' / 'debug-pygments.css'
 jsfile = dynahome / 'external' / 'prototype-1.6.0.3.js'
 
-Edge = namedtuple('Edge', 'head label body')  # "body" is sometimes called the "tail"
-
-def edge_code(x):
-    return '%s = %s(%s)' % (x.head, x.label, ', '.join(x.body)) if x.body else x.label
-Edge.__repr__ = edge_code
+class Edge(object):
+    def __init__(self, head, label, body):
+        self.head = head
+        self.label = label
+        self.body = body
+    def __repr__(self):
+        return '%s = %s(%s)' % (self.head, self.label, ', '.join(self.body)) if self.body else self.label
 
 
 class Hypergraph(object):
@@ -205,92 +231,29 @@ def graph_styles(g):
 
 def main(dynafile, browser=True):
 
+    dynafile = path(dynafile)
+
     if not os.path.exists(cssfile) or not os.path.exists(jsfile):
-        print("Debug must be run from the root of the Dyna source tree")
+        print 'Debug must be run from the root of the Dyna source tree.'
         return
 
     d = dynafile + '.d'
-    os.system('mkdir -p %s' % d)
-
-    shutil.copyfile(cssfile,d+"/debug-pygments.css")
-    shutil.copyfile(jsfile,d+"/prototype.js")
+    d.mkdir_p()
 
-    with file(d + '/index.html', 'wb') as html:
+    # XXX: this is sort of silly
+    shutil.copyfile(cssfile, d + '/debug-pygments.css')
+    shutil.copyfile(jsfile, d + '/prototype.js')
 
-        print >> html, """\
-<head>
-<style>
+    with file(d / 'index.html', 'wb') as html:
 
-html, body {margin:0; padding:0; width: 5000px;}
-
-#dyna-source, #circuit-pane, #dopamine-pane, #update-handler-pane {
-  padding-right: 10px;
-  width: 700px;
-  display: inline;
-  float: left;
-  padding: 20px;
-}
-
-#dyna-source { width: 500px; }
-#circuit-pane { width: 700px; }
-#dopamine-pane { width: 500px; }
-#update-handler-pane { width: 500px; }
-
-#dyna-source, #circuit-pane, #dopamine-pane {
-  border-right: 1px solid #666
-}
-
-h2 { margin-top: 40px; }
-a { cursor: pointer; }
-svg { width: 95%; height: 97%; }
-body { background: black; color: white; }
-
-</style>
-
-<link rel="stylesheet" href="debug-pygments.css">
-
-<script type="text/javascript" language="javascript" src="prototype.js"></script>
-
-<script type="text/javascript" language="javascript">
-function selectline(lineno) {
-  var r = source_to_ruleix[lineno];
-
-  //alert(lineno + "->" + r);
-
-  $("update-handler-pane").innerHTML = "";
-  $$(".handler-" + r).each(function (e) { $("update-handler-pane").innerHTML += e.innerHTML; });
-
-  $("dopamine-pane").innerHTML = "";
-  $$(".dopamine-" + r).each(function (e) { $("dopamine-pane").innerHTML += e.innerHTML; });
-
-  $("circuit-pane").innerHTML = "";
-  $$(".circuit-" + r).each(function (e) { $("circuit-pane").innerHTML += e.innerHTML; });
-}
-</script>
-
-</head>
-"""
+        print >> html, HEADING
 
         print >> html, '<div id="dyna-source">'
         print >> html, '  <pre>'
-
-
         with file(dynafile) as f:
             original_code = f.read()
 
-        offset = 0
-        for x in original_code.split('\n'):
-            if x.strip():  # stop of first non empty line
-                break
-            offset += 1
-
-        lexer = get_lexer_by_name("haskell")
-        formatter = HtmlFormatter(linenos=False)
-        c = re.sub('%', '--', original_code)
-        pretty_code = highlight(c, lexer, formatter)
-        pretty_code = re.sub('--', '%', pretty_code)
-
-#        from arsenal.debug import ip; ip()
+        pretty_code, offset = format_code(original_code)
 
         for lineno, line in enumerate(pretty_code.split('\n'), start=0):
             print >> html, '<a onclick="selectline(%s)">%s    </a>' % (lineno + offset, line)
@@ -302,19 +265,15 @@ function selectline(lineno) {
         print >> html, '<div id="dopamine-pane" style=""></div>'
         print >> html, '<div id="update-handler-pane" style=""></div>'
 
-        cmd = """%s/dist/build/dyna/dyna -B python \
---dump-anf="%s"/anf \
---dump-dopini="%s"/dopini \
---dump-dopupd="%s"/dopupd \
--o "%s"/plan "%s" """ % (dynahome,d,d,d,d,dynafile)
-        if 0 != os.system(cmd):
-            print 'command failed:\n\t' + cmd
-            os.system('gnome-open %s 2>/dev/null >/dev/null' % html.name)
-            return
+        dynac(dynafile,
+              out = d / 'plan',
+              anf = d / 'anf',
+              compiler_args = ['--dump-dopini=' + d / 'dopini',
+                               '--dump-dopupd=' + d / 'dopupd'])
 
         print >> html, '<div style="display:none;">'
 
-        with file(d + '/anf') as f:
+        with file(d / 'anf') as f:
 
             rules = [circuit(x) for x in read_anf(f.read())]
 
@@ -369,6 +328,56 @@ function selectline(lineno) {
         webbrowser.open(html.name)
 
 
+
+HEADING = """
+<head>
+<style>
+
+html, body {margin:0; padding:0; width: 5000px;}
+
+#dyna-source, #circuit-pane, #dopamine-pane, #update-handler-pane {
+  padding-right: 10px;
+  width: 700px;
+  display: inline;
+  float: left;
+  padding: 20px;
+}
+
+#dyna-source { width: 500px; }
+#circuit-pane { width: 700px; }
+#dopamine-pane { width: 500px; }
+#update-handler-pane { width: 500px; }
+
+#dyna-source, #circuit-pane, #dopamine-pane {
+  border-right: 1px solid #666
+}
+
+h2 { margin-top: 40px; }
+a { cursor: pointer; }
+svg { width: 95%; height: 97%; }
+body { background: black; color: white; }
+
+</style>
+
+<link rel="stylesheet" href="debug-pygments.css">
+
+<script type="text/javascript" language="javascript" src="prototype.js"></script>
+
+<script type="text/javascript" language="javascript">
+function selectline(lineno) {
+  var r = source_to_ruleix[lineno];
+  $("update-handler-pane").innerHTML = "";
+  $$(".handler-" + r).each(function (e) { $("update-handler-pane").innerHTML += e.innerHTML; });
+  $("dopamine-pane").innerHTML = "";
+  $$(".dopamine-" + r).each(function (e) { $("dopamine-pane").innerHTML += e.innerHTML; });
+  $("circuit-pane").innerHTML = "";
+  $$(".circuit-" + r).each(function (e) { $("circuit-pane").innerHTML += e.innerHTML; });
+}
+</script>
+
+</head>
+"""
+
 if __name__ == '__main__':
 
     from argparse import ArgumentParser
index 548e2c53ef2dc1feb90e9d036b2c263466c83b7e..602f0f3b888f8186736bb11978810a4d87823130 100644 (file)
@@ -48,7 +48,7 @@ def strip_comments(src):
     return _comments.sub('', src).strip()
 
 
-def dynac(f, out=None):
+def dynac(f, out=None, anf=None, compiler_args=()):
     """
     Run compiler on file, ``f``, write results to ``out``. Raises
     ``DynaCompilerError`` on failure.
@@ -62,9 +62,18 @@ def dynac(f, out=None):
     if out is None:
         out = dotdynadir / 'tmp' / f.read_hexhash('sha1') + '.plan.py'
 
-    p = Popen(['%s/dist/build/dyna/dyna' % dynahome,
-               '--dump-anf=' + out + '.anf',                                 # timv: don't like this filename...
-               '-B', 'python', '-o', out, f], stdout=PIPE, stderr=PIPE)
+    cmd = ['%s/dist/build/dyna/dyna' % dynahome,
+           '-B', 'python', '-o', out, f]
+
+    if anf is None:
+        cmd += ['--dump-anf=' + out + '.anf']
+    else:
+        cmd += ['--dump-anf=' + anf]
+
+    cmd += compiler_args
+
+    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
+
     stdout, stderr = p.communicate()
     if p.returncode:
         assert not stdout.strip(), [stdout, stderr]