forked from munro/SublimeGraphvizPreview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
65 lines (52 loc) · 1.79 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from subprocess import call
import os
import platform
import re
import tempfile
ENVIRON = os.environ
if platform.system() != 'Windows':
ENVIRON['PATH'] += ':/usr/local/bin'
DIGRAPH_START = re.compile('.*(digraph([ \t\n\r]+[a-zA-Z\200-\377_][a-zA-Z\200-\3770-9_]*[ \t\n\r]*{|[ \t\n\r]*{).*)', re.DOTALL | re.IGNORECASE)
def surroundingGraphviz(data, cursor):
'''
Find graphviz code in source surrounding the cursor.
'''
data_before = data[0:cursor]
data_after = data[cursor:]
# find code before selector
code_before_match = DIGRAPH_START.match(data_before)
if not code_before_match:
return None
code_before = code_before_match.group(1)
unopened_braces = len(code_before.split('{')) - len(code_before.split('}'))
# cursor must be in the middle of the graphviz code
if unopened_braces <= 0:
return None
# find code after selector
code_after_match = re.compile('(' + ('.*\\}' * unopened_braces) + ').*', re.DOTALL).match(data_after)
if not code_after_match:
return None
code_after = code_after_match.group(1)
# done!
code = code_before + code_after
return code
def createTempFile(code):
'''
Write graphviz code to a file.
'''
# temporary graphviz file
grapviz = tempfile.NamedTemporaryFile(prefix='sublime_text_graphviz_', dir=None, suffix='.viz', delete=False, mode='wb')
grapviz.write(code.encode('utf-8'))
grapviz.close()
return grapviz.name
def graphvizDot(filename, type):
'''
Convert graphviz file to a PNG.
'''
# compile pdf
path = os.path.dirname(filename)
os.chdir(path)
f, extension = os.path.splitext(filename)
out_filename = f + '.' + type
call(['dot', '-T' + type, '-o' + out_filename, filename], env=ENVIRON)
return out_filename