From: Sapan Bhatia <gwsapan@gmail.com>
Date: Mon, 15 Nov 2010 23:15:34 +0000 (-0500)
Subject: This function is needed to cache the results of API calls, which for a godforsaken... 
X-Git-Tag: plcapi-5.0-19~58^2
X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=a225821931d0a387a83e93f68a304da47a182085;p=plcapi.git

This function is needed to cache the results of API calls, which for a godforsaken reason cannot be pickled.
---

diff --git a/PLC/Methods/GetSlivers.py b/PLC/Methods/GetSlivers.py
index a8ee408c..d54784d0 100644
--- a/PLC/Methods/GetSlivers.py
+++ b/PLC/Methods/GetSlivers.py
@@ -123,6 +123,24 @@ def get_slivers(api, auth, slice_filter, node = None):
 
     return slivers
 
+### The pickle module, used in conjunction with caching has a restriction that it does not
+### work on "connection objects." It doesn't matter if the connection object has
+### an 'str' or 'repr' method, there is a taint check that throws an exception if
+### the pickled class is found to derive from a connection.
+### (To be moved to Method.py)
+
+def sanitize_for_pickle (obj):
+    if (isinstance(obj, dict)):
+        parent = dict(obj)
+        for k in parent.keys(): parent[k] = sanitize_for_pickle (parent[k])
+        return parent
+    elif (isinstance(obj, list)):
+        parent = list(obj)
+        parent = map(sanitize_for_pickle, parent)
+        return parent
+    else:
+        return obj
+
 class GetSlivers(Method):
     """
     Returns a struct containing information about the specified node