summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/utils/console.py
blob: 81e8cf8e1467d0f74acdb3953c0f67606c272315 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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.

"""
Abstraction API above terminal color libraries.
"""

import os
import sys
from StringIO import StringIO

from contextlib import contextmanager

from ..cli import color
from . import formatting


_indent_string = ''


class TopologyStylizer(object):
    def __init__(self, indentation=0):
        self._str = StringIO()
        self._indentation = indentation

    def write(self, string):
        self._str.write(' ' * self._indentation)
        self._str.write(string)
        self._str.write(os.linesep)

    @contextmanager
    def indent(self, indentation=2):
        self._indentation += indentation
        yield
        self._indentation -= indentation

    @staticmethod
    def type_style(value):
        return Colored.blue(value, bold=True)

    @staticmethod
    def node_style(value):
        return Colored.red(value, bold=True)

    @staticmethod
    def property_style(value):
        return Colored.magenta(value, bold=True)

    @staticmethod
    def literal_style(value):
        return Colored.magenta(formatting.safe_repr(value))

    @staticmethod
    def required_style(value):
        return Colored.white(value)

    @staticmethod
    def meta_style(value):
        return Colored.green(value)

    def __str__(self):
        return self._str.getvalue()


def puts(string='', newline=True, stream=sys.stdout):
    stream.write(_indent_string)
    stream.write(formatting.safe_str(string))
    if newline:
        stream.write(os.linesep)


@contextmanager
def indent(size=4):
    global _indent_string
    original_indent_string = _indent_string
    try:
        _indent_string += ' ' * size
        yield
    finally:
        _indent_string = original_indent_string


class Colored(object):
    @staticmethod
    def black(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.BLACK, bold)

    @staticmethod
    def red(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.RED, bold)

    @staticmethod
    def green(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.GREEN, bold)

    @staticmethod
    def yellow(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.YELLOW, bold)

    @staticmethod
    def blue(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.BLUE, bold)

    @staticmethod
    def magenta(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.MAGENTA, bold)

    @staticmethod
    def cyan(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.CYAN, bold)

    @staticmethod
    def white(string, always=False, bold=False):
        return Colored._color(string, color.Colors.Fore.WHITE, bold)

    @staticmethod
    def _color(string, fore, bold):
        return color.StringStylizer(string, color.ColorSpec(
            fore=fore,
            style=color.Colors.Style.BRIGHT if bold else color.Colors.Style.NORMAL))