From 4add3915b0c32439a48e1ad7cb8417c06d7a4fd2 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Tue, 17 Mar 2015 13:37:52 -0400 Subject: [PATCH] create a helper to get keys to attributes in an object Signed-off-by: Alfredo Deza --- radosgw_agent/util/obj.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/radosgw_agent/util/obj.py b/radosgw_agent/util/obj.py index 30d63c0..09ffbda 100644 --- a/radosgw_agent/util/obj.py +++ b/radosgw_agent/util/obj.py @@ -20,3 +20,23 @@ def to_dict(_object, **extra_keys): dictified_obj[k] = v return dictified_obj + + +def keys_to_attributes(dictionary, name="BucketEntry"): + """ + Because some objects are dynamic, we are forced to skip namedtuples + and set the attributes from keys in dictionaries so that accessing them + is easier and compatible with code that accesses them as regular objects. + + .. note: dashes are converted to underscores + """ + class Meta(object): + + def __init__(self, **kw): + for k, v in kw.items(): + k = k.replace('-', '_') + setattr(self, k, v) + + obj_ = Meta(**dictionary) + obj_.__class__.__name__ = name + return obj_ -- 2.47.3