diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-11 12:04:30 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-11 12:04:30 -0500 |
commit | 6a07362dd0145643edf4b61b0ae402e3093793c2 (patch) | |
tree | 85a656ef269cebfb8e55959783be4ccfc9edd258 | |
parent | 3a89c23e7fd330ea662dd57ff74a9d424d476b84 (diff) | |
download | mediagoblin-6a07362dd0145643edf4b61b0ae402e3093793c2.tar.lz mediagoblin-6a07362dd0145643edf4b61b0ae402e3093793c2.tar.xz mediagoblin-6a07362dd0145643edf4b61b0ae402e3093793c2.zip |
Adding a copy_locally() method to the StorageInterface and giving it a test.
-rw-r--r-- | mediagoblin/storage.py | 21 | ||||
-rw-r--r-- | mediagoblin/tests/test_storage.py | 16 |
2 files changed, 37 insertions, 0 deletions
diff --git a/mediagoblin/storage.py b/mediagoblin/storage.py index 685039b2..ba6ac017 100644 --- a/mediagoblin/storage.py +++ b/mediagoblin/storage.py @@ -16,6 +16,7 @@ import os import re +import shutil import urlparse import uuid @@ -141,6 +142,24 @@ class StorageInterface(object): # Subclasses should override this method, if applicable. self.__raise_not_implemented() + def copy_locally(self, filepath, dest_path): + """ + Copy this file locally. + + A basic working method for this is provided that should + function both for local_storage systems and remote storge + systems, but if more efficient systems for copying locally + apply to your system, override this method with something more + appropriate. + """ + if self.local_storage: + shutil.copy( + self.get_local_path(filepath), dest_path) + else: + with self.get_file(filepath, 'rb') as source_file: + with file(dest_path, 'wb') as dest_file: + dest_file.write(source_file.read()) + class BasicFileStorage(StorageInterface): """ @@ -272,3 +291,5 @@ def storage_system_from_paste_config(paste_config, storage_prefix): storage_class = util.import_component(storage_class) return storage_class(**config_params) + + diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 5efed405..83f893f1 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -231,3 +231,19 @@ def test_basic_storage_get_local_path(): def test_basic_storage_is_local(): tmpdir, this_storage = get_tmp_filestorage() assert this_storage.local_storage is True + + +def test_basic_storage_copy_locally(): + tmpdir, this_storage = get_tmp_filestorage() + + dest_tmpdir = tempfile.mkdtemp() + + filepath = ['dir1', 'dir2', 'ourfile.txt'] + with this_storage.get_file(filepath, 'w') as our_file: + our_file.write('Testing this file') + + new_file_dest = os.path.join(dest_tmpdir, 'file2.txt') + + this_storage.copy_locally(filepath, new_file_dest) + + assert file(new_file_dest).read() == 'Testing this file' |