]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/mgr_util.py
16 COLOR_SEQ
= "\033[1;%dm"
17 COLOR_DARK_SEQ
= "\033[0;%dm"
19 UNDERLINE_SEQ
= "\033[4m"
22 def colorize(msg
, color
, dark
=False):
24 Decorate `msg` with escape sequences to give the requested color
26 return (COLOR_DARK_SEQ
if dark
else COLOR_SEQ
) % (30 + color
) \
32 Decorate `msg` with escape sequences to make it appear bold
34 return BOLD_SEQ
+ msg
+ RESET_SEQ
37 def format_units(n
, width
, colored
, decimal
):
39 Format a number without units, so as to fit into `width` characters, substituting
40 an appropriate unit suffix.
42 Use decimal for dimensionless things, use base 2 (decimal=False) for byte sizes/rates.
45 factor
= 1000 if decimal
else 1024
46 units
= [' ', 'k', 'M', 'G', 'T', 'P', 'E']
48 while len("%s" % (int(n
) // (factor
**unit
))) > width
- 1:
52 truncated_float
= ("%f" % (n
/ (float(factor
) ** unit
)))[0:width
- 1]
53 if truncated_float
[-1] == '.':
54 truncated_float
= " " + truncated_float
[0:-1]
56 truncated_float
= "%{wid}d".format(wid
=width
- 1) % n
57 formatted
= "%s%s" % (truncated_float
, units
[unit
])
64 return bold(colorize(formatted
[0:-1], color
[0], color
[1])) \
65 + bold(colorize(formatted
[-1], BLACK
, False))
70 def format_dimless(n
, width
, colored
=True):
71 return format_units(n
, width
, colored
, decimal
=True)
74 def format_bytes(n
, width
, colored
=True):
75 return format_units(n
, width
, colored
, decimal
=False)
78 def merge_dicts(*args
):
79 # type: (dict) -> dict
81 >>> assert merge_dicts({1:2}, {3:4}) == {1:2, 3:4}
82 You can also overwrite keys:
83 >>> assert merge_dicts({1:2}, {1:4}) == {1:4}
84 :rtype: dict[str, Any]
92 def get_default_addr():
93 def is_ipv6_enabled():
95 sock
= socket
.socket(socket
.AF_INET6
)
96 with contextlib
.closing(sock
):
99 except (AttributeError, socket
.error
) as e
:
103 return get_default_addr
.result
104 except AttributeError:
105 result
= '::' if is_ipv6_enabled() else '0.0.0.0'
106 get_default_addr
.result
= result