Andrei Coman avatar Andrei Coman committed c79bd4d

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

Comments (0)

Files changed (4)

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:

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

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
+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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.