]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/terminal.py
9 colorize.make('foo').bold
10 colorize.make('foo').green
11 colorize.make('foo').yellow
12 colorize.make('foo').red
13 colorize.make('foo').blue
15 Otherwise you could go the long way (for example if you are
18 string = colorize('foo')
19 string._set_attributes()
24 def __init__(self
, string
):
25 self
.stdout
= sys
.__stdout
__
28 self
.isatty
= self
.stdout
.isatty()
30 def _set_attributes(self
):
32 Sets the attributes here because the str class does not
33 allow to pass in anything other than a string to the constructor
34 so we can't really mess with the other attributes.
36 for k
, v
in self
.__colors
__.items():
37 setattr(self
, k
, self
.make_color(v
))
39 def make_color(self
, color
):
42 return color
+ self
+ '\033[0m' + self
.appends
56 def make(cls
, string
):
58 A helper method to return itself and workaround the fact that
59 the str object doesn't allow extra arguments passed in to the
67 # Common string manipulations
69 yellow
= lambda x
: colorize
.make(x
).yellow
# noqa
70 blue
= lambda x
: colorize
.make(x
).blue
# noqa
71 green
= lambda x
: colorize
.make(x
).green
# noqa
72 red
= lambda x
: colorize
.make(x
).red
# noqa
73 bold
= lambda x
: colorize
.make(x
).bold
# noqa
74 red_arrow
= red('--> ')
75 blue_arrow
= blue('--> ')
76 green_arrow
= green('--> ')
77 yellow_arrow
= yellow('--> ')
82 def __init__(self
, _writer
=None, prefix
='', suffix
='', flush
=False):
83 self
._writer
= _writer
or sys
.stdout
88 def bold(self
, string
):
89 self
.write(bold(string
))
91 def raw(self
, string
):
92 if not string
.endswith('\n'):
93 string
= '%s\n' % string
96 def write(self
, line
):
97 self
._writer
.write(self
.prefix
+ line
+ self
.suffix
)
103 return _Write(prefix
=blue(' stdout: ')).raw(msg
)
107 return _Write(prefix
=yellow(' stderr: ')).raw(msg
)
111 return _Write().raw(msg
)
115 return _Write(prefix
=red_arrow
).raw(msg
)
119 return _Write(prefix
=blue_arrow
).raw(msg
)
123 return _Write(prefix
=blue_arrow
).raw(msg
)
127 return _Write(prefix
=yellow_arrow
).raw(msg
)
131 return _Write(prefix
=green_arrow
).raw(msg
)
134 class MultiLogger(object):
136 Proxy class to be able to report on both logger instances and terminal
137 messages avoiding the issue of having to call them both separately
139 Initialize it in the same way a logger object::
141 logger = terminal.MultiLogger(__name__)
144 def __init__(self
, name
):
145 self
.logger
= logging
.getLogger(name
)
147 def _make_record(self
, msg
, *args
):
152 self
.logger
.exception('unable to produce log record: %s' % msg
)
155 def warning(self
, msg
, *args
):
156 record
= self
._make
_record
(msg
, *args
)
158 self
.logger
.warning(record
)
160 def debug(self
, msg
, *args
):
161 record
= self
._make
_record
(msg
, *args
)
163 self
.logger
.debug(record
)
165 def info(self
, msg
, *args
):
166 record
= self
._make
_record
(msg
, *args
)
168 self
.logger
.info(record
)
170 def error(self
, msg
, *args
):
171 record
= self
._make
_record
(msg
, *args
)
173 self
.logger
.error(record
)
176 def dispatch(mapper
, argv
=None):
177 argv
= argv
or sys
.argv
178 for count
, arg
in enumerate(argv
, 1):
179 if arg
in mapper
.keys():
180 instance
= mapper
.get(arg
)(argv
[count
:])
181 if hasattr(instance
, 'main'):
188 Look at every value of every key in the mapper and will output any
189 ``class.help`` possible to return it as a string that will be sent to
193 for key
, value
in mapper
.items():
195 help_text
= value
.help
196 except AttributeError:
198 help_text_lines
.append("%-24s %s" % (key
, help_text
))
201 return "Available subcommands:\n\n%s" % '\n'.join(help_text_lines
)