Merge pull request #21962 from rallytime/apply_nested_outputter_fix_to_2014.7

Apply nested outputter optimization fix to 2014.7
This commit is contained in:
jfindlay 2015-03-24 22:02:11 -06:00
commit dd37f431fb

View File

@ -25,11 +25,10 @@ Example output::
'''
# Import python libs
from numbers import Number
import re
# Import salt libs
import salt.utils
import salt.output
from salt.utils import get_colors, sdecode
from salt._compat import string_types
@ -39,6 +38,12 @@ class NestDisplay(object):
'''
def __init__(self):
self.colors = salt.utils.get_colors(__opts__.get('color'))
self.__dict__.update(
get_colors(
__opts__.get('color')
)
)
self.strip_colors = __opts__.get('strip_colors', True)
def ustring(self,
indent,
@ -48,69 +53,87 @@ class NestDisplay(object):
suffix='',
endc=None):
if endc is None:
endc = self.colors['ENDC']
endc = self.ENDC
indent *= ' '
fmt = u'{0}{1}{2}{3}{4}{5}'
try:
return u'{0}{1}{2}{3}{4}{5}\n'.format(
indent, color, prefix, msg, endc, suffix)
return fmt.format(indent, color, prefix, msg, endc, suffix)
except UnicodeDecodeError:
return u'{0}{1}{2}{3}{4}{5}\n'.format(
indent, color, prefix, salt.utils.sdecode(msg), endc, suffix)
return fmt.format(indent, color, prefix, sdecode(msg), endc, suffix)
def display(self, ret, indent, prefix, out):
'''
Recursively iterate down through data structures to determine output
'''
strip_colors = __opts__.get('strip_colors', True)
if ret is None or ret is True or ret is False:
out += self.ustring(
' ' * indent,
self.colors['YELLOW'],
ret,
prefix=prefix)
out.append(
self.ustring(
indent,
self.YELLOW,
ret,
prefix=prefix
)
)
# Number includes all python numbers types
# (float, int, long, complex, ...)
elif isinstance(ret, Number):
out += self.ustring(
' ' * indent,
self.colors['YELLOW'],
ret,
prefix=prefix)
out.append(
self.ustring(
indent,
self.YELLOW,
ret,
prefix=prefix
)
)
elif isinstance(ret, string_types):
lines = re.split(r'\r?\n', ret)
for line in lines:
if strip_colors:
for line in ret.splitlines():
if self.strip_colors:
line = salt.output.strip_esc_sequence(line)
out += self.ustring(
' ' * indent,
self.colors['GREEN'],
line,
prefix=prefix)
elif isinstance(ret, list) or isinstance(ret, tuple):
out.append(
self.ustring(
indent,
self.GREEN,
line,
prefix=prefix
)
)
elif isinstance(ret, (list, tuple)):
for ind in ret:
if isinstance(ind, (list, tuple, dict)):
out += self.ustring(' ' * indent,
self.colors['GREEN'],
'|_')
out.append(
self.ustring(
indent,
self.GREEN,
'|_'
)
)
prefix = '' if isinstance(ind, dict) else '- '
out = self.display(ind, indent + 2, prefix, out)
self.display(ind, indent + 2, prefix, out)
else:
out = self.display(ind, indent, '- ', out)
self.display(ind, indent, '- ', out)
elif isinstance(ret, dict):
if indent:
out += self.ustring(
' ' * indent,
self.colors['CYAN'],
'-' * 10)
out.append(
self.ustring(
indent,
self.CYAN,
'----------'
)
)
for key in sorted(ret):
val = ret[key]
out += self.ustring(
' ' * indent,
self.colors['CYAN'],
key,
suffix=":",
prefix=prefix)
out = self.display(val, indent + 4, '', out)
out.append(
self.ustring(
indent,
self.CYAN,
key,
suffix=':',
prefix=prefix
)
)
self.display(val, indent + 4, '', out)
return out
@ -119,4 +142,6 @@ def output(ret):
Display ret data
'''
nest = NestDisplay()
return nest.display(ret, __opts__.get('nested_indent', 0), '', '')
return '\n'.join(
nest.display(ret, __opts__.get('nested_indent', 0), '', [])
)