コード例 #1
0
def deserialize(d, **kw):

    seqno = (lambda q=iter(xrange(0, 2**31)).next: 'e%d' % q())

    dot = dot_parser.parse_dot_data(d)

    verts = dict((unquote(node.get_name()),
                  unquote(node.get_label() or node.get_name()))
                 for node in dot.get_nodes()
                 if node.get_name() not in ('graph', 'node', 'edge'))

    edges = [(seqno(), unquote(link.get_label()), unquote(link.get_source()),
              unquote(link.get_destination())) for link in dot.get_edges()]

    for i, l, s, t in edges:
        verts.setdefault(s, s)
        verts.setdefault(t, t)

    verts = verts.items()

    if dot.get_graph_type() == 'graph':
        # add back-edges to be equivalent to undirected graph
        edges.extend([(seqno(), l, t, s) for i, l, s, t in edges])

    return verts, edges
コード例 #2
0
ファイル: dot.py プロジェクト: algo-dogs/python-graphwalker
def deserialize(d, **kw):

    seqno = (lambda q=iter(xrange(0, 2 ** 31)).next: 'e%d' % q())

    dot = dot_parser.parse_dot_data(d)

    verts = dict((unquote(node.get_name()),
                  unquote(node.get_label() or node.get_name()))
                 for node in dot.get_nodes()
                 if node.get_name() not in ('graph', 'node', 'edge'))

    edges = [(seqno(),
              unquote(link.get_label()),
              unquote(link.get_source()),
              unquote(link.get_destination()))
             for link in dot.get_edges()]

    for i, l, s, t in edges:
        verts.setdefault(s, s)
        verts.setdefault(t, t)

    verts = verts.items()

    if dot.get_graph_type() == 'graph':
        # add back-edges to be equivalent to undirected graph
        edges.extend([(seqno(), l, t, s) for i, l, s, t in edges])

    return verts, edges
コード例 #3
0
ファイル: pydot.py プロジェクト: CityPulse/KAT
def graph_from_dot_data(data):
    """Load graph as defined by data in DOT format.

    The data is assumed to be in DOT format. It will
    be parsed and a Dot class will be returned,
    representing the graph.
    """

    return dot_parser.parse_dot_data(data)
コード例 #4
0
ファイル: tester.py プロジェクト: refaim/spc
    def compare_dot(self, first, second, mode):
        def nodes(g):
            subgraphs = []
            for subgraph in g.get_subgraph_list():
                subgraphs.append(
                    [(node.get_name(), node.get_label())
                        for node in subgraph.get_node_list()])
            return subgraphs

        def edges(g):
            subgraphs = []
            for subgraph in g.get_subgraph_list():
                subgraphs.append(
                    [(edge.get_source(), edge.get_destination())
                        for edge in subgraph.get_edge_list()])
            return subgraphs

        with open(self.test + first, mode) as answer:
            with open(self.test + second, mode) as output:
                agraph = dot_parser.parse_dot_data(answer.read())
                ograph = dot_parser.parse_dot_data(output.read())
                return nodes(agraph) == nodes(ograph) and \
                       edges(agraph) == edges(ograph)
コード例 #5
0
def graph_from_dot_data(data):
	"""Load graph as defined by data in DOT format.
	
	The data is assumed to be in DOT format. It will
	be parsed and a Dot class will be returned, 
	representing the graph.
	"""
	
	graph = dot_parser.parse_dot_data(data)
	if graph is not None:
		dot = Dot()
		dot.__dict__.update(graph.__dict__)
		return dot
		
	return None
コード例 #6
0
ファイル: pydot.py プロジェクト: techvoltage/openrce-snippets
def graph_from_dot_data(data):
    """Load graph as defined by data in DOT format.
	
	The data is assumed to be in DOT format. It will
	be parsed and a Dot class will be returned, 
	representing the graph.
	"""

    graph = dot_parser.parse_dot_data(data)
    if graph is not None:
        dot = Dot()
        dot.__dict__.update(graph.__dict__)
        return dot

    return None
コード例 #7
0
ファイル: dot.py プロジェクト: j2ali/python-graphwalker
def deserialize(d, **kw):

    seqno = lambda q=iter(xrange(0, 2 ** 31)).next: "e%d" % q()

    dot = dot_parser.parse_dot_data(d)

    verts = [
        (unquote(node.get_name()), unquote(node.get_label() or node.get_name()))
        for node in dot.get_nodes()
        if node.get_name() not in ("graph", "node", "edge")
    ]

    edges = [
        (seqno(), unquote(link.get_label()), unquote(link.get_source()), unquote(link.get_destination()))
        for link in dot.get_edges()
    ]

    if dot.get_graph_type() == "graph":
        # add back-edges to be equivalent to undirected graph
        edges.extend([(seqno(), l, d, s) for i, l, s, d in edges])

    return verts, edges
