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.
19 from ...utils import console
20 from ..validation import ValidationContext
21 from ..loading import LoadingContext
22 from ..reading import ReadingContext
23 from ..presentation import PresentationContext
24 from ..modeling import ModelingContext
27 _thread_locals = threading.local()
30 class ConsumptionContext(object):
34 :ivar args: runtime arguments (usually provided on the command line)
35 :ivar out: message output stream (defaults to stdout)
36 :ivar style: message output style
38 :ivar validation: validation context
39 :vartype validation: :class:`ValidationContext`
40 :ivar loading: loading context
41 :vartype loading: :class:`LoadingContext`
42 :ivar reading: reading context
43 :vartype reading: :class:`ReadingContext`
44 :ivar presentation: presentation context
45 :vartype presentation: :class:`PresentationContext`
46 :ivar modeling: modeling context
47 :vartype modeling: :class:`ModelingContext`
51 def get_thread_local():
53 Gets the context attached to the current thread if there is one.
56 return getattr(_thread_locals, 'aria_consumption_context', None)
58 def __init__(self, set_thread_local=True):
61 self.validation = ValidationContext()
62 self.loading = LoadingContext()
63 self.reading = ReadingContext()
64 self.presentation = PresentationContext()
65 self.modeling = ModelingContext()
66 self.style = console.TopologyStylizer()
69 self.set_thread_local()
71 def set_thread_local(self):
73 Attaches this context to the current thread.
76 _thread_locals.aria_consumption_context = self
78 def write(self, string):
80 Writes to our ``out``, making sure to encode UTF-8 if required.
84 self.out.write(string)
85 except UnicodeEncodeError:
86 self.out.write(string.encode('utf8'))
88 def has_arg_switch(self, name):
90 return name in self.args
92 def get_arg_value(self, name, default=None):
95 if arg.startswith(name):
96 return arg[len(name):]
99 def get_arg_value_int(self, name, default=None):
100 value = self.get_arg_value(name)
101 if value is not None:
104 except (TypeError, ValueError):