1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from ...exceptions import AriaException
18 from ...utils.exceptions import print_exception
19 from ..validation import Issue
22 class Consumer(object):
24 Base class for ARIA consumers.
26 Consumers provide useful functionality by consuming presentations.
29 def __init__(self, context):
30 from ...orchestrator import topology
32 self.topology = topology.Topology()
33 self.context = context
41 def _handle_exception(self, e):
42 if hasattr(e, 'issue') and isinstance(e.issue, Issue):
43 self.context.validation.report(issue=e.issue)
45 self.context.validation.report(exception=e)
46 if not isinstance(e, AriaException):
50 class ConsumerChain(Consumer):
54 Calls consumers in order, handling exception by calling ``_handle_exception`` on them, and stops
55 the chain if there are any validation issues.
58 def __init__(self, context, consumer_classes=None, handle_exceptions=True):
59 super(ConsumerChain, self).__init__(context)
60 self.handle_exceptions = handle_exceptions
63 for consumer_class in consumer_classes:
64 self.append(consumer_class)
66 def append(self, *consumer_classes):
67 for consumer_class in consumer_classes:
68 self.consumers.append(consumer_class(self.context))
71 for consumer in self.consumers:
74 except BaseException as e:
75 if self.handle_exceptions:
76 handle_exception(consumer, e)
80 if consumer.topology.has_issues:
81 self.context.validation.extend_issues(consumer.topology.issues)
83 if self.context.validation.has_issues:
87 def handle_exception(consumer, e):
88 if isinstance(e, AriaException) and e.issue:
89 consumer.context.validation.report(issue=e.issue)
91 consumer.context.validation.report(exception=e)
92 if not isinstance(e, AriaException):