Пример #1
0
 def test_append_vertex_bound_to_another_graph(self):
     g = Graph()
     node = g.add_vertex()
     self.assertRaises(
         interfaces.DatabaseException,
         self.graph.append_vertex,
         node
     )
Пример #2
0
 def test_is_bound(self):
     edge = Edge(self.marko, "de-friends", self.josh)
     graph = Graph()
     graph.bind_to_graph(edge)
     self.assertEqual(
         edge.is_bound(),
         True
     )
Пример #3
0
    def test_no_constraints_filter_found_one_vertex(self):
        graph = Graph()
        graph.add_vertex("dog", name="rex")

        spot = graph.add_vertex("dog", name="spot")
        spot2 = graph.get_or_create_vertex("dog", name="spot")

        self.assertEqual(
            spot2,
            spot,
        )
Пример #4
0
    def test_append_edge_bound_to_another_graph(self):
        g = Graph()
        node1 = g.add_vertex(label="NODE")
        node2 = g.add_vertex(label="NODE")
        edge = g.add_edge(node1, "knows", node2)

        # hack for testing only
        node1.ident = None
        node2.ident = None
        node1.graph = None
        node2.graph = None

        self.assertRaises(interfaces.DatabaseException, self.graph.append_edge,
                          edge)
Пример #5
0
 def test_is_bound(self):
     graph = Graph()
     self.marko.graph = graph  # mock is bound
     self.assertEqual(
         self.marko.is_bound(),
         True
     )
Пример #6
0
def setup_empty_graph(context):
    """
    Setup a empty graph object and attach it to the context object for later.

    :param context: Context object share between all the setups.
    :type context: :class:`behave.runner.Context`
    """
    context.graph = Graph()
Пример #7
0
    def test_append_edge_bound_to_another_graph(self):
        g = Graph()
        node1 = g.add_vertex(label="NODE")
        node2 = g.add_vertex(label="NODE")
        edge = g.add_edge(node1, "knows", node2)

        # hack for testing only
        node1.ident = None
        node2.ident = None
        node1.graph = None
        node2.graph = None

        self.assertRaises(
            interfaces.DatabaseException,
            self.graph.append_edge,
            edge
        )
Пример #8
0
    def test_no_constraints_filter_found_one_vertex(self):
        graph = Graph()
        graph.add_vertex("dog", name="rex")

        spot = graph.add_vertex("dog", name="spot")
        spot2 = graph.get_or_create_vertex("dog", name="spot")

        self.assertEqual(
            spot2,
            spot,
        )
Пример #9
0
"""
Scrape an ansable inventory and build a dependency graph.
"""
from ruruki.graphs import Graph

__all__ = ["GRAPH", "scrape_inventroy", "scrape_hosts", "scrape_playbook"]

GRAPH = Graph()
GRAPH.add_vertex_constraint("HOST", "name")
GRAPH.add_vertex_constraint("GROUP", "name")
GRAPH.add_vertex_constraint("PLAY", "name")
GRAPH.add_vertex_constraint("TASK", "name")


def _link_node_to_groups(node, groups, edge_label="HAS-GROUP"):
    """
    Create group node for all the groups provided and link the
    node to the group node.

    :param node: Node that is using the group.
    :type node: :class:`ruruki.interfaces.IVertex`
    :param groups: Iterable of groups that you are creating,
        inspecting and linking to the node.
    :type groups: :class:`ansible.inventroy.group.Group`
    :param edge_label: Edge label to use.
    :type edge_label: :class:`str`
    """
    for group in groups:
        if group.get_name() == "all":
            continue
