Skip to content

base

PermissionGraphBackend

Bases: ABC

Base class for PermissionGraph interface.

Source code in src/permission_graph/backends/base.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class PermissionGraphBackend(abc.ABC):
    """Base class for PermissionGraph interface."""

    @abc.abstractmethod
    def add_vertex(self, vertex: Vertex, **kwargs) -> None:
        """Add a vertex to the permission graph.

        Raises ValueError if vertex already exists.
        """

    @abc.abstractmethod
    def remove_vertex(self, vertex: Vertex, **kwargs) -> None:
        """Remove a vertex from the permission graph."""

    @abc.abstractmethod
    def vertex_exists(self, vertex: Vertex) -> bool:
        """Check if a vertex with vtype=vtype and id=id already exists."""

    @abc.abstractmethod
    def get_vertices_to(self, vertex: Vertex) -> list[Vertex]:
        """Get all vertices that target a vertex."""

    @abc.abstractmethod
    def get_vertices_from(self, vertex: Vertex) -> list[Vertex]:
        """Get all vertices that a vertex targets."""

    @abc.abstractmethod
    def update_vertex_attributes(self, vertex: Vertex, **kwargs):
        """Update one or more attributes of a vertex."""

    @abc.abstractmethod
    def add_edge(self, etype: str, source: Vertex, target: Vertex, **kwargs: Any) -> None:
        """Add a edge to the permission graph.

        Args:
            etype: edge type (one of 'member_of', 'allow', 'deny')
            source: source vertex
            target: target vertex
            **kwargs: addition attributes to add to edge

        Raises ValueError if an edge from source to target already exists.
        """

    @abc.abstractmethod
    def edge_exists(self, source: Vertex, target: Vertex) -> bool:
        """Return True if edge exists."""

    @abc.abstractmethod
    def remove_edge(self, source: Vertex, target: Vertex) -> None:
        """Remove an edge from the permission graph."""

    @abc.abstractmethod
    def shortest_paths(self, source: Vertex, target: Vertex) -> list[list[Vertex]]:
        """Return the lists of vertices that make the shortest paths from source to target.

        Returns:
            - If there is a true shortest path (no ties), return a list containing one element
                (the shortest path).
            - Otherwise, return a list containing all of the paths with length equal to the
                shortest path.
        """

    @abc.abstractmethod
    def get_edge_type(self, source: Vertex, target: Vertex) -> EdgeType:
        """Return the EdgeType of the edge connecting two vertices.

        Raises ValueError if there is no edge between the two vertices.
        """

    @abc.abstractmethod
    def vertex_factory(self, vertex_id) -> Vertex:
        """Return a vertex from a vertex id.

        Given a vertex id, return an vertex object of the appropriate subclass.
        """

add_edge(etype, source, target, **kwargs) abstractmethod

Add a edge to the permission graph.

Parameters:

Name Type Description Default
etype str

edge type (one of 'member_of', 'allow', 'deny')

required
source Vertex

source vertex

required
target Vertex

target vertex

required
**kwargs Any

addition attributes to add to edge

{}

Raises ValueError if an edge from source to target already exists.

Source code in src/permission_graph/backends/base.py
37
38
39
40
41
42
43
44
45
46
47
48
@abc.abstractmethod
def add_edge(self, etype: str, source: Vertex, target: Vertex, **kwargs: Any) -> None:
    """Add a edge to the permission graph.

    Args:
        etype: edge type (one of 'member_of', 'allow', 'deny')
        source: source vertex
        target: target vertex
        **kwargs: addition attributes to add to edge

    Raises ValueError if an edge from source to target already exists.
    """

add_vertex(vertex, **kwargs) abstractmethod

Add a vertex to the permission graph.

Raises ValueError if vertex already exists.

Source code in src/permission_graph/backends/base.py
10
11
12
13
14
15
@abc.abstractmethod
def add_vertex(self, vertex: Vertex, **kwargs) -> None:
    """Add a vertex to the permission graph.

    Raises ValueError if vertex already exists.
    """

