This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.py
60 lines (45 loc) · 1.62 KB
/
convert.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
import json
import sys
from pathlib import Path
import click
try:
import networkx as nx
except ImportError:
nx = None # For code warnings
click.secho('[!] NetworkX is required, please install it (python3 -m pip install networkx)', fg='red')
sys.exit(1)
OUTPUT_FORMATS = {
'gexf': nx.write_gexf,
'gml': nx.write_gml,
'graphml': nx.write_graphml,
}
@click.command()
@click.option('--format',
help='Format to use',
default='list')
@click.argument('infile', type=click.Path(file_okay=True, dir_okay=False, readable=True, writable=False, exists=True))
@click.argument('outdir', type=click.Path(file_okay=False, dir_okay=True, readable=True, writable=True, exists=False))
def convert(**kwargs):
if kwargs['format'] == 'list':
click.secho("Available formats:")
for item in OUTPUT_FORMATS:
click.secho('- {}'.format(item))
return 0
writer = OUTPUT_FORMATS.get(kwargs['format'])
if not writer:
click.secho('[!] Invalid format {format}'.format(**kwargs), fg='red')
return 1
infile = Path(kwargs['infile'])
outdir = Path(kwargs['outdir'])
with infile.open('r') as ifile:
base = json.load(ifile)
graph = nx.DiGraph()
for idx, node in enumerate(base['nodes']):
graph.add_node(idx, **node)
for rel in base['links']:
graph.add_edges_from([(rel['source'], rel['target'])], relations=','.join(rel['rels']))
with (outdir / '{}.{}'.format(infile.stem, kwargs['format'])).open('wb') as ofile:
writer(graph, ofile)
outdir.mkdir(exist_ok=True)
if __name__ == '__main__':
convert()