Commit fa6816e0 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Remove "permissions" control in Graph class

parent 26553a42
...@@ -1621,10 +1621,6 @@ class Graph(object): ...@@ -1621,10 +1621,6 @@ class Graph(object):
# directedness is always a filter # directedness is always a filter
self.set_directed(g.is_directed()) self.set_directed(g.is_directed())
# modification permissions
self.__perms = {"add_edge": True, "del_edge": True,
"add_vertex": True, "del_vertex": True}
def copy(self): def copy(self):
"""Return a deep copy of self. All :ref:`internal property maps <sec_internal_props>` """Return a deep copy of self. All :ref:`internal property maps <sec_internal_props>`
are also copied.""" are also copied."""
...@@ -1655,10 +1651,6 @@ class Graph(object): ...@@ -1655,10 +1651,6 @@ class Graph(object):
# Graph access # Graph access
# ============ # ============
def __check_perms(self, ptype):
if not self.__perms[ptype]:
raise RuntimeError("the graph cannot be modified at this point!")
def vertices(self): def vertices(self):
"""Return an :meth:`iterator <iterator.__iter__>` over the vertices. """Return an :meth:`iterator <iterator.__iter__>` over the vertices.
...@@ -1759,7 +1751,6 @@ class Graph(object): ...@@ -1759,7 +1751,6 @@ class Graph(object):
if n == 0: if n == 0:
return (None for i in range(0, 0)) return (None for i in range(0, 0))
self.__check_perms("add_vertex")
v = libcore.add_vertex(self.__graph, n) v = libcore.add_vertex(self.__graph, n)
vfilt = self.get_vertex_filter() vfilt = self.get_vertex_filter()
...@@ -1815,8 +1806,6 @@ class Graph(object): ...@@ -1815,8 +1806,6 @@ class Graph(object):
the graph will no longer be the same. the graph will no longer be the same.
""" """
self.__check_perms("del_vertex")
try: try:
vs = numpy.array([int(vertex)], dtype="int64") vs = numpy.array([int(vertex)], dtype="int64")
except TypeError: except TypeError:
...@@ -1862,7 +1851,6 @@ class Graph(object): ...@@ -1862,7 +1851,6 @@ class Graph(object):
in the graph if they don't yet exist. in the graph if they don't yet exist.
""" """
self.__check_perms("add_edge")
e = libcore.add_edge(self.__graph, e = libcore.add_edge(self.__graph,
self.vertex(int(source), add_missing=add_missing), self.vertex(int(source), add_missing=add_missing),
self.vertex(int(target), add_missing=add_missing)) self.vertex(int(target), add_missing=add_missing))
...@@ -1887,7 +1875,6 @@ class Graph(object): ...@@ -1887,7 +1875,6 @@ class Graph(object):
unchanged, unless :meth:`~Graph.set_fast_edge_removal` is set to unchanged, unless :meth:`~Graph.set_fast_edge_removal` is set to
`True`, in which case it can change. `True`, in which case it can change.
""" """
self.__check_perms("del_edge")
return libcore.remove_edge(self.__graph, edge) return libcore.remove_edge(self.__graph, edge)
def add_edge_list(self, edge_list, hashed=False, string_vals=False, def add_edge_list(self, edge_list, hashed=False, string_vals=False,
...@@ -1911,7 +1898,6 @@ class Graph(object): ...@@ -1911,7 +1898,6 @@ class Graph(object):
If given, ``eprops`` specifies edge property maps that will be filled If given, ``eprops`` specifies edge property maps that will be filled
with the remaining values at each row, if there are more than two. with the remaining values at each row, if there are more than two.
""" """
self.__check_perms("add_edge")
if eprops is None: if eprops is None:
eprops = () eprops = ()
else: else:
...@@ -1954,13 +1940,10 @@ class Graph(object): ...@@ -1954,13 +1940,10 @@ class Graph(object):
def clear(self): def clear(self):
"""Remove all vertices and edges from the graph.""" """Remove all vertices and edges from the graph."""
self.__check_perms("del_vertex")
self.__check_perms("del_edge")
self.__graph.Clear() self.__graph.Clear()
def clear_edges(self): def clear_edges(self):
"""Remove all edges from the graph.""" """Remove all edges from the graph."""
self.__check_perms("del_edge")
self.__graph.ClearEdges() self.__graph.ClearEdges()
# Internal property maps # Internal property maps
......
...@@ -97,36 +97,6 @@ __all__ = ["bfs_search", "BFSVisitor", "dfs_search", "DFSVisitor", ...@@ -97,36 +97,6 @@ __all__ = ["bfs_search", "BFSVisitor", "dfs_search", "DFSVisitor",
"StopSearch"] "StopSearch"]
class VisitorWrapper(object):
def __init__(self, g, visitor, edge_members, vertex_members):
self.visitor = visitor
self.g = g
self.edge_members = set(edge_members)
self.vertex_members = set(vertex_members)
def __getattr__(self, attr):
try:
orig_attr = self.visitor.__getattribute__(attr)
except AttributeError:
return object.__getattribute__(self, attr)
if callable(orig_attr):
def wrapped_visitor_member(*args, **kwargs):
old_perms = dict(self.g._Graph__perms)
perms ={"del_vertex": False, "del_edge": False, "add_edge": False}
if attr in self.edge_members:
perms.update({"del_edge": True, "add_edge": True})
elif attr in self.vertex_members:
perms.update({"add_vertex": False})
self.g._Graph__perms.update(perms)
try:
ret = orig_attr(*args, **kwargs)
finally:
self.g._Graph__perms.update(old_perms)
return ret
return wrapped_visitor_member
else:
return orig_attr
class BFSVisitor(object): class BFSVisitor(object):
r"""A visitor object that is invoked at the event-points inside the r"""A visitor object that is invoked at the event-points inside the
:func:`~graph_tool.search.bfs_search` algorithm. By default, it performs no :func:`~graph_tool.search.bfs_search` algorithm. By default, it performs no
...@@ -308,10 +278,6 @@ def bfs_search(g, source, visitor=BFSVisitor()): ...@@ -308,10 +278,6 @@ def bfs_search(g, source, visitor=BFSVisitor()):
.. [bfs-wikipedia] http://en.wikipedia.org/wiki/Breadth-first_search .. [bfs-wikipedia] http://en.wikipedia.org/wiki/Breadth-first_search
""" """
visitor = VisitorWrapper(g, visitor,
["initialize_vertex", "examine_vertex", "finish_vertex"],
["initialize_vertex"])
try: try:
libgraph_tool_search.bfs_search(g._Graph__graph, libgraph_tool_search.bfs_search(g._Graph__graph,
int(source), visitor) int(source), visitor)
...@@ -539,10 +505,6 @@ def dfs_search(g, source, visitor=DFSVisitor()): ...@@ -539,10 +505,6 @@ def dfs_search(g, source, visitor=DFSVisitor()):
.. [dfs-wikipedia] http://en.wikipedia.org/wiki/Depth-first_search .. [dfs-wikipedia] http://en.wikipedia.org/wiki/Depth-first_search
""" """
visitor = VisitorWrapper(g, visitor,
["initialize_vertex", "discover_vertex", "finish_vertex",
"start_vertex"], ["initialize_vertex"])
try: try:
libgraph_tool_search.dfs_search(g._Graph__graph, libgraph_tool_search.dfs_search(g._Graph__graph,
int(source), visitor) int(source), visitor)
...@@ -811,10 +773,6 @@ def dijkstra_search(g, source, weight, visitor=DijkstraVisitor(), dist_map=None, ...@@ -811,10 +773,6 @@ def dijkstra_search(g, source, weight, visitor=DijkstraVisitor(), dist_map=None,
.. [dijkstra-wikipedia] http://en.wikipedia.org/wiki/Dijkstra's_algorithm .. [dijkstra-wikipedia] http://en.wikipedia.org/wiki/Dijkstra's_algorithm
""" """
visitor = VisitorWrapper(g, visitor,
["initialize_vertex", "examine_vertex", "finish_vertex"],
["initialize_vertex"])
if visitor is None: if visitor is None:
visitor = DijkstraVisitor() visitor = DijkstraVisitor()
if dist_map is None: if dist_map is None:
...@@ -1078,8 +1036,6 @@ def bellman_ford_search(g, source, weight, visitor=BellmanFordVisitor(), ...@@ -1078,8 +1036,6 @@ def bellman_ford_search(g, source, weight, visitor=BellmanFordVisitor(),
.. [bellman-ford-wikipedia] http://en.wikipedia.org/wiki/Bellman-Ford_algorithm .. [bellman-ford-wikipedia] http://en.wikipedia.org/wiki/Bellman-Ford_algorithm
""" """
visitor = VisitorWrapper(g, visitor, [], [])
if dist_map is None: if dist_map is None:
dist_map = g.new_vertex_property(weight.value_type()) dist_map = g.new_vertex_property(weight.value_type())
if pred_map is None: if pred_map is None:
...@@ -1537,10 +1493,6 @@ def astar_search(g, source, weight, visitor=AStarVisitor(), ...@@ -1537,10 +1493,6 @@ def astar_search(g, source, weight, visitor=AStarVisitor(),
.. [astar-wikipedia] http://en.wikipedia.org/wiki/A*_search_algorithm .. [astar-wikipedia] http://en.wikipedia.org/wiki/A*_search_algorithm
""" """
visitor = VisitorWrapper(g, visitor,
["initialize_vertex", "examine_vertex", "finish_vertex"],
["initialize_vertex"])
if dist_map is None: if dist_map is None:
dist_map = g.new_vertex_property(weight.value_type()) dist_map = g.new_vertex_property(weight.value_type())
if pred_map is None: if pred_map is None:
...@@ -1572,9 +1524,6 @@ def astar_search(g, source, weight, visitor=AStarVisitor(), ...@@ -1572,9 +1524,6 @@ def astar_search(g, source, weight, visitor=AStarVisitor(),
try: try:
if not implicit: if not implicit:
g._Graph__perms.update({"del_vertex": False, "del_edge": False,
"add_edge": False})
libgraph_tool_search.astar_search(g._Graph__graph, libgraph_tool_search.astar_search(g._Graph__graph,
int(source), _prop("v", g, dist_map), int(source), _prop("v", g, dist_map),
_prop("v", g, pred_map), _prop("v", g, pred_map),
...@@ -1587,19 +1536,13 @@ def astar_search(g, source, weight, visitor=AStarVisitor(), ...@@ -1587,19 +1536,13 @@ def astar_search(g, source, weight, visitor=AStarVisitor(),
elif cost_map.value_type() != dist_map.value_type(): elif cost_map.value_type() != dist_map.value_type():
raise ValueError("The cost_map value type must be the same as" + raise ValueError("The cost_map value type must be the same as" +
" dist_map.") " dist_map.")
g._Graph__perms.update({"del_vertex": False})
libgraph_tool_search.astar_search_implicit\ libgraph_tool_search.astar_search_implicit\
(g._Graph__graph, int(source), (g._Graph__graph, int(source),
_prop("v", g, dist_map), _prop("v", g, pred_map), _prop("v", g, dist_map), _prop("v", g, pred_map),
_prop("v", g, cost_map), _prop("e", g, weight), visitor, _prop("v", g, cost_map), _prop("e", g, weight), visitor,
compare, combine, zero, infinity, h) compare, combine, zero, infinity, h)
except StopSearch: except StopSearch:
g._Graph__perms.update({"del_vertex": True, "del_edge": True, pass
"add_edge": True})
finally:
g._Graph__perms.update({"del_vertex": True, "del_edge": True,
"add_edge": True})
return dist_map, pred_map return dist_map, pred_map
......
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