Merge pull request #19165 from thatch45/color_themes

Color themes
This commit is contained in:
Thomas S Hatch 2014-12-28 12:57:54 -07:00
commit 2e24a4fdaa
7 changed files with 42 additions and 7 deletions

View File

@ -31,7 +31,7 @@ def output(grains):
'''
Output the grains in a clean way
'''
colors = salt.utils.get_colors(__opts__.get('color'))
colors = salt.utils.get_colors(__opts__.get('color'), __opts__.get('color_theme'))
encoding = grains['locale_info']['defaultencoding']
if encoding == 'unknown':
encoding = 'utf-8' # let's hope for the best

View File

@ -79,7 +79,9 @@ def output(data):
def _format_host(host, data):
colors = salt.utils.get_colors(__opts__.get('color'))
colors = salt.utils.get_colors(
__opts__.get('color'),
__opts__('color_theme'))
tabular = __opts__.get('state_tabular', False)
rcounts = {}
hcolor = colors['GREEN']

View File

@ -17,7 +17,9 @@ def output(data):
Read in the dict structure generated by the salt key API methods and
print the structure.
'''
color = salt.utils.get_colors(__opts__.get('color'))
color = salt.utils.get_colors(
__opts__.get('color'),
__opts__.get('color_theme'))
strip_colors = __opts__.get('strip_colors', True)
if __opts__['transport'] == 'zeromq':
acc = 'minions'

View File

@ -39,7 +39,9 @@ class NestDisplay(object):
Manage the nested display contents
'''
def __init__(self):
self.colors = salt.utils.get_colors(__opts__.get('color'))
self.colors = salt.utils.get_colors(
__opts__.get('color'),
__opts__.get('color_theme'))
def ustring(self,
indent,

View File

@ -23,7 +23,9 @@ class NestDisplay(object):
Create generator for nested output
'''
def __init__(self):
self.colors = salt.utils.get_colors(__opts__.get('color'))
self.colors = salt.utils.get_colors(
__opts__.get('color'),
__opts__.get('color_theme'))
def display(self, ret, indent, prefix, out):
'''

View File

@ -23,7 +23,9 @@ def output(data):
'''
Format the data for printing stage information from the overstate system
'''
colors = salt.utils.get_colors(__opts__.get('color'))
colors = salt.utils.get_colors(
__opts__.get('color'),
__opts__.get('color_theme'))
ostr = ''
for comp in data:
for name, stage in comp.items():

View File

@ -104,6 +104,8 @@ from salt.exceptions import (
SaltInvocationError
)
# Import third party libs
import yaml
# Do not use these color declarations, use get_colors()
# These color declarations will be removed in the future
@ -152,7 +154,28 @@ def is_empty(filename):
return False
def get_colors(use=True):
def get_color_theme(theme):
'''
Return the color theme to use
'''
if not os.path.isfile(theme):
log.warning('The named theme {0} if not available'.format(theme))
try:
with fopen(theme, 'rb') as fp_:
colors = yaml.safe_load(fp_.read())
ret = {}
for color in colors:
ret[color] = '\033[{0}m'.format(colors[color])
if not isinstance(colors, dict):
log.warning('The theme file {0} is not a dict'.format(theme))
return {}
return ret
except Exception:
log.warning('Failed to read the color theme {0}'.format(theme))
return {}
def get_colors(use=True, theme=None):
'''
Return the colors as an easy to use dict, pass False to return the colors
as empty strings so that they will not be applied
@ -178,6 +201,8 @@ def get_colors(use=True):
'RED_BOLD': '\033[01;31m',
'ENDC': '\033[0m',
}
if theme:
colors.update(get_color_theme(theme))
if not use:
for color in colors: