Add ability to specify encoding in sdecode

This commit is contained in:
twangboy 2018-03-08 15:15:40 -07:00 committed by Daniel A. Wozniak
parent 2e7985a81c
commit 8c0164fb63
No known key found for this signature in database
GPG key ID: 166B9D2C06C82D61
3 changed files with 11 additions and 9 deletions

View file

@ -17,10 +17,7 @@ def get_encodings():
'''
return a list of string encodings to try
'''
if salt.utils.is_windows():
encodings = ['utf-8', __salt_system_encoding__]
else:
encodings = [__salt_system_encoding__]
encodings = [__salt_system_encoding__]
try:
sys_enc = sys.getdefaultencoding()
@ -36,12 +33,15 @@ def get_encodings():
return encodings
def sdecode(string_):
def sdecode(string_, encoding=None):
'''
Since we don't know where a string is coming from and that string will
need to be safely decoded, this function will attempt to decode the string
until it has a working string that does not stack trace
'''
if encoding:
return salt.utils.to_unicode(string_, encoding)
encodings = get_encodings()
for encoding in encodings:
try:

View file

@ -288,7 +288,7 @@ def _get_jinja_error(trace, context=None):
return line, out
def render_jinja_tmpl(tmplstr, context, tmplpath=None):
def render_jinja_tmpl(tmplstr, context, tmplpath=None, encoding=None):
opts = context['opts']
saltenv = context['saltenv']
loader = None
@ -406,11 +406,12 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
decoded_context = {}
for key, value in six.iteritems(context):
if not isinstance(value, six.string_types):
if isinstance(value, six.text_type):
decoded_context[key] = value
continue
decoded_context[key] = salt.utils.locales.sdecode(value)
decoded_context[key] = salt.utils.locales.sdecode(value,
encoding=encoding)
try:
template = jinja_env.from_string(tmplstr)

View file

@ -303,7 +303,8 @@ class TestGetTemplate(TestCase):
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote',
'file_roots': self.local_opts['file_roots'],
'pillar_roots': self.local_opts['pillar_roots']},
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt))
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt),
encoding='utf-8')
self.assertEqual(out, u'Hey world !Hi Sàlt !' + os.linesep)
self.assertEqual(fc.requests[0]['path'], 'salt://macro')