From: Tim Vieira Date: Fri, 28 Jun 2013 20:39:46 +0000 (-0400) Subject: pygments is now optional (if it's missing you are warned if you run or import X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=825f7555b45a599fe66297b109a611b5b8526b14;p=dyna2 pygments is now optional (if it's missing you are warned if you run or import debug somewhere.) extended dyna compiler wrapper (dynac) --- diff --git a/src/Dyna/Backend/Python/debug.py b/src/Dyna/Backend/Python/debug.py index f74f50a..8c7c212 100644 --- a/src/Dyna/Backend/Python/debug.py +++ b/src/Dyna/Backend/Python/debug.py @@ -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, """\ - - - - - - - - - - -""" + print >> html, HEADING print >> html, '
' print >> html, '
'
-
-
         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, '%s    ' % (lineno + offset, line)
@@ -302,19 +265,15 @@ function selectline(lineno) {
         print >> html, '
' print >> html, '
' - 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, '
' - 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 = """ + + + + + + + + + + +""" + if __name__ == '__main__': from argparse import ArgumentParser diff --git a/src/Dyna/Backend/Python/utils.py b/src/Dyna/Backend/Python/utils.py index 548e2c5..602f0f3 100644 --- a/src/Dyna/Backend/Python/utils.py +++ b/src/Dyna/Backend/Python/utils.py @@ -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]