Untie backend from settings.
authorAntony Chazapis <chazapis@gmail.com>
Wed, 11 Jan 2012 10:26:22 +0000 (12:26 +0200)
committerAntony Chazapis <chazapis@gmail.com>
Wed, 11 Jan 2012 10:26:22 +0000 (12:26 +0200)
Fixes #1816

pithos/api/util.py
pithos/backends/__init__.py
pithos/backends/base.py
pithos/backends/modular.py
pithos/settings.d/10-backend.conf

index 449a96a..709de17 100644 (file)
@@ -763,6 +763,15 @@ def hashmap_md5(request, hashmap, size):
         md5.update(data + ('\x00' * pad))
     return md5.hexdigest().lower()
 
+def get_backend():
+    backend = connect_backend(db_module=settings.BACKEND_DB_MODULE,
+                              db_connection=settings.BACKEND_DB_CONNECTION,
+                              block_module=settings.BACKEND_BLOCK_MODULE,
+                              block_path=settings.BACKEND_BLOCK_PATH)
+    backend.default_policy['quota'] = settings.BACKEND_QUOTA
+    backend.default_policy['versioning'] = settings.BACKEND_VERSIONING
+    return backend
+
 def update_request_headers(request):
     # Handle URL-encoded keys and values.
     # Handle URL-encoded keys and values.
@@ -864,7 +873,7 @@ def api_method(http_method=None, format_allowed=False, user_required=True):
                 
                 # Fill in custom request variables.
                 request.serialization = request_serialization(request, format_allowed)
-                request.backend = connect_backend()
+                request.backend = get_backend()
                 
                 response = func(request, *args, **kwargs)
                 update_response_headers(request, response)
index 519cb6e..e4dcafd 100644 (file)
 
 import warnings
 
-from django.conf import settings
-
 from modular import ModularBackend
 
 
-def connect_backend():
+def connect_backend(**kwargs):
     # Suppress mysql warnings.
     original_filters = warnings.filters[:]
     warnings.simplefilter('ignore')
     try:
-        backend = ModularBackend(settings.BACKEND_DB_MODULE,
-                                 settings.BACKEND_DB_CONNECTION,
-                                 settings.BACKEND_BLOCK_MODULE,
-                                 settings.BACKEND_BLOCK_PATH)
+        backend = ModularBackend(**kwargs)
     finally:
         # Restore warnings.
         warnings.filters = original_filters
     
-    # Set defaults.
-    backend.default_policy['quota'] = settings.DEFAULT_QUOTA
-    backend.default_policy['versioning'] = settings.DEFAULT_VERSIONING
-    
     return backend
index fef3242..d0c8b61 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
+# Default setting for new accounts.
+DEFAULT_QUOTA = 0 # No quota.
+DEFAULT_VERSIONING = 'auto'
+
+
 class NotAllowedError(Exception):
     pass
 
@@ -55,10 +60,6 @@ class BaseBackend(object):
         'default_policy': A dictionary with default policy settings
     """
     
-    def __init__(self, db_module, db_connection, block_module, block_path):
-        """Initialize backend with supplied modules and options."""
-        pass
-    
     def close(self):
         """Close the backend connection."""
         pass
index 10da492..345691f 100644 (file)
@@ -8,8 +8,7 @@
 #      copyright notice, this list of conditions and the following
 #      disclaimer.
 # 
-#   2. Redistributions in binary form must reproduce the above
-#      copyright notice, this list of conditions and the following
+#   2. Redistributions in binary form must reproduce the above #      copyright notice, this list of conditions and the following
 #      disclaimer in the documentation and/or other materials
 #      provided with the distribution.
 # 
@@ -38,10 +37,16 @@ import uuid as uuidlib
 import logging
 import binascii
 
-from base import NotAllowedError, QuotaError, BaseBackend
+from base import DEFAULT_QUOTA, DEFAULT_VERSIONING, NotAllowedError, QuotaError, BaseBackend
 
 from pithos.lib.hashmap import HashMap
 
+# Default modules and settings.
+DEFAULT_DB_MODULE = 'pithos.backends.lib.sqlalchemy'
+DEFAULT_DB_CONNECTION = 'sqlite:///backend.db'
+DEFAULT_BLOCK_MODULE = 'pithos.backends.lib.hashfiler'
+DEFAULT_BLOCK_PATH = 'data/'
+
 ( CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED ) = range(3)
 
 inf = float('inf')
@@ -78,14 +83,16 @@ class ModularBackend(BaseBackend):
     Uses modules for SQL functions and storage.
     """
     
-    def __init__(self, db_module, db_connection, block_module, block_path):
-        db_module = db_module or 'pithos.backends.lib.sqlalchemy'
-        block_module = block_module or 'pithos.backends.lib.hashfiler'
+    def __init__(self, db_module=None, db_connection=None, block_module=None, block_path=None):
+        db_module = db_module or DEFAULT_DB_MODULE
+        db_connection = db_connection or DEFAULT_DB_CONNECTION
+        block_module = block_module or DEFAULT_BLOCK_MODULE
+        block_path = block_path or DEFAULT_BLOCK_PATH
         
         self.hash_algorithm = 'sha256'
         self.block_size = 4 * 1024 * 1024 # 4MB
         
-        self.default_policy = {'quota': 0, 'versioning': 'auto'}
+        self.default_policy = {'quota': DEFAULT_QUOTA, 'versioning': DEFAULT_VERSIONING}
         
         __import__(db_module)
         self.db_module = sys.modules[db_module]
index 0b59172..5cdb0d6 100644 (file)
@@ -15,5 +15,5 @@ BACKEND_BLOCK_MODULE = 'pithos.backends.lib.hashfiler'
 BACKEND_BLOCK_PATH = join(PROJECT_PATH, 'data/')
 
 # Default setting for new accounts.
-DEFAULT_QUOTA = 50 * 1024 * 1024 * 1024
-DEFAULT_VERSIONING = 'auto'
\ No newline at end of file
+BACKEND_QUOTA = 50 * 1024 * 1024 * 1024
+BACKEND_VERSIONING = 'auto'