You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2011/11/02 00:20:24 UTC

svn commit: r1196390 - in /libcloud/trunk: libcloud/storage/base.py test/storage/test_base.py

Author: tomaz
Date: Tue Nov  1 23:20:24 2011
New Revision: 1196390

URL: http://svn.apache.org/viewvc?rev=1196390&view=rev
Log:
Use _get_hash_type function instead of always directly instantiating hashlib.md5
function.

Modified:
    libcloud/trunk/libcloud/storage/base.py
    libcloud/trunk/test/storage/test_base.py

Modified: libcloud/trunk/libcloud/storage/base.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/base.py?rev=1196390&r1=1196389&r2=1196390&view=diff
==============================================================================
--- libcloud/trunk/libcloud/storage/base.py (original)
+++ libcloud/trunk/libcloud/storage/base.py Tue Nov  1 23:20:24 2011
@@ -583,7 +583,7 @@ class StorageDriver(BaseDriver):
         data_hash = None
 
         if calculate_hash:
-            data_hash = hashlib.md5()
+            data_hash = self._get_hash_function()
             data_hash.update(data)
 
         try:
@@ -633,7 +633,7 @@ class StorageDriver(BaseDriver):
 
         data_hash = None
         if calculate_hash:
-            data_hash = hashlib.md5()
+            data_hash = self._get_hash_function()
 
         generator = utils.read_in_chunks(iterator, chunk_size)
 
@@ -714,3 +714,16 @@ class StorageDriver(BaseDriver):
                     calculate_hash=calculate_hash))
 
         return success, data_hash, bytes_transferred
+
+    def _get_hash_function(self):
+        """
+        Return instantiated hash function for the hash type supported by
+        the provider.
+        """
+        try:
+            func = getattr(hashlib, self.hash_type)()
+        except AttributeError:
+            raise RuntimeError('Invalid or unsupported hash type: %s' %
+                               (self.hash_type))
+
+        return func

Modified: libcloud/trunk/test/storage/test_base.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/test/storage/test_base.py?rev=1196390&r1=1196389&r2=1196390&view=diff
==============================================================================
--- libcloud/trunk/test/storage/test_base.py (original)
+++ libcloud/trunk/test/storage/test_base.py Tue Nov  1 23:20:24 2011
@@ -120,6 +120,22 @@ class BaseStorageTests(unittest.TestCase
         self.assertEqual(bytes_transferred, (len(data)))
         self.assertEqual(self.send_called, 1)
 
+    def test__get_hash_function(self):
+        self.driver1.hash_type = 'md5'
+        func = self.driver1._get_hash_function()
+        self.assertTrue(func)
+
+        self.driver1.hash_type = 'sha1'
+        func = self.driver1._get_hash_function()
+        self.assertTrue(func)
+
+        try:
+            self.driver1.hash_type = 'invalid-hash-function'
+            func = self.driver1._get_hash_function()
+        except RuntimeError:
+            pass
+        else:
+            self.fail('Invalid hash type but exception was not thrown')
 
 if __name__ == '__main__':
     sys.exit(unittest.main())