mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add ability to specify encoding in sdecode
This commit is contained in:
parent
2e7985a81c
commit
8c0164fb63
3 changed files with 11 additions and 9 deletions
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue