mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #12723 from mgedmin/py3k
Make ansible.vars.unsafe_proxy Python 3 friendly
This commit is contained in:
commit
2d77c643e4
1 changed files with 35 additions and 13 deletions
|
@ -52,25 +52,45 @@
|
||||||
|
|
||||||
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
|
__all__ = ['UnsafeProxy', 'AnsibleUnsafe', 'wrap_var']
|
||||||
|
|
||||||
import __builtin__
|
|
||||||
|
|
||||||
class AnsibleUnsafe(object):
|
class AnsibleUnsafe(object):
|
||||||
__UNSAFE__ = True
|
__UNSAFE__ = True
|
||||||
|
|
||||||
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
try:
|
||||||
pass
|
unicode
|
||||||
|
except NameError:
|
||||||
|
# Python 3
|
||||||
|
class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
|
||||||
|
pass
|
||||||
|
|
||||||
class AnsibleUnsafeUnicode(unicode, AnsibleUnsafe):
|
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class UnsafeProxy(object):
|
||||||
|
def __new__(cls, obj, *args, **kwargs):
|
||||||
|
if obj.__class__ == str:
|
||||||
|
return AnsibleUnsafeStr(obj)
|
||||||
|
elif obj.__class__ == bytes:
|
||||||
|
return AnsibleUnsafeBytes(obj)
|
||||||
|
else:
|
||||||
|
return obj
|
||||||
|
else:
|
||||||
|
# Python 2
|
||||||
|
class AnsibleUnsafeStr(str, AnsibleUnsafe):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AnsibleUnsafeUnicode(unicode, AnsibleUnsafe):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class UnsafeProxy(object):
|
||||||
|
def __new__(cls, obj, *args, **kwargs):
|
||||||
|
if obj.__class__ == unicode:
|
||||||
|
return AnsibleUnsafeUnicode(obj)
|
||||||
|
elif obj.__class__ == str:
|
||||||
|
return AnsibleUnsafeStr(obj)
|
||||||
|
else:
|
||||||
|
return obj
|
||||||
|
|
||||||
class UnsafeProxy(object):
|
|
||||||
def __new__(cls, obj, *args, **kwargs):
|
|
||||||
if obj.__class__ == unicode:
|
|
||||||
return AnsibleUnsafeUnicode(obj)
|
|
||||||
elif obj.__class__ == str:
|
|
||||||
return AnsibleUnsafeStr(obj)
|
|
||||||
else:
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def _wrap_dict(v):
|
def _wrap_dict(v):
|
||||||
for k in v.keys():
|
for k in v.keys():
|
||||||
|
@ -78,12 +98,14 @@ def _wrap_dict(v):
|
||||||
v[k] = wrap_var(v[k])
|
v[k] = wrap_var(v[k])
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
def _wrap_list(v):
|
def _wrap_list(v):
|
||||||
for idx, item in enumerate(v):
|
for idx, item in enumerate(v):
|
||||||
if item is not None:
|
if item is not None:
|
||||||
v[idx] = wrap_var(item)
|
v[idx] = wrap_var(item)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
def wrap_var(v):
|
def wrap_var(v):
|
||||||
if isinstance(v, dict):
|
if isinstance(v, dict):
|
||||||
v = _wrap_dict(v)
|
v = _wrap_dict(v)
|
||||||
|
|
Loading…
Reference in a new issue