Visualizes information flow connections in humans
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

126 lines
3.3 KiB

#!/bin/python
# The behavior can be influenced by setting certain environment variables:
# - DELETE_LONE_NODES={1,0}
import csv, sys, os
import json
if len(sys.argv) != 4:
print("usage: convert.sh <nodes.csv> <edges.csv> <functions.csv>")
sys.exit()
nodereader = csv.reader(open(sys.argv[1], 'r'), skipinitialspace=True)
edgereader = csv.reader(open(sys.argv[2], 'r'), skipinitialspace=True)
functionreader = csv.reader(open(sys.argv[3], 'r'), skipinitialspace=True)
USE_SUBNODES = False
global nodes
nodes = {}
COMPACT_NAMES = False
TOKEN = "t" if COMPACT_NAMES else "token"
NAME = "n" if COMPACT_NAMES else "name"
FNAME = "N" if COMPACT_NAMES else "fullname"
FLAGS = "f" if COMPACT_NAMES else "flags"
URL = "u" if COMPACT_NAMES else "url"
FUNCTIONS = "F" if COMPACT_NAMES else "info"
SUBNODES = "s" if COMPACT_NAMES else "subnodes"
EDGES = "e" if COMPACT_NAMES else "edges"
EDGE_FLAGS = "f" if COMPACT_NAMES else "flags"
EDGE_TARGET = "t" if COMPACT_NAMES else "target"
EDGE_REFERENCE = "r" if COMPACT_NAMES else "ref"
FUNC_TYPE = "t" if COMPACT_NAMES else "type"
FUNC_CONTENT = "d" if COMPACT_NAMES else "data"
FUNC_REFERENCE = "r" if COMPACT_NAMES else "ref"
def newnode(token):
node = {}
node[TOKEN] = token
node[NAME] = token
return node
def getnode(addr, create=True):
if USE_SUBNODES:
address = addr.split(".")
pointer = nodes
breadcrumb = []
for token in address:
breadcrumb.append(token)
if SUBNODES in pointer and token in pointer[SUBNODES]:
pointer = pointer[SUBNODES][token]
elif create:
if SUBNODES not in pointer:
pointer[SUBNODES] = {}
node = newnode(".".join(breadcrumb))
pointer[SUBNODES][token] = node
pointer = node
else:
return None
return pointer
else:
if addr in nodes:
return nodes[addr]
else:
if create:
node = newnode(addr)
nodes[addr] = node
return node
else:
return None
for entry in edgereader:
if not entry: continue
origin, target, flags, reference = entry
node = getnode(origin)
newedge = {}
if flags:
newedge[EDGE_FLAGS] = flags
newedge[EDGE_TARGET] = target
if reference:
newedge[EDGE_REFERENCE] = reference
if EDGES in node:
node[EDGES].append(newedge)
else:
node[EDGES] = [newedge]
for entry in nodereader:
if not entry: continue
token, name, fullname, flags, url = entry
node = getnode(token)
if fullname:
node[FNAME] = fullname
if name:
node[NAME] = name
if flags:
node[FLAGS] = flags
if url:
node[URL] = url
for entry in functionreader:
if not entry: continue
token, data, reference = entry
node = getnode(token, create=False)
if not node: continue
function = {}
function[FUNC_TYPE] = "text"
function[FUNC_CONTENT] = data
function[FUNC_REFERENCE] = reference
if FUNCTIONS in node:
node[FUNCTIONS].append(function)
else:
node[FUNCTIONS] = [function]
#print(json.dumps(nodes, sort_keys=True, separators=(',',':')))
#print(json.dumps(nodes))
print(json.dumps(nodes, sort_keys=True, indent=4, separators=(",", ": ")))