Пример #10
0
    def test_load(self):
        graph = Graph()
        fh = helpers.get_test_dump_graph_file_handler()
        graph.load(fh)

        marko = self.graph.get_vertex(0)
        vadas = self.graph.get_vertex(1)
        lop = self.graph.get_vertex(2)
        josh = self.graph.get_vertex(3)
        ripple = self.graph.get_vertex(4)
        peter = self.graph.get_vertex(5)

        peter_created_lop = self.graph.get_edge(0)
        josh_created_lop = self.graph.get_edge(1)
        marko_created_lop = self.graph.get_edge(2)
        marko_knows_josh = self.graph.get_edge(3)
        marko_knows_vadas = self.graph.get_edge(4)
        josh_created_ripple = self.graph.get_edge(5)

        # The containers that the graph uses checks the attributes on
        # vertices and edges and not the `id` from the python built-in
        # function.
        self.assertIn(marko, self.graph)
        self.assertEqual(marko.label, "person")
        self.assertEqual(marko.ident, 0)
        self.assertDictEqual(
            marko.properties,
            {
                "age": 29,
                "name": "marko",
            },
        )

        self.assertIn(vadas, self.graph)
        self.assertEqual(vadas.label, "person")
        self.assertEqual(vadas.ident, 1)
        self.assertDictEqual(
            vadas.properties,
            {
                "age": 27,
                "name": "vadas",
            },
        )

        self.assertIn(lop, self.graph)
        self.assertEqual(lop.label, "app")
        self.assertEqual(lop.ident, 2)
        self.assertDictEqual(
            lop.properties,
            {
                "lang": "java",
                "name": "lop",
            },
        )

        self.assertIn(josh, self.graph)
        self.assertEqual(josh.label, "person")
        self.assertEqual(josh.ident, 3)
        self.assertDictEqual(
            josh.properties,
            {
                "age": 32,
                "name": "josh",
            },
        )

        self.assertIn(ripple, self.graph)
        self.assertEqual(ripple.label, "app")
        self.assertEqual(ripple.ident, 4)
        self.assertDictEqual(
            ripple.properties,
            {
                "lang": "java",
                "name": "ripple",
            },
        )

        self.assertIn(peter, self.graph)
        self.assertEqual(peter.label, "person")
        self.assertEqual(peter.ident, 5)
        self.assertDictEqual(
            peter.properties,
            {
                "age": 35,
                "name": "peter",
            },
        )

        self.assertIn(peter_created_lop, self.graph)
        self.assertEqual(
            peter_created_lop.label, "created"
        )
        self.assertEqual(
            peter_created_lop.head, peter
        )
        self.assertEqual(
            peter_created_lop.tail, lop
        )
        self.assertDictEqual(
            peter_created_lop.properties,
            {
                "weight": 0.2
            }
        )

        self.assertIn(josh_created_lop, self.graph)
        self.assertEqual(
            josh_created_lop.label, "created"
        )
        self.assertEqual(
            josh_created_lop.head, josh
        )
        self.assertEqual(
            josh_created_lop.tail, lop
        )
        self.assertDictEqual(
            josh_created_lop.properties,
            {
                "weight": 0.4
            }
        )

        self.assertIn(marko_created_lop, self.graph)
        self.assertEqual(
            marko_created_lop.label, "created"
        )
        self.assertEqual(
            marko_created_lop.head, marko
        )
        self.assertEqual(
            marko_created_lop.tail, lop
        )
        self.assertDictEqual(
            marko_created_lop.properties,
            {
                "weight": 0.4
            }
        )

        self.assertIn(marko_knows_josh, self.graph)
        self.assertEqual(
            marko_knows_josh.label, "knows"
        )
        self.assertEqual(
            marko_knows_josh.head, marko
        )
        self.assertEqual(
            marko_knows_josh.tail, josh
        )
        self.assertDictEqual(
            marko_knows_josh.properties,
            {
                "weight": 1
            }
        )

        self.assertIn(marko_knows_vadas, self.graph)
        self.assertEqual(
            marko_knows_vadas.label, "knows"
        )
        self.assertEqual(
            marko_knows_vadas.head, marko
        )
        self.assertEqual(
            marko_knows_vadas.tail, vadas
        )
        self.assertDictEqual(
            marko_knows_vadas.properties,
            {
                "weight": 0.5
            }
        )

        self.assertIn(josh_created_ripple, self.graph)
        self.assertEqual(
            josh_created_ripple.label, "created"
        )
        self.assertEqual(
            josh_created_ripple.head, josh
        )
        self.assertEqual(
            josh_created_ripple.tail, ripple
        )
        self.assertDictEqual(
            josh_created_ripple.properties,
            {
                "weight": 1
            }
        )
Пример #11
0
#pylint: skip-file

import logging
import inspect
import ruruki

from ruruki.graphs import Graph

GRAPH = Graph()
GRAPH.add_vertex_constraint("class", "name")
GRAPH.add_vertex_constraint("method", "name")
GRAPH.add_vertex_constraint("file", "name")
GRAPH.add_vertex_constraint("function", "name")
GRAPH.add_vertex_constraint("module", "name")

SEEN = set()


def build_dep(lib, parent):
    previous = parent

    for name, module in inspect.getmembers(lib, inspect.ismodule):
        if module in SEEN:
            continue
        SEEN.add(module)
        parent = GRAPH.get_or_create_vertex("module", name=name)

        # link to the previous parent
        GRAPH.get_or_create_edge(parent, "comes-form", previous)

        try:
Пример #12
0
#pylint: skip-file

import logging
import inspect
import ruruki

from ruruki.graphs import Graph


