]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/terminal.py
5 terminal_logger
= logging
.getLogger('terminal')
10 Pretty simple to use::
12 colorize.make('foo').bold
13 colorize.make('foo').green
14 colorize.make('foo').yellow
15 colorize.make('foo').red
16 colorize.make('foo').blue
18 Otherwise you could go the long way (for example if you are
21 string = colorize('foo')
22 string._set_attributes()
27 def __init__(self
, string
):
30 self
.isatty
= sys
.__stderr
__.isatty()
32 def _set_attributes(self
):
34 Sets the attributes here because the str class does not
35 allow to pass in anything other than a string to the constructor
36 so we can't really mess with the other attributes.
38 for k
, v
in self
.__colors
__.items():
39 setattr(self
, k
, self
.make_color(v
))
41 def make_color(self
, color
):
44 return color
+ self
+ '\033[0m' + self
.appends
58 def make(cls
, string
):
60 A helper method to return itself and workaround the fact that
61 the str object doesn't allow extra arguments passed in to the
69 # Common string manipulations
71 yellow
= lambda x
: colorize
.make(x
).yellow
# noqa
72 blue
= lambda x
: colorize
.make(x
).blue
# noqa
73 green
= lambda x
: colorize
.make(x
).green
# noqa
74 red
= lambda x
: colorize
.make(x
).red
# noqa
75 bold
= lambda x
: colorize
.make(x
).bold
# noqa
76 red_arrow
= red('--> ')
77 blue_arrow
= blue('--> ')
78 green_arrow
= green('--> ')
79 yellow_arrow
= yellow('--> ')
84 def __init__(self
, _writer
=None, prefix
='', suffix
='', flush
=False):
85 # we can't set sys.stderr as the default for _writer. otherwise
86 # pytest's capturing gets confused
87 self
._writer
= _writer
or sys
.stderr
92 def bold(self
, string
):
93 self
.write(bold(string
))
95 def raw(self
, string
):
96 if not string
.endswith('\n'):
97 string
= '%s\n' % string
100 def write(self
, line
):
101 entry
= self
.prefix
+ line
+ self
.suffix
104 self
._writer
.write(entry
)
107 except (UnicodeDecodeError, UnicodeEncodeError):
109 terminal_logger
.info(entry
.strip('\n'))
110 except (AttributeError, TypeError):
111 terminal_logger
.info(entry
)
115 return _Write(prefix
=blue(' stdout: ')).raw(msg
)
119 return _Write(prefix
=yellow(' stderr: ')).raw(msg
)
123 return _Write().raw(msg
)
127 return _Write(prefix
=red_arrow
).raw(msg
)
131 return _Write(prefix
=blue_arrow
).raw(msg
)
135 return _Write(prefix
=blue_arrow
).raw(msg
)
139 return _Write(prefix
=yellow_arrow
).raw(msg
)
143 return _Write(prefix
=green_arrow
).raw(msg
)
146 class MultiLogger(object):
148 Proxy class to be able to report on both logger instances and terminal
149 messages avoiding the issue of having to call them both separately
151 Initialize it in the same way a logger object::
153 logger = terminal.MultiLogger(__name__)
156 def __init__(self
, name
):
157 self
.logger
= logging
.getLogger(name
)
159 def _make_record(self
, msg
, *args
):
164 self
.logger
.exception('unable to produce log record: %s' % msg
)
167 def warning(self
, msg
, *args
):
168 record
= self
._make
_record
(msg
, *args
)
170 self
.logger
.warning(record
)
172 def debug(self
, msg
, *args
):
173 record
= self
._make
_record
(msg
, *args
)
175 self
.logger
.debug(record
)
177 def info(self
, msg
, *args
):
178 record
= self
._make
_record
(msg
, *args
)
180 self
.logger
.info(record
)
182 def error(self
, msg
, *args
):
183 record
= self
._make
_record
(msg
, *args
)
185 self
.logger
.error(record
)
188 def dispatch(mapper
, argv
=None):
189 argv
= argv
or sys
.argv
190 for count
, arg
in enumerate(argv
, 1):
191 if arg
in mapper
.keys():
192 instance
= mapper
.get(arg
)(argv
[count
:])
193 if hasattr(instance
, 'main'):
200 Look at every value of every key in the mapper and will output any
201 ``class.help`` possible to return it as a string that will be sent to
205 for key
, value
in mapper
.items():
207 help_text
= value
.help
208 except AttributeError:
210 help_text_lines
.append("%-24s %s" % (key
, help_text
))
213 return "Available subcommands:\n\n%s" % '\n'.join(help_text_lines
)