| 1 |
""" |
| 2 |
Graph file parsing. |
| 3 |
""" |
| 4 |
from __future__ import print_function, absolute_import |
| 5 |
|
| 6 |
import sys, re |
| 7 |
import subprocess |
| 8 |
|
| 9 |
from dotviewer import msgstruct |
| 10 |
|
| 11 |
from dotviewer.strunicode import forcestr, forceunicode |
| 12 |
|
| 13 |
re_nonword = re.compile(forcestr(r'([^0-9a-zA-Z_.]+)')) |
| 14 |
re_plain = re.compile(forcestr(r'graph [-0-9.]+ [-0-9.]+ [-0-9.]+$'), re.MULTILINE) |
| 15 |
re_digraph = re.compile(forcestr(r'\b(graph|digraph)\b'), re.IGNORECASE) |
| 16 |
|
| 17 |
def guess_type(content): |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
if re_plain.match(content): |
| 22 |
return 'plain' |
| 23 |
|
| 24 |
bracepos = None |
| 25 |
lastfound = '' |
| 26 |
for match in re_digraph.finditer(content): |
| 27 |
position = match.start() |
| 28 |
if bracepos is None: |
| 29 |
bracepos = content.find(b'{', position) |
| 30 |
if bracepos < 0: |
| 31 |
break |
| 32 |
elif position > bracepos: |
| 33 |
break |
| 34 |
lastfound = match.group() |
| 35 |
if lastfound.lower() == b'digraph': |
| 36 |
return 'dot' |
| 37 |
if lastfound.lower() == b'graph': |
| 38 |
return 'neato' |
| 39 |
print("Warning: could not guess file type, using 'dot'", file=sys.stderr) |
| 40 |
return 'unknown' |
| 41 |
|
| 42 |
def dot2plain_graphviz(content, contenttype, use_codespeak=False): |
| 43 |
if contenttype != 'neato': |
| 44 |
cmdline = 'dot -Tplain' |
| 45 |
else: |
| 46 |
cmdline = 'neato -Tplain' |
| 47 |
|
| 48 |
close_fds = sys.platform != 'win32' |
| 49 |
p = subprocess.Popen(cmdline, shell=True, close_fds=close_fds, |
| 50 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 51 |
(child_in, child_out) = (p.stdin, p.stdout) |
| 52 |
try: |
| 53 |
import threading |
| 54 |
except ImportError: |
| 55 |
bkgndwrite(child_in, content) |
| 56 |
else: |
| 57 |
t = threading.Thread(target=bkgndwrite, args=(child_in, content)) |
| 58 |
t.start() |
| 59 |
plaincontent = child_out.read() |
| 60 |
child_out.close() |
| 61 |
if not plaincontent: |
| 62 |
raise PlainParseError("no result from running 'dot'") |
| 63 |
return plaincontent |
| 64 |
|
| 65 |
def bkgndwrite(f, data): |
| 66 |
f.write(data) |
| 67 |
f.close() |
| 68 |
|
| 69 |
class PlainParseError(Exception): |
| 70 |
pass |
| 71 |
|
| 72 |
def splitline(line, re_word = re.compile(forcestr(r'[^\s"]\S*|["]["]|["].*?[^\\]["]'))): |
| 73 |
import ast |
| 74 |
result = [] |
| 75 |
for word in re_word.findall(line): |
| 76 |
if word.startswith(b'"'): |
| 77 |
word = ast.literal_eval(forceunicode(word)) |
| 78 |
result.append(word) |
| 79 |
return result |
| 80 |
|
| 81 |
def parse_plain(graph_id, plaincontent, links={}, fixedfont=False): |
| 82 |
plaincontent = plaincontent.replace(b'\r\n', b'\n') |
| 83 |
lines = plaincontent.splitlines(True) |
| 84 |
for i in range(len(lines)-2, -1, -1): |
| 85 |
if lines[i].endswith(b'\\\n'): |
| 86 |
lines[i] = lines[i][:-2] + lines[i+1] |
| 87 |
del lines[i+1] |
| 88 |
header = splitline(lines.pop(0)) |
| 89 |
if header[0] != b'graph': |
| 90 |
raise PlainParseError("should start with 'graph'") |
| 91 |
yield (msgstruct.CMSG_START_GRAPH, graph_id) + tuple(header[1:]) |
| 92 |
|
| 93 |
texts = [] |
| 94 |
for line in lines: |
| 95 |
line = splitline(line) |
| 96 |
if line[0] == B'node': |
| 97 |
if len(line) != 11: |
| 98 |
raise PlainParseError("bad 'node'") |
| 99 |
yield (msgstruct.CMSG_ADD_NODE,) + tuple(line[1:]) |
| 100 |
texts.append(line[6]) |
| 101 |
if line[0] == b'edge': |
| 102 |
yield (msgstruct.CMSG_ADD_EDGE,) + tuple(line[1:]) |
| 103 |
i = 4 + 2 * int(line[3]) |
| 104 |
if len(line) > i + 2: |
| 105 |
texts.append(line[i]) |
| 106 |
if line[0] == b'stop': |
| 107 |
break |
| 108 |
|
| 109 |
if links: |
| 110 |
|
| 111 |
seen = {} |
| 112 |
for text in texts: |
| 113 |
for word in re_nonword.split(text): |
| 114 |
if word and word in links and word not in seen: |
| 115 |
t = links[word] |
| 116 |
if isinstance(t, tuple): |
| 117 |
statusbartext, color = t |
| 118 |
else: |
| 119 |
statusbartext = t |
| 120 |
color = None |
| 121 |
if color is not None: |
| 122 |
yield (msgstruct.CMSG_ADD_LINK, word, |
| 123 |
statusbartext, color[0], color[1], color[2]) |
| 124 |
else: |
| 125 |
yield (msgstruct.CMSG_ADD_LINK, word, statusbartext) |
| 126 |
seen[word] = True |
| 127 |
|
| 128 |
if fixedfont: |
| 129 |
yield (msgstruct.CMSG_FIXED_FONT,) |
| 130 |
|
| 131 |
yield (msgstruct.CMSG_STOP_GRAPH,) |
| 132 |
|
| 133 |
def parse_dot(graph_id, content, links={}, fixedfont=False): |
| 134 |
contenttype = guess_type(content) |
| 135 |
if contenttype == 'plain': |
| 136 |
plaincontent = content |
| 137 |
else: |
| 138 |
try: |
| 139 |
plaincontent = dot2plain_graphviz(content, contenttype) |
| 140 |
except PlainParseError as e: |
| 141 |
raise |
| 142 |
return list(parse_plain(graph_id, plaincontent, links, fixedfont)) |