#!/usr/bin/env python
"""
com.inamidst.summary - Arcs Summary Widget
Author: Sean B. Palmer, inamidst.com
"""
import urllib, heapq
import trio
def main(arcs, write, params):
lib = arcs.library
write(title='Arcs - Quick Summary')
write('
Quick Summary
')
ranked = []
for node, rank in lib.rank.iteritems():
heapq.heappush(ranked, (-rank, node))
nodes = []
for i in xrange(25):
rank, node = heapq.heappop(ranked)
n = arcs.Node(node, lib)
nodes.append(n)
def write_node(node, association=None):
label = node.label()
origins = [] # @@ if origin length > 3...
for graph_label, G in node.graphs():
branch, leaf = arcs.split(G.docURI)
if G.docURI.startswith('file:///'):
domain = 'localhost '
elif G.docURI.startswith('http://'):
domain = G.docURI.split('/')[2] + ' '
else: domain = ''
origins.append(domain + leaf)
type_labels = list(node.type_labels())
type_labels = sorted(type_labels)
if len(type_labels) > 1:
type_labels = [type_labels[0]] + ['&c.']
if not association:
write('')
else: write('
')
write('')
nodeURI = urllib.quote(str(node.node).strip('<>'), safe='/:')
write('%s' % (nodeURI, label))
if type_labels:
write(' (' + ', '.join(type_labels) + ')')
write('')
write('
')
if association:
write(association)
write('
')
if association:
write(node.summary(80))
else: write(node.summary(160))
write('
')
write('')
sibs = len(node.siblings.keys())
write(', '.join(origins) + ' - ' + str(sibs) + ' facts')
write('')
write('
')
write('\n')
write('')
sibling_count = {}
for node in nodes:
list(node.arcs())
for other in nodes:
if node.node == other.node: continue
if node.siblings.has_key(other.node):
try: sibling_count[node.node] += 1
except KeyError:
sibling_count[node.node] = 1
while nodes:
node = nodes[0]
# list(node.arcs())
write_node(node)
found = 0
for other in nodes[1:]:
if sibling_count.get(other.node, 2) > 1: continue
if node.siblings.has_key(other.node):
arc = node.siblings[other.node].pop()
if arc[1] == trio.n3['rdf:type']: continue
# This arc is from the other direction
if arc[0] == '->':
prefix = '← '
else: prefix = ''
arc_label = prefix + lib.label(arc[1])
write_node(other, arc_label + ' ' + node.label())
nodes.remove(other)
found += 1
if found >= 3: break
nodes = nodes[1:]
write('
')
node_amount = len(lib.manifest.keys())
write('Showing 25 out of %s things...
' % node_amount)
if __name__ == '__main__':
print __doc__