Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for setting file IDs explicitly. #70

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions depot/io/awss3.py
Expand Up @@ -89,11 +89,11 @@ class S3Storage(FileStorage):

All the files are stored inside a bucket named ``bucket`` on ``host`` which Depot
connects to using ``access_key_id`` and ``secret_access_key``.

Additional options include:
* ``host`` which can be used to specify an host different from Amazon
AWS S3 Storage
* ``policy`` which can be used to specify a canned ACL policy of either
* ``policy`` which can be used to specify a canned ACL policy of either
``private`` or ``public-read``.
* ``encrypt_key`` which can be specified to use the server side
encryption feature.
Expand Down Expand Up @@ -164,9 +164,9 @@ def __save_file(self, key, content, filename, content_type=None):
key.set_contents_from_string(content, policy=self._policy,
encrypt_key=self._encrypt_key)

def create(self, content, filename=None, content_type=None):
def create(self, content, filename=None, content_type=None, fileid=None):
content, filename, content_type = self.fileinfo(content, filename, content_type)
new_file_id = str(uuid.uuid1())
new_file_id = fileid if fileid is not None else str(uuid.uuid1())
key = self._bucket_driver.new_key(new_file_id)
self.__save_file(key, content, filename, content_type)
return new_file_id
Expand Down
8 changes: 4 additions & 4 deletions depot/io/boto3.py
Expand Up @@ -111,15 +111,15 @@ class S3Storage(FileStorage):

All the files are stored inside a bucket named ``bucket`` on ``host`` which Depot
connects to using ``access_key_id`` and ``secret_access_key``.

Additional options include:
* ``region`` which can be used to specify the AWS region.
* ``endpoint_url`` which can be used to specify an host different from Amazon
AWS S3 Storage
* ``policy`` which can be used to specify a canned ACL policy of either
``private`` or ``public-read``.
* ``storage_class`` which can be used to specify a class of storage.
* ``prefix`` parameter can be used to store all files under
* ``prefix`` parameter can be used to store all files under
specified prefix. Use a prefix like **dirname/** (*see trailing slash*)
to store in a subdirectory.
"""
Expand Down Expand Up @@ -189,9 +189,9 @@ def __save_file(self, key, content, filename, content_type=None):
raise TypeError('Only bytes can be stored, not unicode')
key.put(Body=content, **attrs)

def create(self, content, filename=None, content_type=None):
def create(self, content, filename=None, content_type=None, fileid=None):
content, filename, content_type = self.fileinfo(content, filename, content_type)
new_file_id = str(uuid.uuid1())
new_file_id = fileid if fileid is not None else str(uuid.uuid1())
key = self._bucket_driver.new_key(new_file_id)
self.__save_file(key, content, filename, content_type)
return new_file_id
Expand Down
16 changes: 11 additions & 5 deletions depot/io/gridfs.py
Expand Up @@ -73,12 +73,18 @@ def get(self, file_or_id):

return GridFSStoredFile(fileid, gridout)

def create(self, content, filename=None, content_type=None):
def create(self, content, filename=None, content_type=None, fileid=None):
content, filename, content_type = self.fileinfo(content, filename, content_type)
new_file_id = self._gridfs.put(content,
filename=filename,
content_type=content_type,
last_modified=utils.timestamp())
if fileid is None:
new_file_id = self._gridfs.put(content,
filename=filename,
content_type=content_type,
last_modified=utils.timestamp())
else:
new_file_id = self._gridfs.put(content, _id=fileid,
filename=filename,
content_type=content_type,
last_modified=utils.timestamp())
return str(new_file_id)

def replace(self, file_or_id, content, filename=None, content_type=None):
Expand Down
4 changes: 3 additions & 1 deletion depot/io/interfaces.py
Expand Up @@ -152,12 +152,14 @@ def get(self, file_or_id): # pragma: no cover
return

@abstractmethod
def create(self, content, filename=None, content_type=None): # pragma: no cover
def create(self, content, filename=None, content_type=None, fileid=None): # pragma: no cover
"""Saves a new file and returns the ID of the newly created file.

``content`` parameter can either be ``bytes``, another ``file object``
or a :class:`cgi.FieldStorage`. When ``filename`` and ``content_type``
parameters are not provided they are deducted from the content itself.
When ``fileid`` is provided it will manually set the file ID used.
USE WITH CARE.
"""
return

Expand Down
4 changes: 2 additions & 2 deletions depot/io/local.py
Expand Up @@ -105,8 +105,8 @@ def __save_file(self, file_id, content, filename, content_type=None):
with open(_metadata_path(local_file_path), 'w') as metadatafile:
metadatafile.write(json.dumps(metadata))

def create(self, content, filename=None, content_type=None):
new_file_id = str(uuid.uuid1())
def create(self, content, filename=None, content_type=None, fileid=None):
new_file_id = fileid if fileid is not None else str(uuid.uuid1())
content, filename, content_type = self.fileinfo(content, filename, content_type)
self.__save_file(new_file_id, content, filename, content_type)
return new_file_id
Expand Down
4 changes: 2 additions & 2 deletions depot/io/memory.py
Expand Up @@ -75,8 +75,8 @@ def __save_file(self, file_id, content, filename, content_type=None):
}
}

def create(self, content, filename=None, content_type=None):
new_file_id = str(uuid.uuid1())
def create(self, content, filename=None, content_type=None, fileid=None):
new_file_id = fileid if fileid is not None else str(uuid.uuid1())
content, filename, content_type = self.fileinfo(content, filename, content_type)
self.__save_file(new_file_id, content, filename, content_type)
return new_file_id
Expand Down
10 changes: 10 additions & 0 deletions tests/test_storage_interface.py
Expand Up @@ -141,6 +141,16 @@ def test_replace_only_existing(self):

self.fs.replace(file_id, FILE_CONTENT)

@raises(IOError)
def test_create_set_id(self):
file_id = self.fs.create(FILE_CONTENT, 'file.txt')
assert self.fs.exists(file_id)
self.fs.delete(file_id)
assert not self.fs.exists(file_id)

self.fs.create(FILE_CONTENT, fileid=file_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want a assert self.fs.exists(file_id) affert this line. Otherwise we wouldn't be testing much.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

assert not self.fs.exists(file_id)

@raises(ValueError)
def test_replace_invalidid(self):
self.fs.replace('INVALIDID', FILE_CONTENT)
Expand Down