vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / consumption / context.py
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 import sys
17 import threading
18
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
25
26
27 _thread_locals = threading.local()
28
29
30 class ConsumptionContext(object):
31     """
32     Consumption context.
33
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
37     :vartype style: 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`
48     """
49
50     @staticmethod
51     def get_thread_local():
52         """
53         Gets the context attached to the current thread if there is one.
54         """
55
56         return getattr(_thread_locals, 'aria_consumption_context', None)
57
58     def __init__(self, set_thread_local=True):
59         self.args = []
60         self.out = sys.stdout
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()
67
68         if set_thread_local:
69             self.set_thread_local()
70
71     def set_thread_local(self):
72         """
73         Attaches this context to the current thread.
74         """
75
76         _thread_locals.aria_consumption_context = self
77
78     def write(self, string):
79         """
80         Writes to our ``out``, making sure to encode UTF-8 if required.
81         """
82
83         try:
84             self.out.write(string)
85         except UnicodeEncodeError:
86             self.out.write(string.encode('utf8'))
87
88     def has_arg_switch(self, name):
89         name = '--%s' % name
90         return name in self.args
91
92     def get_arg_value(self, name, default=None):
93         name = '--%s=' % name
94         for arg in self.args:
95             if arg.startswith(name):
96                 return arg[len(name):]
97         return default
98
99     def get_arg_value_int(self, name, default=None):
100         value = self.get_arg_value(name)
101         if value is not None:
102             try:
103                 return int(value)
104             except (TypeError, ValueError):
105                 pass
106         return default