-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_tree.py
39 lines (29 loc) · 961 Bytes
/
create_tree.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 12 23:56:25 2018
@author: gjacopo
"""
import os
import warnings
try:
import json
except:
try:
import simplejson as json#analysis:ignore
except:
warnings.warn("Package simplejson/json not imported")
#%%
#==============================================================================
# ToC FUNCTION
#==============================================================================
def __filedirexists(file):
return os.path.exists(os.path.abspath(os.path.dirname(file)))
def toc2json(toc, **kwargs):
odir, ofmt = kwargs.pop('odir','.'), kwargs.pop('ofmt','json')
otocfn = kwargs.pop('otocfn','ToC')
ofn = '%s/%s.%s' % (odir, otocfn, ofmt)
if __filedirexists(ofn):
with open(ofn, 'w') as f:
# json.dump(toc, f, indent=4) # sort_keys=True
f.write('var treeData = %s;' % json.dumps(toc, indent=4))