GRAPH = Graph()
GRAPH.add_vertex_constraint("class", "name")
GRAPH.add_vertex_constraint("method", "name")
GRAPH.add_vertex_constraint("file", "name")
GRAPH.add_vertex_constraint("function", "name")
GRAPH.add_vertex_constraint("module", "name")


SEEN = set()

def build_dep(lib, parent):
    previous = parent

    for name, module in inspect.getmembers(lib, inspect.ismodule):
        if module in SEEN:
            continue
        SEEN.add(module)
        parent = GRAPH.get_or_create_vertex("module", name=name)

        # link to the previous parent
        GRAPH.get_or_create_edge(parent, "comes-form", previous)
Пример #13
0
    def test_load(self):
        graph = Graph()
        fh = helpers.get_test_dump_graph_file_handler()
        graph.load(fh)

        marko = self.graph.get_vertex(0)
        vadas = self.graph.get_vertex(1)
        lop = self.graph.get_vertex(2)
        josh = self.graph.get_vertex(3)
        ripple = self.graph.get_vertex(4)
        peter = self.graph.get_vertex(5)

        peter_created_lop = self.graph.get_edge(0)
        josh_created_lop = self.graph.get_edge(1)
        marko_created_lop = self.graph.get_edge(2)
        marko_knows_josh = self.graph.get_edge(3)
        marko_knows_vadas = self.graph.get_edge(4)
        josh_created_ripple = self.graph.get_edge(5)

        # The containers that the graph uses checks the attributes on
        # vertices and edges and not the `id` from the python built-in
        # function.
        self.assertIn(marko, self.graph)
        self.assertEqual(marko.label, "person")
        self.assertEqual(marko.ident, 0)
        self.assertDictEqual(
            marko.properties,
            {
                "age": 29,
                "name": "marko",
            },
        )

        self.assertIn(vadas, self.graph)
        self.assertEqual(vadas.label, "person")
        self.assertEqual(vadas.ident, 1)
        self.assertDictEqual(
            vadas.properties,
            {
                "age": 27,
                "name": "vadas",
            },
        )

        self.assertIn(lop, self.graph)
        self.assertEqual(lop.label, "app")
        self.assertEqual(lop.ident, 2)
        self.assertDictEqual(
            lop.properties,
            {
                "lang": "java",
                "name": "lop",
            },
        )

        self.assertIn(josh, self.graph)
        self.assertEqual(josh.label, "person")
        self.assertEqual(josh.ident, 3)
        self.assertDictEqual(
            josh.properties,
            {
                "age": 32,
                "name": "josh",
            },
        )

        self.assertIn(ripple, self.graph)
        self.assertEqual(ripple.label, "app")
        self.assertEqual(ripple.ident, 4)
        self.assertDictEqual(
            ripple.properties,
            {
                "lang": "java",
                "name": "ripple",
            },
        )

        self.assertIn(peter, self.graph)
        self.assertEqual(peter.label, "person")
        self.assertEqual(peter.ident, 5)
        self.assertDictEqual(
            peter.properties,
            {
                "age": 35,
                "name": "peter",
            },
        )

        self.assertIn(peter_created_lop, self.graph)
        self.assertEqual(
            peter_created_lop.label, "created"
        )
        self.assertEqual(
            peter_created_lop.head, peter
        )
        self.assertEqual(
            peter_created_lop.tail, lop
        )
        self.assertDictEqual(
            peter_created_lop.properties,
            {
                "weight": 0.2
            }
        )

        self.assertIn(josh_created_lop, self.graph)
        self.assertEqual(
            josh_created_lop.label, "created"
        )
        self.assertEqual(
            josh_created_lop.head, josh
        )
        self.assertEqual(
            josh_created_lop.tail, lop
        )
        self.assertDictEqual(
            josh_created_lop.properties,
            {
                "weight": 0.4
            }
        )

        self.assertIn(marko_created_lop, self.graph)
        self.assertEqual(
            marko_created_lop.label, "created"
        )
        self.assertEqual(
            marko_created_lop.head, marko
        )
        self.assertEqual(
            marko_created_lop.tail, lop
        )
        self.assertDictEqual(
            marko_created_lop.properties,
            {
                "weight": 0.4
            }
        )

        self.assertIn(marko_knows_josh, self.graph)
        self.assertEqual(
            marko_knows_josh.label, "knows"
        )
        self.assertEqual(
            marko_knows_josh.head, marko
        )
        self.assertEqual(
            marko_knows_josh.tail, josh
        )
        self.assertDictEqual(
            marko_knows_josh.properties,
            {
                "weight": 1
            }
        )

        self.assertIn(marko_knows_vadas, self.graph)
        self.assertEqual(
            marko_knows_vadas.label, "knows"
        )
        self.assertEqual(
            marko_knows_vadas.head, marko
        )
        self.assertEqual(
            marko_knows_vadas.tail, vadas
        )
        self.assertDictEqual(
            marko_knows_vadas.properties,
            {
                "weight": 0.5
            }
        )

        self.assertIn(josh_created_ripple, self.graph)
        self.assertEqual(
            josh_created_ripple.label, "created"
        )
        self.assertEqual(
            josh_created_ripple.head, josh
        )
        self.assertEqual(
            josh_created_ripple.tail, ripple
        )
        self.assertDictEqual(
            josh_created_ripple.properties,
            {
                "weight": 1
            }
        )
