Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-volume / ceph_volume / terminal.py
1 import sys
2
3
4 class colorize(str):
5     """
6     Pretty simple to use::
7
8         colorize.make('foo').bold
9         colorize.make('foo').green
10         colorize.make('foo').yellow
11         colorize.make('foo').red
12         colorize.make('foo').blue
13
14     Otherwise you could go the long way (for example if you are
15     testing this class)::
16
17         string = colorize('foo')
18         string._set_attributes()
19         string.red
20
21     """
22
23     def __init__(self, string):
24         self.stdout = sys.__stdout__
25         self.appends = ''
26         self.prepends = ''
27         self.isatty = self.stdout.isatty()
28
29     def _set_attributes(self):
30         """
31         Sets the attributes here because the str class does not
32         allow to pass in anything other than a string to the constructor
33         so we can't really mess with the other attributes.
34         """
35         for k, v in self.__colors__.items():
36             setattr(self, k, self.make_color(v))
37
38     def make_color(self, color):
39         if not self.isatty:
40             return self
41         return color + self + '\033[0m' + self.appends
42
43     @property
44     def __colors__(self):
45         return dict(
46             blue='\033[34m',
47             green='\033[92m',
48             yellow='\033[33m',
49             red='\033[91m',
50             bold='\033[1m',
51             ends='\033[0m'
52         )
53
54     @classmethod
55     def make(cls, string):
56         """
57         A helper method to return itself and workaround the fact that
58         the str object doesn't allow extra arguments passed in to the
59         constructor
60         """
61         obj = cls(string)
62         obj._set_attributes()
63         return obj
64
65 #
66 # Common string manipulations
67 #
68 yellow = lambda x: colorize.make(x).yellow  # noqa
69 blue = lambda x: colorize.make(x).blue  # noqa
70 green = lambda x: colorize.make(x).green  # noqa
71 red = lambda x: colorize.make(x).red  # noqa
72 bold = lambda x: colorize.make(x).bold  # noqa
73 red_arrow = red('--> ')
74 blue_arrow = blue('--> ')
75 green_arrow = green('--> ')
76 yellow_arrow = yellow('--> ')
77
78
79 class _Write(object):
80
81     def __init__(self, _writer=None, prefix='', suffix='', flush=False):
82         self._writer = _writer or sys.stdout
83         self.suffix = suffix
84         self.prefix = prefix
85         self.flush = flush
86
87     def bold(self, string):
88         self.write(bold(string))
89
90     def raw(self, string):
91         if not string.endswith('\n'):
92             string = '%s\n' % string
93         self.write(string)
94
95     def write(self, line):
96         self._writer.write(self.prefix + line + self.suffix)
97         if self.flush:
98             self._writer.flush()
99
100
101 def stdout(msg):
102     return _Write(prefix=blue(' stdout: ')).raw(msg)
103
104
105 def stderr(msg):
106     return _Write(prefix=yellow(' stderr: ')).raw(msg)
107
108
109 def write(msg):
110     return _Write().raw(msg)
111
112
113 def error(msg):
114     return _Write(prefix=red_arrow).raw(msg)
115
116
117 def warning(msg):
118     return _Write(prefix=yellow_arrow).raw(msg)
119
120
121 def success(msg):
122     return _Write(prefix=green_arrow).raw(msg)
123
124
125 def dispatch(mapper, argv=None):
126     argv = argv or sys.argv
127     for count, arg in enumerate(argv, 1):
128         if arg in mapper.keys():
129             instance = mapper.get(arg)(argv[count:])
130             if hasattr(instance, 'main'):
131                 instance.main()
132                 raise SystemExit(0)
133
134
135 def subhelp(mapper):
136     """
137     Look at every value of every key in the mapper and will output any
138     ``class.help`` possible to return it as a string that will be sent to
139     stdout.
140     """
141     help_text_lines = []
142     for key, value in mapper.items():
143         try:
144             help_text = value.help
145         except AttributeError:
146             continue
147         help_text_lines.append("%-24s %s" % (key, help_text))
148
149     if help_text_lines:
150         return "Available subcommands:\n\n%s" % '\n'.join(help_text_lines)
151     return ''