vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / utils / console.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 """
17 Abstraction API above terminal color libraries.
18 """
19
20 import os
21 import sys
22 from StringIO import StringIO
23
24 from contextlib import contextmanager
25
26 from ..cli import color
27 from . import formatting
28
29
30 _indent_string = ''
31
32
33 class TopologyStylizer(object):
34     def __init__(self, indentation=0):
35         self._str = StringIO()
36         self._indentation = indentation
37
38     def write(self, string):
39         self._str.write(' ' * self._indentation)
40         self._str.write(string)
41         self._str.write(os.linesep)
42
43     @contextmanager
44     def indent(self, indentation=2):
45         self._indentation += indentation
46         yield
47         self._indentation -= indentation
48
49     @staticmethod
50     def type_style(value):
51         return Colored.blue(value, bold=True)
52
53     @staticmethod
54     def node_style(value):
55         return Colored.red(value, bold=True)
56
57     @staticmethod
58     def property_style(value):
59         return Colored.magenta(value, bold=True)
60
61     @staticmethod
62     def literal_style(value):
63         return Colored.magenta(formatting.safe_repr(value))
64
65     @staticmethod
66     def required_style(value):
67         return Colored.white(value)
68
69     @staticmethod
70     def meta_style(value):
71         return Colored.green(value)
72
73     def __str__(self):
74         return self._str.getvalue()
75
76
77 def puts(string='', newline=True, stream=sys.stdout):
78     stream.write(_indent_string)
79     stream.write(formatting.safe_str(string))
80     if newline:
81         stream.write(os.linesep)
82
83
84 @contextmanager
85 def indent(size=4):
86     global _indent_string
87     original_indent_string = _indent_string
88     try:
89         _indent_string += ' ' * size
90         yield
91     finally:
92         _indent_string = original_indent_string
93
94
95 class Colored(object):
96     @staticmethod
97     def black(string, always=False, bold=False):
98         return Colored._color(string, color.Colors.Fore.BLACK, bold)
99
100     @staticmethod
101     def red(string, always=False, bold=False):
102         return Colored._color(string, color.Colors.Fore.RED, bold)
103
104     @staticmethod
105     def green(string, always=False, bold=False):
106         return Colored._color(string, color.Colors.Fore.GREEN, bold)
107
108     @staticmethod
109     def yellow(string, always=False, bold=False):
110         return Colored._color(string, color.Colors.Fore.YELLOW, bold)
111
112     @staticmethod
113     def blue(string, always=False, bold=False):
114         return Colored._color(string, color.Colors.Fore.BLUE, bold)
115
116     @staticmethod
117     def magenta(string, always=False, bold=False):
118         return Colored._color(string, color.Colors.Fore.MAGENTA, bold)
119
120     @staticmethod
121     def cyan(string, always=False, bold=False):
122         return Colored._color(string, color.Colors.Fore.CYAN, bold)
123
124     @staticmethod
125     def white(string, always=False, bold=False):
126         return Colored._color(string, color.Colors.Fore.WHITE, bold)
127
128     @staticmethod
129     def _color(string, fore, bold):
130         return color.StringStylizer(string, color.ColorSpec(
131             fore=fore,
132             style=color.Colors.Style.BRIGHT if bold else color.Colors.Style.NORMAL))