Completed
Push — master ( f161ca...d2dc5c )
by Kenny
01:12
created

Plugin.__repr__()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""Abstract classes that define the reader and writer plugin base classes.
3
4
.. moduleauthor:: Kenny Freeman <[email protected]>
5
6
"""
7
__author__ = 'Kenny Freeman'
8
__email__ = '[email protected]'
9
__license__ = "ISCL"
10
__docformat__ = 'reStructuredText'
11
12
from abc import ABCMeta, abstractmethod
13
14
15
class Plugin(object):
16
    """The base class for all plugins.
17
18
    :param config: an instance of plumd.conf configuration helper
19
    :type config: conf
20
    """
21
    __metaclass__ = ABCMeta
22
    defaults = {}
23
24
    def __init__(self, log, config):
25
        """reader abstract class constructor - each plugin must sub class.
26
27
        :param config: an instance of plumd.conf configuration helper
28
        :type config: conf
29
        """
30
        self.config = config
31
        self.log = log
32
        # plugins can set their own defaults byt defining self.defaults
33
        self.config.defaults(self.defaults)
34
35
    def __str__(self):
36
        """Return a human readable str.
37
        :rtype: str"""
38
        s = "Plugin(class={1})".format(self.__class__.__name__)
39
        return s
40
41
42
    def __repr__(self):
43
        """Return a human readable str.
44
        :rtype: str"""
45
        s = "Plugin(class={1})".format(self.__class__.__name__)
46
        return s
47
48
49
    def onstart(self):
50
        """onstart() is called before the first call to push()."""
51
        self.log.debug("Plugin: onstart: {0}".format(self.config.get('name')))
52
        pass
53
54
55
    def onstop(self):
56
        """onstop() is called just before the main process exits."""
57
        self.log.debug("Plugin: onstop: {0}".format(self.config.get('name')))
58
        pass
59
60
61
    def onreload(self):
62
        """onreload() is called to reload configuration at runtime."""
63
        raise NotImplementedError("todo")
64
65
66
class Reader(Plugin):
67
    """An abstract base class that all writer pluguns subclass."""
68
    __metaclass__ = ABCMeta
69
70
    @abstractmethod
71
    def poll(self):
72
        """Reader plugins must define this method, it measures and returns a
73
        metrics string.
74
75
        format <name:str> <value:float|int> <timestamp:time.time()>\n[...]
76
        set to change to a python object.
77
78
        :rtype: str
79
        """
80
        # this will never get called
81
        raise NotImplementedError("poll() not defined")
82
83
84
class Writer(Plugin):
85
    """An abstract base class that all writer pluguns subclass."""
86
    __metaclass__ = ABCMeta
87
88
    @abstractmethod
89
    def push(self, metrics):
90
        """Writer plugins must define this method, it accepts a metrics string
91
        and sends to its backend(s).
92
93
        :param metrics: metrics collection from reader poll() call.
94
95
        :type metrics: str
96
        """
97
        # this will never get called
98
        raise NotImplementedError("push() not defined")
99