Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
b186676e
Commit
b186676e
authored
Mar 20, 2015
by
Toshio Kuratomi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up jsonify and make json_dict_*to* more flexible at the same time.
parent
f976d47f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
15 deletions
+20
-15
v2/ansible/module_utils/basic.py
+20
-15
No files found.
v2/ansible/module_utils/basic.py
View file @
b186676e
...
@@ -65,6 +65,7 @@ import pwd
...
@@ -65,6 +65,7 @@ import pwd
import
platform
import
platform
import
errno
import
errno
import
tempfile
import
tempfile
from
itertools
import
imap
,
repeat
try
:
try
:
import
json
import
json
...
@@ -234,7 +235,7 @@ def load_platform_subclass(cls, *args, **kwargs):
...
@@ -234,7 +235,7 @@ def load_platform_subclass(cls, *args, **kwargs):
return
super
(
cls
,
subclass
)
.
__new__
(
subclass
)
return
super
(
cls
,
subclass
)
.
__new__
(
subclass
)
def
json_dict_unicode_to_bytes
(
d
):
def
json_dict_unicode_to_bytes
(
d
,
encoding
=
'utf-8'
):
''' Recursively convert dict keys and values to byte str
''' Recursively convert dict keys and values to byte str
Specialized for json return because this only handles, lists, tuples,
Specialized for json return because this only handles, lists, tuples,
...
@@ -242,17 +243,17 @@ def json_dict_unicode_to_bytes(d):
...
@@ -242,17 +243,17 @@ def json_dict_unicode_to_bytes(d):
'''
'''
if
isinstance
(
d
,
unicode
):
if
isinstance
(
d
,
unicode
):
return
d
.
encode
(
'utf-8'
)
return
d
.
encode
(
encoding
)
elif
isinstance
(
d
,
dict
):
elif
isinstance
(
d
,
dict
):
return
dict
(
map
(
json_dict_unicode_to_bytes
,
d
.
iteritems
(
)))
return
dict
(
imap
(
json_dict_unicode_to_bytes
,
d
.
iteritems
(),
repeat
(
encoding
)))
elif
isinstance
(
d
,
list
):
elif
isinstance
(
d
,
list
):
return
list
(
map
(
json_dict_unicode_to_bytes
,
d
))
return
list
(
imap
(
json_dict_unicode_to_bytes
,
d
,
repeat
(
encoding
)
))
elif
isinstance
(
d
,
tuple
):
elif
isinstance
(
d
,
tuple
):
return
tuple
(
map
(
json_dict_unicode_to_bytes
,
d
))
return
tuple
(
imap
(
json_dict_unicode_to_bytes
,
d
,
repeat
(
encoding
)
))
else
:
else
:
return
d
return
d
def
json_dict_bytes_to_unicode
(
d
):
def
json_dict_bytes_to_unicode
(
d
,
encoding
=
'utf-8'
):
''' Recursively convert dict keys and values to byte str
''' Recursively convert dict keys and values to byte str
Specialized for json return because this only handles, lists, tuples,
Specialized for json return because this only handles, lists, tuples,
...
@@ -260,13 +261,13 @@ def json_dict_bytes_to_unicode(d):
...
@@ -260,13 +261,13 @@ def json_dict_bytes_to_unicode(d):
'''
'''
if
isinstance
(
d
,
str
):
if
isinstance
(
d
,
str
):
return
unicode
(
d
,
'utf-8'
)
return
unicode
(
d
,
encoding
)
elif
isinstance
(
d
,
dict
):
elif
isinstance
(
d
,
dict
):
return
dict
(
map
(
json_dict_bytes_to_unicode
,
d
.
iteritems
(
)))
return
dict
(
imap
(
json_dict_bytes_to_unicode
,
d
.
iteritems
(),
repeat
(
encoding
)))
elif
isinstance
(
d
,
list
):
elif
isinstance
(
d
,
list
):
return
list
(
map
(
json_dict_bytes_to_unicode
,
d
))
return
list
(
imap
(
json_dict_bytes_to_unicode
,
d
,
repeat
(
encoding
)
))
elif
isinstance
(
d
,
tuple
):
elif
isinstance
(
d
,
tuple
):
return
tuple
(
map
(
json_dict_bytes_to_unicode
,
d
))
return
tuple
(
imap
(
json_dict_bytes_to_unicode
,
d
,
repeat
(
encoding
)
))
else
:
else
:
return
d
return
d
...
@@ -1189,13 +1190,17 @@ class AnsibleModule(object):
...
@@ -1189,13 +1190,17 @@ class AnsibleModule(object):
self
.
fail_json
(
msg
=
'Boolean
%
s not in either boolean list'
%
arg
)
self
.
fail_json
(
msg
=
'Boolean
%
s not in either boolean list'
%
arg
)
def
jsonify
(
self
,
data
):
def
jsonify
(
self
,
data
):
for
encoding
in
(
"utf-8"
,
"latin-1"
,
"unicode_escape"
):
for
encoding
in
(
"utf-8"
,
"latin-1"
):
try
:
try
:
return
json
.
dumps
(
data
,
encoding
=
encoding
)
return
json
.
dumps
(
data
,
encoding
=
encoding
)
# Old systems using simplejson module does not support encoding keyword.
# Old systems using old simplejson module does not support encoding keyword.
except
TypeError
,
e
:
except
TypeError
:
return
json
.
dumps
(
data
)
try
:
except
UnicodeDecodeError
,
e
:
new_data
=
json_dict_bytes_to_unicode
(
data
,
encoding
=
encoding
)
except
UnicodeDecodeError
:
continue
return
json
.
dumps
(
new_data
)
except
UnicodeDecodeError
:
continue
continue
self
.
fail_json
(
msg
=
'Invalid unicode encoding encountered'
)
self
.
fail_json
(
msg
=
'Invalid unicode encoding encountered'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment