Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
New storages.util module for commonly used functions among storages
Made S3Boto and AzureStorage use the new module
  • Loading branch information
comandrei committed Jan 27, 2013
1 parent c62fe43 commit 00a252a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
14 changes: 6 additions & 8 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
Expand All @@ -13,15 +12,13 @@
"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")
Expand All @@ -34,8 +31,8 @@ def __init__(self, *args, **kwargs):
@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"):
Expand All @@ -44,7 +41,8 @@ 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:
Expand Down
3 changes: 2 additions & 1 deletion storages/backends/gs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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
Expand Down
13 changes: 3 additions & 10 deletions storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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
Expand All @@ -23,21 +22,15 @@
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):
raise ImproperlyConfigured("The installed Boto library must be 2.4 or "
"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.
Expand Down Expand Up @@ -92,7 +85,7 @@ class S3BotoStorageFile(File):
# 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
Expand Down
9 changes: 9 additions & 0 deletions storages/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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)

0 comments on commit 00a252a

Please sign in to comment.