-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaphne.py
27 lines (24 loc) · 969 Bytes
/
daphne.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
# Standard imports
import json
import subprocess
from os.path import exists
def load_program(daphne_dir, daphne_file, json_file, mode, compile=True):
'''
Either load a pre-compiled json file or compile daphne and save json
'''
if (not compile) and exists(json_file):
with open(json_file) as f:
ast_or_graph = json.load(f)
else:
ast_or_graph = get_json_from_daphne([mode, '-i', daphne_file], daphne_dir)
with open(json_file, 'w') as f:
json.dump(ast_or_graph, f, indent=4, ensure_ascii=False)
return ast_or_graph
def get_json_from_daphne(args, daphne_dir):
'''
Run the daphne compiler and return the output in json format
'''
proc = subprocess.run(['lein', 'run', '-f', 'json']+args, capture_output=True, cwd=daphne_dir)
if proc.returncode != 0:
raise Exception(proc.stdout.decode()+proc.stderr.decode())
return json.loads(proc.stdout) # Load the output into json