Commit 0c94f08a authored by Jameson Rollins's avatar Jameson Rollins

updates for networkx >= 2.0

mostly updates for usage of new graph `nodes` View object:
parent cac09a20
import re
import inspect
import networkx
# HACK: for pydot->pydotplus in newer networkx (>1.10?)
from networkx import to_pydot
import pydot
except ImportError:
from networkx.drawing.nx_pydot import to_pydot
import pydotplus as pydot
from networkx.drawing.nx_pydot import to_pydot
import pydot
NOT_DECORATORS = ['property']
......@@ -68,8 +63,7 @@ def find_jump_edges(guardstate, module):
def fullgraph(system, find_jumps=False):
"""Return MultiDiGraph copy of system graph with JUMP edges added."""
G = networkx.MultiDiGraph(system.graph)
for node, data in G.nodes(data=True):
code = data['code']
for node, code in G.nodes(data='code'):
obj = code()
if find_jumps:
for jump in find_jump_edges(code, system.module):
......@@ -154,13 +148,13 @@ def sys2dot(system,
color = 5
# set attributes
G.node[node]['label'] = label
G.node[node]['shape'] = shape
G.node[node]['style'] = style
G.node[node]['fillcolor'] = '/%s/%s' % (colorscheme, color)
G.node[node]['penwidth'] = penwidth
G.node[node]['color'] = outline
G.node[node]['fontcolor'] = fontcolor
G.nodes[node]['label'] = label
G.nodes[node]['shape'] = shape
G.nodes[node]['style'] = style
G.nodes[node]['fillcolor'] = '/%s/%s' % (colorscheme, color)
G.nodes[node]['penwidth'] = penwidth
G.nodes[node]['color'] = outline
G.nodes[node]['fontcolor'] = fontcolor
# set edge attributes
remove_edges = []
......@@ -343,13 +343,14 @@ class GuardSystem(object):
o = obj()
# finally, add the state to the graph
def add_edge(self, edge, goto=False):
"""Add an edge to the system.
......@@ -619,15 +620,15 @@ class GuardSystem(object):
def is_goto(self, state):
"""Return True if state is a goto state."""
return self._graph.node[state]['goto']
return self._graph.nodes[state]['goto']
def is_request(self, state):
"""Return True if state is a requestable state."""
return self._graph.node[state]['request']
return self._graph.nodes[state]['request']
def can_redirect(self, state):
"""Return True if state is redirectable."""
return self._graph.node[state]['redirect']
return self._graph.nodes[state]['redirect']
def states_iter(self, reverse=True, filters=[], init_last=True):
......@@ -657,7 +658,7 @@ class GuardSystem(object):
yield (state, data['index'], data['request'])
if init_last and 'INIT' in self._graph:
state = 'INIT'
data = self._graph.node[state]
data = self._graph.nodes[state]
if filt(filters, data):
yield (state, data['index'], data['request'])
......@@ -707,7 +708,7 @@ class GuardSystem(object):
def __iter__(self):
"""Iterator over state names."""
return iter(self._graph.nodes())
return iter(self._graph.nodes)
def _data_for_key(self, key):
"""Return state (key, data) tuple for state.
......@@ -720,14 +721,14 @@ class GuardSystem(object):
if isinstance(key, str):
data = self._graph.node[key]
data = self._graph.nodes[key]
return data['index'], data
except KeyError:
raise KeyError("%s is not a state name" % key)
elif isinstance(key, int):
for state, data in self._graph.nodes(data=True):
if data['index'] == key:
for state, index in self._graph.nodes(data='index'):
if index == key:
return state, data
raise KeyError("%s is not a state index" % key)
......@@ -743,15 +744,15 @@ class GuardSystem(object):
def indices(self):
"""List of state indices."""
return [data['index'] for state, data in self._graph.nodes(data=True)]
return [index for state, index in self._graph.nodes(data='index')]
def state_index_dict(self):
"""Return a dict of state:index pairs."""
return {state: data['index'] for state, data in self._graph.nodes(data=True)}
return {state: index for state, index in self._graph.nodes(data='index')}
def index_state_dict(self):
"""Return a dict of index:state pairs."""
return {data['index']: state for state, data in self._graph.nodes(data=True)}
return {index: state for state, index in self._graph.nodes(data='index')}
def index(self, key):
"""Convert state name to index or vice versa.
......@@ -784,20 +785,11 @@ class GuardSystem(object):
# check validity of state and request arguments
if source:
# HACK: a bug in networkx 1.6 causes shortest_path to fail if
# source == target and weighting is specified. bypass this by
# just returning what would be returned in that case, i.e. a
# single element list containing the request. This is fixed
# in networkx 1.7.
if source == request:
return [request]
# if the source in not specified (None), find the goto with
# the shortest path to the request
if not source and self.goto_states:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment