]> git-server-git.apps.pok.os.sepia.ceph.com Git - radosgw-agent.git/commitdiff
create a helper to get keys to attributes in an object
authorAlfredo Deza <adeza@redhat.com>
Tue, 17 Mar 2015 17:37:52 +0000 (13:37 -0400)
committerAlfredo Deza <adeza@redhat.com>
Tue, 17 Mar 2015 17:37:52 +0000 (13:37 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
radosgw_agent/util/obj.py

index 30d63c0347a02b56f15049c7b92bf75746c08675..09ffbda54cf982de73c581c1ef0431efecbd9d55 100644 (file)
@@ -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_