open_upload_stream returns only GridIn

This commit is contained in:
aherlihy 2015-08-11 12:14:06 -04:00
parent d06a6397b8
commit 04b1b8ad34
2 changed files with 11 additions and 16 deletions

View File

@ -426,8 +426,7 @@ class GridFSBucket(object):
grid_in.write("data I want to store!")
grid_in.close() # uploaded on close
Returns an instance of :class:`~gridfs.grid_file.GridIn` and the _id
of the file to upload.
Returns an instance of :class:`~gridfs.grid_file.GridIn`.
Raises :exc:`~gridfs.errors.NoFile` if no such version of
that file exists.
@ -449,8 +448,7 @@ class GridFSBucket(object):
if metadata is not None:
opts["metadata"] = metadata
gin = GridIn(self._collection, **opts)
return gin, gin._id
return GridIn(self._collection, **opts)
def upload_from_stream(self, filename, source, chunk_size_bytes=None,
metadata=None):
@ -484,14 +482,11 @@ class GridFSBucket(object):
files collection document. If not provided the metadata field will
be omitted from the files collection document.
"""
gin, _id = self.open_upload_stream(filename, chunk_size_bytes,
metadata)
try:
with self.open_upload_stream(
filename, chunk_size_bytes, metadata) as gin:
gin.write(source)
finally:
gin.close()
return _id
return gin._id
def open_download_stream(self, file_id):
"""Opens a Stream from which the application can read the contents of

View File

@ -53,7 +53,7 @@ class JustWrite(threading.Thread):
def run(self):
for _ in range(self.num):
file, _ = self.gfs.open_upload_stream("test")
file = self.gfs.open_upload_stream("test")
file.write(b"hello")
file.close()
@ -224,7 +224,7 @@ class TestGridfs(IntegrationTest):
def test_get_last_version(self):
one = self.fs.upload_from_stream("test", b"foo")
time.sleep(0.01)
two, _ = self.fs.open_upload_stream("test")
two = self.fs.open_upload_stream("test")
two.write(b"bar")
two.close()
time.sleep(0.01)
@ -356,18 +356,18 @@ class TestGridfs(IntegrationTest):
"second_name").read())
def test_abort(self):
gin, file_id = self.fs.open_upload_stream("test_filename",
chunk_size_bytes=5)
gin = self.fs.open_upload_stream("test_filename",
chunk_size_bytes=5)
gin.write(b"test1")
gin.write(b"test2")
gin.write(b"test3")
self.assertEqual(3, self.db.fs.chunks.count(
{"files_id": file_id}))
{"files_id": gin._id}))
gin.abort()
self.assertTrue(gin.closed)
self.assertRaises(ValueError, gin.write, b"test4")
self.assertEqual(0, self.db.fs.chunks.count(
{"files_id": file_id}))
{"files_id": gin._id}))
class TestGridfsBucketReplicaSet(TestReplicaSetClientBase):