Пример #14
0
#pylint: skip-file
from ruruki.graphs import Graph

# create a empty graph
graph = Graph()

# create some constraints to ensure uniqueness
graph.add_vertex_constraint("person", "name")
graph.add_vertex_constraint("book", "title")
graph.add_vertex_constraint("author", "fullname")
graph.add_vertex_constraint("category", "name")

# add in a couple books and their authors
# Note that I am duplicating the books title property as a name too. This is for ruruki-eye display
programming = graph.get_or_create_vertex("category", name="Programming")
operating_systems = graph.get_or_create_vertex("category",
                                               name="Operating Systems")

python_crash_course = graph.get_or_create_vertex("book",
                                                 title="Python Crash Course")
graph.set_property(python_crash_course, name="Python Crash Course")
eric_matthes = graph.get_or_create_vertex("author",
                                          fullname="Eric Matthes",
                                          name="Eric",
                                          surname="Matthes")
graph.get_or_create_edge(python_crash_course, "CATEGORY", programming)
graph.get_or_create_edge(python_crash_course, "BY", eric_matthes)

python_pocket_ref = graph.get_or_create_vertex("book",
                                               title="Python Pocket Reference")
graph.set_property(python_pocket_ref, name="Python Pocket Reference")
Пример #15
0
 def __init__(self):
     '''
     Constructor
     '''
     self.graph = Graph()
Пример #16
0
"""
Scrape an ansable inventory and build a dependency graph.
"""
from ruruki.graphs import Graph

__all__ = ["GRAPH", "scrape_inventroy", "scrape_hosts", "scrape_playbook"]

GRAPH = Graph()
GRAPH.add_vertex_constraint("HOST", "name")
GRAPH.add_vertex_constraint("GROUP", "name")
GRAPH.add_vertex_constraint("PLAY", "name")
GRAPH.add_vertex_constraint("TASK", "name")


def _link_node_to_groups(node, groups, edge_label="HAS-GROUP"):
    """
    Create group node for all the groups provided and link the
    node to the group node.

    :param node: Node that is using the group.
    :type node: :class:`ruruki.interfaces.IVertex`
    :param groups: Iterable of groups that you are creating,
        inspecting and linking to the node.
    :type groups: :class:`ansible.inventroy.group.Group`
    :param edge_label: Edge label to use.
    :type edge_label: :class:`str`
    """
    for group in groups:
        if group.get_name() == "all":
            continue
Пример #17
0
#pylint: skip-file
from __future__ import print_function
from ruruki.graphs import Graph

# create a empty graph
graph = Graph()


# create some constraints to ensure uniqueness
graph.add_vertex_constraint("person", "name")
graph.add_vertex_constraint("book", "title")
graph.add_vertex_constraint("author", "fullname")
graph.add_vertex_constraint("category", "name")


# add in a couple books and their authors
# Note that I am duplicating the books title property as a name too. This is for ruruki-eye display
programming = graph.get_or_create_vertex("category", name="Programming")
operating_systems = graph.get_or_create_vertex("category", name="Operating Systems")


python_crash_course = graph.get_or_create_vertex("book", title="Python Crash Course")
graph.set_property(python_crash_course, name="Python Crash Course")
eric_matthes = graph.get_or_create_vertex("author", fullname="Eric Matthes", name="Eric", surname="Matthes")
graph.get_or_create_edge(python_crash_course, "CATEGORY", programming)
graph.get_or_create_edge(python_crash_course, "BY", eric_matthes)


python_pocket_ref = graph.get_or_create_vertex("book", title="Python Pocket Reference")
graph.set_property(python_pocket_ref, name="Python Pocket Reference")
mark_lutz = graph.get_or_create_vertex("author", fullname="Mark Lutz", name="Mark", surname="Lutz")
 def __init__(self, nbins):
     self.nbins = nbins
     self.graph = Graph()
     print("num of bins=" + str(self.nbins))
     print(self.graph)