<s> import os <EOL> import sys <EOL> import json <EOL> import csv <EOL> import StringIO <EOL> def nodeslinks ( threshold ) : <EOL> nodes = [ ] <EOL> links = [ ] <EOL> for line in sys . stdin : <EOL> tokens = line . split ( "<STR_LIT:U+002C>" ) <EOL> try : <EOL> if int ( tokens [ <NUM_LIT:2> ] ) >= threshold : <EOL> if not tokens [ <NUM_LIT:0> ] in nodes : <EOL> nodes . append ( tokens [ <NUM_LIT:0> ] ) <EOL> if not tokens [ <NUM_LIT:1> ] in nodes : <EOL> nodes . append ( tokens [ <NUM_LIT:1> ] ) <EOL> links . append ( { "<STR_LIT:source>" : nodes . index ( tokens [ <NUM_LIT:0> ] ) , <EOL> "<STR_LIT:target>" : nodes . index ( tokens [ <NUM_LIT:1> ] ) , <EOL> "<STR_LIT:value>" : int ( tokens [ <NUM_LIT:2> ] ) } ) <EOL> except : <EOL> continue <EOL> nodelist = [ ] <EOL> for node in nodes : <EOL> nodelist . append ( { "<STR_LIT:name>" : node } ) <EOL> print json . dump ( { "<STR_LIT>" : nodelist , "<STR_LIT>" : links } ) <EOL> def nodeslinktrees ( profile , nodes ) : <EOL> nodesoutput = [ ] <EOL> linksoutput = [ ] <EOL> if hasattr ( profile [ "<STR_LIT>" ] , "<STR_LIT>" ) : <EOL> graph = profile [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> else : <EOL> graph = "<STR_LIT>" <EOL> for node in nodes : <EOL> if graph == "<STR_LIT>" : <EOL> title = "<STR_LIT>" + str ( node [ "<STR_LIT>" ] ) + "<STR_LIT>" <EOL> if node [ "<STR_LIT>" ] != <NUM_LIT:1> : <EOL> title += "<STR_LIT:s>" <EOL> title += "<STR_LIT>" + unicode ( node [ "<STR_LIT:source>" ] ) + "<STR_LIT>" + unicode ( node [ "<STR_LIT:target>" ] ) + "<STR_LIT>" <EOL> else : <EOL> title = "<STR_LIT>" + str ( node [ "<STR_LIT>" ] ) + "<STR_LIT>" <EOL> if node [ "<STR_LIT>" ] != <NUM_LIT:1> : <EOL> title += "<STR_LIT:s>" <EOL> title += "<STR_LIT:)>" <EOL> nodesoutput . append ( { "<STR_LIT:name>" : node [ "<STR_LIT:name>" ] , <EOL> "<STR_LIT:title>" : unicode ( node [ "<STR_LIT:name>" ] ) + title } ) <EOL> for targetname in node [ "<STR_LIT>" ] . iterkeys ( ) : <EOL> target = node [ "<STR_LIT>" ] [ targetname ] <EOL> if target [ "<STR_LIT:count>" ] >= profile [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> linksoutput . append ( { <EOL> "<STR_LIT:source>" : node [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT:target>" : target [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT:value>" : target [ "<STR_LIT:count>" ] <EOL> } ) <EOL> return { "<STR_LIT>" : profile , "<STR_LIT>" : nodesoutput , "<STR_LIT>" : linksoutput } <EOL> def namevaluecsv ( data ) : <EOL> csvout = StringIO . StringIO ( ) <EOL> csvwriter = csv . writer ( csvout ) <EOL> csvwriter . writerow ( [ "<STR_LIT:name>" , "<STR_LIT:value>" ] ) <EOL> for key , value in sorted ( data . iteritems ( ) ) : <EOL> csvwriter . writerow ( [ key , value ] ) <EOL> return csvout . getvalue ( ) <EOL> def valuecsv ( data ) : <EOL> csvout = StringIO . StringIO ( ) <EOL> csvwriter = csv . writer ( csvout ) <EOL> csvwriter . writerow ( [ "<STR_LIT:value>" ] ) <EOL> for d in data : <EOL> csvwriter . writerow ( [ d ] ) <EOL> return csvout . getvalue ( ) <EOL> def nodeslinkcsv ( data ) : <EOL> csvout = StringIO . StringIO ( ) <EOL> csvwriter = csv . writer ( csvout ) <EOL> csvwriter . writerow ( [ "<STR_LIT:source>" , "<STR_LIT:target>" , "<STR_LIT:value>" ] ) <EOL> for node in data : <EOL> source = node [ "<STR_LIT:name>" ] <EOL> for targetname in node [ "<STR_LIT>" ] . iterkeys ( ) : <EOL> csvwriter . writerow ( [ source , targetname , node [ "<STR_LIT>" ] [ targetname ] [ "<STR_LIT:count>" ] ] ) <EOL> return csvout . getvalue ( ) <EOL> def namevaluejson ( data ) : <EOL> output = [ ] <EOL> for key , value in sorted ( data . iteritems ( ) ) : <EOL> output . append ( { "<STR_LIT:name>" : key , "<STR_LIT:value>" : value } ) <EOL> return output <EOL> def valuejson ( data ) : <EOL> output = [ ] <EOL> for d in data : <EOL> output . append ( d ) <EOL> return output <EOL> def embed ( template , d3json ) : <EOL> if d3json [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> metadata_file = d3json [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> else : <EOL> metadata_file = os . path . join ( os . path . dirname ( d3json [ "<STR_LIT>" ] [ "<STR_LIT:args>" ] [ <NUM_LIT:0> ] ) , "<STR_LIT>" ) <EOL> try : <EOL> with open ( metadata_file ) as json_data : <EOL> metadata = json . load ( json_data ) <EOL> json_data . close ( ) <EOL> except : <EOL> metadata = { "<STR_LIT:title>" : d3json [ "<STR_LIT>" ] [ "<STR_LIT:args>" ] [ <NUM_LIT:0> ] <EOL> + ( "<STR_LIT>" if len ( d3json [ "<STR_LIT>" ] [ "<STR_LIT:args>" ] ) > <NUM_LIT:1> else "<STR_LIT>" ) } <EOL> d3json [ "<STR_LIT>" ] = metadata <EOL> template_file = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" , template ) <EOL> with open ( template_file , "<STR_LIT:r>" ) as template : <EOL> output = template . read ( ) <EOL> output = output . replace ( "<STR_LIT>" , metadata [ "<STR_LIT:title>" ] ) <EOL> output = output . replace ( "<STR_LIT>" , json . dumps ( d3json ) ) <EOL> print output </s>
