Fix: "has_key" issue for Python 2/3 compatible code
remove temporary_mutation function which contains 'has_key' code since temporary_mutation is not used anywhere. Change-Id: I88d8894f4ac55973408f583680c228dbe78abaeb Story: 2003433 Task:25737 Signed-off-by: Sun Austin <austin.sun@intel.com>
This commit is contained in:
parent
f999509bca
commit
1f53e7abf3
@ -588,55 +588,6 @@ def hash_file(file_like_object):
|
|||||||
return checksum.hexdigest()
|
return checksum.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def temporary_mutation(obj, **kwargs):
|
|
||||||
"""Temporarily set the attr on a particular object to a given value then
|
|
||||||
revert when finished.
|
|
||||||
|
|
||||||
One use of this is to temporarily set the read_deleted flag on a context
|
|
||||||
object:
|
|
||||||
|
|
||||||
with temporary_mutation(context, read_deleted="yes"):
|
|
||||||
do_something_that_needed_deleted_objects()
|
|
||||||
"""
|
|
||||||
def is_dict_like(thing):
|
|
||||||
return hasattr(thing, 'has_key')
|
|
||||||
|
|
||||||
def get(thing, attr, default):
|
|
||||||
if is_dict_like(thing):
|
|
||||||
return thing.get(attr, default)
|
|
||||||
else:
|
|
||||||
return getattr(thing, attr, default)
|
|
||||||
|
|
||||||
def set_value(thing, attr, val):
|
|
||||||
if is_dict_like(thing):
|
|
||||||
thing[attr] = val
|
|
||||||
else:
|
|
||||||
setattr(thing, attr, val)
|
|
||||||
|
|
||||||
def delete(thing, attr):
|
|
||||||
if is_dict_like(thing):
|
|
||||||
del thing[attr]
|
|
||||||
else:
|
|
||||||
delattr(thing, attr)
|
|
||||||
|
|
||||||
NOT_PRESENT = object()
|
|
||||||
|
|
||||||
old_values = {}
|
|
||||||
for attr, new_value in kwargs.items():
|
|
||||||
old_values[attr] = get(obj, attr, NOT_PRESENT)
|
|
||||||
set_value(obj, attr, new_value)
|
|
||||||
|
|
||||||
try:
|
|
||||||
yield
|
|
||||||
finally:
|
|
||||||
for attr, old_value in old_values.items():
|
|
||||||
if old_value is NOT_PRESENT:
|
|
||||||
delete(obj, attr)
|
|
||||||
else:
|
|
||||||
set_value(obj, attr, old_value)
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def tempdir(**kwargs):
|
def tempdir(**kwargs):
|
||||||
tempfile.tempdir = CONF.tempdir
|
tempfile.tempdir = CONF.tempdir
|
||||||
|
Loading…
Reference in New Issue
Block a user