Allow storage systems to be local and allow for a get_local_path
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 11 Jun 2011 16:18:03 +0000 (11:18 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 11 Jun 2011 16:18:03 +0000 (11:18 -0500)
method if applicable.

mediagoblin/storage.py
mediagoblin/tests/test_storage.py

index 5d7e70d6eb5d69b865bbce3b23e242aa337347a7..685039b2745210ce433bd943a60dc0c27f35d03d 100644 (file)
@@ -60,6 +60,9 @@ class StorageInterface(object):
     StorageInterface.
     """
 
+    # Whether this file store is on the local filesystem.
+    local_storage = False
+
     def __raise_not_implemented(self):
         """
         Raise a warning about some component not implemented by a
@@ -127,12 +130,25 @@ class StorageInterface(object):
         else:
             return filepath
 
+    def get_local_path(self, filepath):
+        """
+        If this is a local_storage implementation, give us a link to
+        the local filesystem reference to this file.
+
+        >>> storage_handler.get_local_path(['foo', 'bar', 'baz.jpg'])
+        u'/path/to/mounting/foo/bar/baz.jpg'
+        """
+        # Subclasses should override this method, if applicable.
+        self.__raise_not_implemented()
+
 
 class BasicFileStorage(StorageInterface):
     """
     Basic local filesystem implementation of storage API
     """
 
+    local_storage = True
+
     def __init__(self, base_dir, base_url=None, **kwargs):
         """
         Keyword arguments:
@@ -177,6 +193,9 @@ class BasicFileStorage(StorageInterface):
             self.base_url,
             '/'.join(clean_listy_filepath(filepath)))
 
+    def get_local_path(self, filepath):
+        return self._resolve_filepath(filepath)
+
 
 ###########
 # Utilities
@@ -187,7 +206,7 @@ def clean_listy_filepath(listy_filepath):
     Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and
     clean out any nastiness from it.
 
-    For example:
+
     >>> clean_listy_filepath([u'/dir1/', u'foo/../nasty', u'linooks.jpg'])
     [u'dir1', u'foo_.._nasty', u'linooks.jpg']
 
index 61dd5dcac7013200947831fc1a13f2a100fa912a..5efed405cb4397caef01f123d270a75d9b6d93f4 100644 (file)
@@ -214,3 +214,20 @@ def test_basic_storage_url_for_file():
         ['dir1', 'dir2', 'filename.txt'])
     expected = 'http://media.example.org/ourmedia/dir1/dir2/filename.txt'
     assert result == expected
+
+
+def test_basic_storage_get_local_path():
+    tmpdir, this_storage = get_tmp_filestorage()
+    
+    result = this_storage.get_local_path(
+        ['dir1', 'dir2', 'filename.txt'])
+
+    expected = os.path.join(
+        tmpdir, 'dir1/dir2/filename.txt')
+
+    assert result == expected
+
+
+def test_basic_storage_is_local():
+    tmpdir, this_storage = get_tmp_filestorage()
+    assert this_storage.local_storage is True