kytos.core.events   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 72
rs 10
c 0
b 0
f 0
ccs 23
cts 23
cp 1
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A KytosEvent.__init__() 0 11 2
A KytosEvent.source() 0 4 1
A KytosEvent.destination() 0 4 1
A KytosEvent.set_destination() 0 7 1
A KytosEvent.message() 0 15 2
A KytosEvent.__str__() 0 2 1
A KytosEvent.set_source() 0 7 1
A KytosEvent.__repr__() 0 2 1
1
"""Module with Kytos Events."""
2
3 1
from kytos.core.helpers import now
4
5
6 1
class KytosEvent:
7
    """Base Event class.
8
9
    The event data will be passed in the `content` attribute, which should be a
10
    dictionary.
11
    """
12
13 1
    def __init__(self, name=None, content=None):
14
        """Create an event to be published.
15
16
        Args:
17
            name (string): The name of the event. You should prepend it with
18
                           the name of the napp.
19
            content (dict): Dictionary with any extra data for the event.
20
        """
21 1
        self.name = name
22 1
        self.content = content if content is not None else {}
23 1
        self.timestamp = now()
24
25 1
    def __str__(self):
26 1
        return self.name
27
28 1
    def __repr__(self):
29 1
        return f"KytosEvent({self.name!r}, {self.content!r})"
30
31 1
    @property
32
    def destination(self):
33
        """Return the destination of KytosEvent."""
34 1
        return self.content.get('destination')
35
36 1
    def set_destination(self, destination):
37
        """Update the destination of KytosEvent.
38
39
        Args:
40
            destination (string): destination of KytosEvent.
41
        """
42 1
        self.content['destination'] = destination
43
44 1
    @property
45
    def source(self):
46
        """Return the source of KytosEvent."""
47 1
        return self.content.get('source')
48
49 1
    def set_source(self, source):
50
        """Update the source of KytosEvent.
51
52
        Args:
53
            source (string): source of KytosEvent.
54
        """
55 1
        self.content['source'] = source
56
57 1
    @property
58
    def message(self):
59
        """Return the message carried by the event if it exists.
60
61
        If there is any OpenFlow message on the event it'll be stored on
62
        the 'message' key of the 'content' attribute.
63
64
        Returns:
65
            A python-openflow message instance if it exists, None otherwise.
66
67
        """
68 1
        try:
69 1
            return self.content['message']
70 1
        except KeyError:
71
            return None
72