summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/parser/consumption/consumer.py
blob: 878a1618cb33ec2a551e5454945eb49acbbf79e0 (plain)
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
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
82
83
84
85
86
87
88
89
90
91
92
93
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from ...exceptions import AriaException
from ...utils.exceptions import print_exception
from ..validation import Issue


class Consumer(object):
    """
    Base class for ARIA consumers.

    Consumers provide useful functionality by consuming presentations.
    """

    def __init__(self, context):
        from ...orchestrator import topology

        self.topology = topology.Topology()
        self.context = context

    def consume(self):
        pass

    def dump(self):
        pass

    def _handle_exception(self, e):
        if hasattr(e, 'issue') and isinstance(e.issue, Issue):
            self.context.validation.report(issue=e.issue)
        else:
            self.context.validation.report(exception=e)
        if not isinstance(e, AriaException):
            print_exception(e)


class ConsumerChain(Consumer):
    """
    ARIA consumer chain.

    Calls consumers in order, handling exception by calling ``_handle_exception`` on them, and stops
    the chain if there are any validation issues.
    """

    def __init__(self, context, consumer_classes=None, handle_exceptions=True):
        super(ConsumerChain, self).__init__(context)
        self.handle_exceptions = handle_exceptions
        self.consumers = []
        if consumer_classes:
            for consumer_class in consumer_classes:
                self.append(consumer_class)

    def append(self, *consumer_classes):
        for consumer_class in consumer_classes:
            self.consumers.append(consumer_class(self.context))

    def consume(self):
        for consumer in self.consumers:
            try:
                consumer.consume()
            except BaseException as e:
                if self.handle_exceptions:
                    handle_exception(consumer, e)
                else:
                    raise e

            if consumer.topology.has_issues:
                self.context.validation.extend_issues(consumer.topology.issues)

            if self.context.validation.has_issues:
                break


def handle_exception(consumer, e):
    if isinstance(e, AriaException) and e.issue:
        consumer.context.validation.report(issue=e.issue)
    else:
        consumer.context.validation.report(exception=e)
    if not isinstance(e, AriaException):
        print_exception(e)