edge_exists(source, target) abstractmethod

Return True if edge exists.

Source code in src/permission_graph/backends/base.py
50
51
52
@abc.abstractmethod
def edge_exists(self, source: Vertex, target: Vertex) -> bool:
    """Return True if edge exists."""

get_edge_type(source, target) abstractmethod

Return the EdgeType of the edge connecting two vertices.

Raises ValueError if there is no edge between the two vertices.

Source code in src/permission_graph/backends/base.py
69
70
71
72
73
74
@abc.abstractmethod
def get_edge_type(self, source: Vertex, target: Vertex) -> EdgeType:
    """Return the EdgeType of the edge connecting two vertices.

    Raises ValueError if there is no edge between the two vertices.
    """

get_vertices_from(vertex) abstractmethod

Get all vertices that a vertex targets.

Source code in src/permission_graph/backends/base.py
29
30
31
@abc.abstractmethod
def get_vertices_from(self, vertex: Vertex) -> list[Vertex]:
    """Get all vertices that a vertex targets."""

get_vertices_to(vertex) abstractmethod

Get all vertices that target a vertex.

Source code in src/permission_graph/backends/base.py
25
26
27
@abc.abstractmethod
def get_vertices_to(self, vertex: Vertex) -> list[Vertex]:
    """Get all vertices that target a vertex."""

remove_edge(source, target) abstractmethod

Remove an edge from the permission graph.

Source code in src/permission_graph/backends/base.py
54
55
56
@abc.abstractmethod
def remove_edge(self, source: Vertex, target: Vertex) -> None:
    """Remove an edge from the permission graph."""

remove_vertex(vertex, **kwargs) abstractmethod

Remove a vertex from the permission graph.

Source code in src/permission_graph/backends/base.py
17
18
19
@abc.abstractmethod
def remove_vertex(self, vertex: Vertex, **kwargs) -> None:
    """Remove a vertex from the permission graph."""

shortest_paths(source, target) abstractmethod

Return the lists of vertices that make the shortest paths from source to target.

Returns:

Type Description
list[list[Vertex]]
  • If there is a true shortest path (no ties), return a list containing one element (the shortest path).
list[list[Vertex]]
  • Otherwise, return a list containing all of the paths with length equal to the shortest path.
Source code in src/permission_graph/backends/base.py
58
59
60
61
62
63
64
65
66
67
@abc.abstractmethod
def shortest_paths(self, source: Vertex, target: Vertex) -> list[list[Vertex]]:
    """Return the lists of vertices that make the shortest paths from source to target.

    Returns:
        - If there is a true shortest path (no ties), return a list containing one element
            (the shortest path).
        - Otherwise, return a list containing all of the paths with length equal to the
            shortest path.
    """

update_vertex_attributes(vertex, **kwargs) abstractmethod

Update one or more attributes of a vertex.

Source code in src/permission_graph/backends/base.py
33
34
35
@abc.abstractmethod
def update_vertex_attributes(self, vertex: Vertex, **kwargs):
    """Update one or more attributes of a vertex."""

vertex_exists(vertex) abstractmethod

Check if a vertex with vtype=vtype and id=id already exists.

Source code in src/permission_graph/backends/base.py
21
22
23
@abc.abstractmethod
def vertex_exists(self, vertex: Vertex) -> bool:
    """Check if a vertex with vtype=vtype and id=id already exists."""

vertex_factory(vertex_id) abstractmethod

Return a vertex from a vertex id.

Given a vertex id, return an vertex object of the appropriate subclass.

Source code in src/permission_graph/backends/base.py
76
77
78
79
80
81
@abc.abstractmethod
def vertex_factory(self, vertex_id) -> Vertex:
    """Return a vertex from a vertex id.

    Given a vertex id, return an vertex object of the appropriate subclass.
    """