AbstractActor.py 3.06 KB
Newer Older
Olof Svensson's avatar
Olof Svensson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# Copyright (c) European Synchrotron Radiation Facility (ESRF)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__authors__ = ["O. Svensson"]
__license__ = "MIT"
__date__ = "28/05/2019"

import logging
27
28
29
from pypushflow.addon import utils
from pypushflow.addon.classes import BaseActorAddOn

Operator for ID30's avatar
Operator for ID30 committed
30

Olof Svensson's avatar
Olof Svensson committed
31
32
33
34
logger = logging.getLogger('pypushflow')


class AbstractActor(object):
35
36
37
    """
    TODO
    """
Operator for ID30's avatar
Operator for ID30 committed
38
    def __init__(self, parent=None, name=None):
Olof Svensson's avatar
Olof Svensson committed
39
40
41
42
        if name is None:
            raise RuntimeError('Actor name is None!')
        self.name = name
        self.listDownStreamActor = []
Operator for ID30's avatar
Operator for ID30 committed
43
44
        self.parent = parent
        self.actorId = None
Olof Svensson's avatar
Olof Svensson committed
45

46
47
48
49
        self._add_ons = []
        for add_on_class in self._getAddOnsClasses():
            self._add_ons.append(add_on_class())

Olof Svensson's avatar
Olof Svensson committed
50
51
52
53
54
55
56
57
58
59
60
    def connect(self, actor):
        logger.debug('Connecting actor "{0}" to actor "{1}"'.format(
            self.name, actor.name
        ))
        self.listDownStreamActor.append(actor)

    def trigger(self, inData):
        for actor in self.listDownStreamActor:
            logger.debug('In actor "{0}", triggering actor "{1}"'.format(
                self.name, actor.name
            ))
61
            self._process_pre_trigger_add_on(inData)
Olof Svensson's avatar
Olof Svensson committed
62
            actor.trigger(inData)
63
            self._process_post_trigger_add_on()
Operator for ID30's avatar
Operator for ID30 committed
64

Operator for ID30's avatar
Operator for ID30 committed
65
66
    def getActorPath(self):
        return self.parent.getActorPath()
payno's avatar
payno committed
67
68
69
70

    def get_addons(self):
        """Return the list of add-on that will be apply to this actor"""
        raise NotImplementedError()
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

    def _process_post_trigger_add_on(self):
        for add_on in self._add_ons:
            add_on.post_trigger_action(actor=self)

    def _process_pre_trigger_add_on(self, in_data):
        for add_on in self._add_ons:
            add_on.pre_trigger_action(actor=self, in_data=in_data)

    def _getAddOnsClasses(self):
        add_ons = []
        for _, classes in utils.get_registered_add_ons_classes().items():
            for class_ in classes:
                import inspect
                if BaseActorAddOn in (inspect.getmro(class_)):
                    add_ons.append(class_)
        return add_ons