コード例 #8
0
ファイル: dot-neighbour-filter.py プロジェクト: 8l/rose
        description=__doc__,
        epilog='Please report bugs to <*****@*****.**>.')

    cmdline.add_argument('-i','--input',    metavar='<in.dot>',      type=file, help='input graph')
    cmdline.add_argument('-o','--output',   metavar='<out.dot>',                help='output graph')
    cmdline.add_argument('-c','--center',   metavar='<label>', nargs='+',       help='center node')
    cmdline.add_argument('-d','--distance', metavar='<N>', default=3, type=int, help='distance N')
    cmdline.add_argument('-v','--verbose', action='store_true', help='verbose mode')

    args = cmdline.parse_args()

    if not args.input:  print 'no input specified!'; exit(1)
    if not args.output: print 'no output specified!'; exit(1)

    print "reading file", args.input.name
    g = dot_parser.parse_dot_data(args.input.read())

    centers = []
    for c in args.center:
        centers.extend(g.get_node(c))
    if len(centers) == 0: print "center node <%s> not found"%args.center; exit(1)

    edges = dict()
    for e in g.get_edges():
        src = e.get_source()
        dest = e.get_destination()
        edges[src]  = [dest] + (edges[src] if src  in edges.keys() else [])
        edges[dest] = [src] + (edges[dest] if dest in edges.keys() else [])

    num=0
    marked = []
コード例 #9
0
def start(dotfile):
    if getattr(current_app, 'started', False):
        return "Already started"

    current_app.started = True
    
    print("Starting", dotfile)
    directory = os.path.join(os.path.dirname(__file__), "run")
    current_app.src = open(dotfile).read()
    dot = dot_parser.parse_dot_data(current_app.src)
    graph = pydot.graph_from_dot_file(dotfile)[0]

    assert(len(dot) == 1)
    dot = dot[0]
    
    nodes = []
    for e in dot.get_edges():
        points = e.obj_dict['points']
        nodes.append(points[0])
        nodes.append(points[1])

    # Deduplicate nodes
    nodes = set(nodes)
    
    current_app.directory = directory
    current_app.executor = ThreadPoolExecutor(max_workers=50)
    current_app.bitcoind = start_bitcoind(directory)
    current_app.node_factory = NodeFactory(current_app.bitcoind, current_app.executor, current_app.directory)
    ex = current_app.executor
    nf = current_app.node_factory
    for n in nodes:
        print("Starting {}".format(n))
        current_app.node_factory.get_node(name=n, may_reconnect=True)

    def channel_confirming(src, dst):
        peers = src.rpc.listpeers(dst.info['id'])['peers']
        if not peers:
            return False

        peer = peers[0]
        if len(peer['channels']) != 1:
            return False

        channel = peer['channels'][0]
        if channel['state'] != 'CHANNELD_AWAITING_LOCKIN':
            return False
        return True

    for e in dot.get_edges():
        points = e.obj_dict['points']
        print("Connecting {} <-> {}".format(*points))
        src = get_node_by_name(points[0])
        dst = get_node_by_name(points[1])
        assert(src and dst)

        attrs = e.get_attributes()
        e.capacities = get_edge_capacity(attrs)
        ex.submit(src.openchannel, dst, connect=True, capacity=sum(e.capacities), announce=False, confirm=False)

    for e in graph.get_edges():
        points = e.obj_dict['points']
        src = get_node_by_name(points[0])
        dst = get_node_by_name(points[1])
        while not channel_confirming(src, dst):
            time.sleep(0.1)

    current_app.bitcoind.generate_block(6)
    print('Waiting for gossip to propagate')
    while True:
        count = sum([len(n.rpc.listnodes()['nodes']) for n in nf.nodes])
        expected = len(nf.nodes)**2
        
        if expected == count:
            break
        print("Gossip progress {:2f}%".format(100*count/expected))
        time.sleep(1)

    print("Rebalancing channels")
    for e in graph.get_edges():
        attrs = e.get_attributes()
        caps = get_edge_capacity(attrs)
        if caps[1] == 0:
            continue
        points = e.obj_dict['points']
        print("Rebalancing {} --{}-> {}".format(points[0], caps[1], points[1]))
        src = get_node_by_name(points[0])
        dst = get_node_by_name(points[1])
        bolt11 = dst.rpc.invoice(caps[1], 'rebalance-{}-{}'.format(*points), "Rebalancing")['bolt11']
        src.rpc.pay(bolt11)
    
    return {'dot': current_app.src, 'nodes': list(nodes)}
コード例 #10
0
def assert_is_dot_format(dot):
    assert dot_parser.parse_dot_data(dot) is not None
コード例 #11
0
    cmdline.add_argument('-v',
                         '--verbose',
                         action='store_true',
                         help='verbose mode')

    args = cmdline.parse_args()

    if not args.input:
        print 'no input specified!'
        exit(1)
    if not args.output:
        print 'no output specified!'
        exit(1)

    print "reading file", args.input.name
    g = dot_parser.parse_dot_data(args.input.read())

    centers = []
    for c in args.center:
        centers.extend(g.get_node(c))
    if len(centers) == 0:
        print "center node <%s> not found" % args.center
        exit(1)

    edges = dict()
    for e in g.get_edges():
        src = e.get_source()
        dest = e.get_destination()
        edges[src] = [dest] + (edges[src] if src in edges.keys() else [])
        edges[dest] = [src] + (edges[dest] if dest in edges.keys() else [])
コード例 #12
0
def dot_to_json(graph):
    graph = JGraph.from_graph(dot_parser.parse_dot_data(graph))
    return jsonpickle.encode(graph)