Commits

Andrei Coman  committed c79bd4d

Minor refactorings
New storages.util module for commonly used functions among storages
Made S3Boto and AzureStorage use the new module

  • Participants
  • Parent commits bc4091b

Comments (0)

Files changed (4)

File storages/backends/azure_storage.py

 import os.path
 
-from django.conf import settings
 from django.core.files.base import ContentFile
 from django.core.files.storage import Storage
 from django.core.exceptions import ImproperlyConfigured
         "Could not load Azure bindings. "
         "See https://github.com/WindowsAzure/azure-sdk-for-python")
 
+from storages.util import setting
+
 
 def clean_name(name):
     return os.path.normpath(name).replace("\\", "/")
 
 
-def setting(name, default=None):
-    return getattr(settings, name, default)
-
-
 class AzureStorage(Storage):
     account_name = setting("AZURE_ACCOUNT_NAME")
     account_key = setting("AZURE_ACCOUNT_KEY")
     @property
     def connection(self):
         if self._connection is None:
-            self._connection = azure.storage.BlobService(self.account_name,
-                                                         self.account_key)
+            self._connection = azure.storage.BlobService(
+                self.account_name, self.account_key)
         return self._connection
 
     def _open(self, name, mode="rb"):
 
     def exists(self, name):
         try:
-            self.connection.get_blob_properties(self.azure_container, name)
+            self.connection.get_blob_properties(
+                self.azure_container, name)
         except azure.WindowsAzureMissingResourceError:
             return False
         else:

File storages/backends/gs.py

 
 from django.core.exceptions import ImproperlyConfigured
 
-from storages.backends.s3boto import S3BotoStorage, S3BotoStorageFile, setting
+from storages.backends.s3boto import S3BotoStorage, S3BotoStorageFile
+from storages.util import setting
 
 try:
     from boto.gs.connection import GSConnection, SubdomainCallingFormat

File storages/backends/s3boto.py

 except ImportError:
     from StringIO import StringIO  # noqa
 
-from django.conf import settings
 from django.core.files.base import File
 from django.core.files.storage import Storage
 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
     raise ImproperlyConfigured("Could not load Boto's S3 bindings.\n"
                                "See https://github.com/boto/boto")
 
+from storages.util import setting
+
 boto_version_info = tuple([int(i) for i in boto_version.split('.')])
 
 if boto_version_info[:2] < (2, 4):
                                "higher.\nSee https://github.com/boto/boto")
 
 
-def setting(name, default=None):
-    """
-    Helper function to get a Django setting by name or (optionally) return
-    a default (or else ``None``).
-    """
-    return getattr(settings, name, default)
-
-
 def safe_join(base, *paths):
     """
     A version of django.utils._os.safe_join for S3 paths.
     # TODO: Read/Write (rw) mode may be a bit undefined at the moment. Needs testing.
     # TODO: When Django drops support for Python 2.5, rewrite to use the
     #       BufferedIO streams in the Python 2.6 io module.
-    buffer_size = getattr(settings, 'AWS_S3_FILE_BUFFER_SIZE', 5242880)
+    buffer_size = setting('AWS_S3_FILE_BUFFER_SIZE', 5242880)
 
     def __init__(self, name, mode, storage, buffer_size=None):
         self._storage = storage

File storages/util.py

+from django.conf import settings
+
+
+def setting(name, default=None):
+    """
+    Helper function to get a Django setting by name or (optionally) return
+    a default (or else ``None``).
+    """
+    return getattr(settings, name, default)