Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DSON outputter #49338

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions salt/output/dson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
'''
Display return data in DSON format
==================================

This outputter is intended for demonstration purposes. Information on the DSON
spec can be found `here`__.

.. __: http://vpzomtrrfrt.github.io/DSON/

This outputter requires `Dogeon`__ (installable via pip)

.. __: https://github.com/soasme/dogeon
'''
from __future__ import absolute_import, print_function, unicode_literals

# Import python libs
import logging

# Import 3rd-party libs
try:
import dson
except ImportError:
dson = None

from salt.ext import six

log = logging.getLogger(__name__)


def __virtual__():
if dson is None:
return (False, 'The dogeon Python package is not installed')
return True


def output(data, **kwargs): # pylint: disable=unused-argument
'''
Print the output data in JSON
'''
try:
dump_opts = {'indent': 4, 'default': repr}

if 'output_indent' in __opts__:

indent = __opts__.get('output_indent')
sort_keys = False

if indent == 'pretty':
indent = 4
sort_keys = True

elif isinstance(indent, six.integer_types):
if indent >= 0:
indent = indent
else:
indent = None

dump_opts['indent'] = indent
dump_opts['sort_keys'] = sort_keys

return dson.dumps(data, **dump_opts)

except UnicodeDecodeError as exc:
log.error('Unable to serialize output to dson')
return dson.dumps(
{'error': 'Unable to serialize output to DSON',
'message': six.text_type(exc)}
)

except TypeError:
log.debug('An error occurred while outputting DSON', exc_info=True)
# Return valid JSON for unserializable objects
return dson.dumps({})
8 changes: 7 additions & 1 deletion salt/renderers/dson.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
try:
import dson
except ImportError:
raise
dson = None

# Import salt libs
from salt.ext import six

log = logging.getLogger(__name__)


def __virtual__():
if dson is None:
return (False, 'The dogeon Python package is not installed')
return True


def render(dson_input, saltenv='base', sls='', **kwargs):
'''
Accepts DSON data as a string or as a file object and runs it through the
Expand Down