Skip to content

Commit 12d74ff

Browse files
committed
PYTHON-821 - Use new CRUD API in GridFS.
1 parent 30af616 commit 12d74ff

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

gridfs/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from pymongo import (ASCENDING,
2828
DESCENDING)
2929
from pymongo.database import Database
30+
from pymongo.errors import ConfigurationError
3031

3132

3233
class GridFS(object):
@@ -48,11 +49,19 @@ def __init__(self, database, collection="fs"):
4849
- `connect` (optional): whether to begin connecting the client in
4950
the background
5051
52+
.. versionchanged:: 3.0
53+
`database` must use an acknowledged
54+
:attr:`~pymongo.database.Database.write_concern`
55+
5156
.. mongodoc:: gridfs
5257
"""
5358
if not isinstance(database, Database):
5459
raise TypeError("database must be an instance of Database")
5560

61+
if not database.write_concern.acknowledged:
62+
raise ConfigurationError('database must use '
63+
'acknowledged write_concern')
64+
5665
self.__database = database
5766
self.__collection = database[collection]
5867
self.__files = self.__collection.files
@@ -210,7 +219,7 @@ def get_last_version(self, filename=None, **kwargs):
210219
def delete(self, file_id):
211220
"""Delete a file from GridFS by ``"_id"``.
212221
213-
Removes all data belonging to the file with ``"_id"``:
222+
Deletes all data belonging to the file with ``"_id"``:
214223
`file_id`.
215224
216225
.. warning:: Any processes/threads reading from the file while
@@ -225,9 +234,8 @@ def delete(self, file_id):
225234
- `file_id`: ``"_id"`` of the file to delete
226235
"""
227236
self.__ensure_index_files_id()
228-
self.__files.remove({"_id": file_id},
229-
**self.__files._get_wc_override())
230-
self.__chunks.remove({"files_id": file_id})
237+
self.__files.delete_many({"_id": file_id})
238+
self.__chunks.delete_one({"files_id": file_id})
231239

232240
def list(self):
233241
"""List the names of all files stored in this instance of

gridfs/grid_file.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pymongo import ASCENDING
2828
from pymongo.collection import Collection
2929
from pymongo.cursor import Cursor
30-
from pymongo.errors import DuplicateKeyError, InvalidOperation
30+
from pymongo.errors import ConfigurationError, DuplicateKeyError
3131
from pymongo.read_preferences import ReadPreference
3232

3333
try:
@@ -62,9 +62,8 @@ def getter(self):
6262

6363
def setter(self, value):
6464
if self._closed:
65-
self._coll.files.update({"_id": self._file["_id"]},
66-
{"$set": {field_name: value}},
67-
**self._coll._get_wc_override())
65+
self._coll.files.update_one({"_id": self._file["_id"]},
66+
{"$set": {field_name: value}})
6867
self._file[field_name] = value
6968

7069
if read_only:
@@ -135,12 +134,18 @@ def __init__(self, root_collection, **kwargs):
135134
- `**kwargs` (optional): file level options (see above)
136135
137136
.. versionchanged:: 3.0
138-
w=0 writes to GridFS are now prohibited.
137+
`root_collection` must use an acknowledged
138+
:attr:`~pymongo.collection.Collection.write_concern`
139139
"""
140140
if not isinstance(root_collection, Collection):
141141
raise TypeError("root_collection must be an "
142142
"instance of Collection")
143143

144+
# With w=0, 'filemd5' might run before the final chunks are written.
145+
if not root_collection.write_concern.acknowledged:
146+
raise ConfigurationError('root_collection must use '
147+
'acknowledged write_concern')
148+
144149
# Handle alternative naming
145150
if "content_type" in kwargs:
146151
kwargs["contentType"] = kwargs.pop("content_type")
@@ -204,9 +209,8 @@ def __setattr__(self, name, value):
204209
# them now.
205210
self._file[name] = value
206211
if self._closed:
207-
self._coll.files.update({"_id": self._file["_id"]},
208-
{"$set": {name: value}},
209-
**self._coll._get_wc_override())
212+
self._coll.files.update_one({"_id": self._file["_id"]},
213+
{"$set": {name: value}})
210214

211215
def __flush_data(self, data):
212216
"""Flush `data` to a chunk.
@@ -224,7 +228,7 @@ def __flush_data(self, data):
224228
"data": Binary(data)}
225229

226230
try:
227-
self._chunks.insert(chunk)
231+
self._chunks.insert_one(chunk)
228232
except DuplicateKeyError:
229233
self._raise_file_exists(self._file['_id'])
230234
self._chunk_number += 1
@@ -252,8 +256,7 @@ def __flush(self):
252256
self._file["length"] = self._position
253257
self._file["uploadDate"] = datetime.datetime.utcnow()
254258

255-
return self._coll.files.insert(self._file,
256-
**self._coll._get_wc_override())
259+
return self._coll.files.insert_one(self._file)
257260
except DuplicateKeyError:
258261
self._raise_file_exists(self._id)
259262

@@ -296,10 +299,6 @@ def write(self, data):
296299
if self._closed:
297300
raise ValueError("cannot write to a closed file")
298301

299-
# With w=0, 'filemd5' might run before the final chunks are written.
300-
if not self._coll.write_concern.acknowledged:
301-
raise InvalidOperation('Cannot write file to GridFS with w=0')
302-
303302
try:
304303
# file-like
305304
read = data.read

test/test_grid_file.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
GridOutCursor)
3333
from gridfs.errors import NoFile
3434
from pymongo import MongoClient
35-
from pymongo.errors import ConnectionFailure, InvalidOperation
35+
from pymongo.errors import ConfigurationError, ConnectionFailure
3636
from test import (client_knobs,
3737
IntegrationTest,
3838
host,
@@ -610,10 +610,8 @@ def test_grid_in_lazy_connect(self):
610610

611611
def test_unacknowledged(self):
612612
# w=0 is prohibited.
613-
infile = GridIn(rs_or_single_client(w=0).pymongo_test.fs)
614-
615-
with self.assertRaises(InvalidOperation):
616-
infile.write(b'data')
613+
with self.assertRaises(ConfigurationError):
614+
GridIn(rs_or_single_client(w=0).pymongo_test.fs)
617615

618616

619617
if __name__ == "__main__":

test/test_gridfs.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from bson.py3compat import u, StringIO, string_type
2828
from pymongo.mongo_client import MongoClient
29-
from pymongo.errors import ConnectionFailure, InvalidOperation
29+
from pymongo.errors import ConfigurationError, ConnectionFailure
3030
from pymongo.read_preferences import ReadPreference
3131
from gridfs.errors import (FileExists,
3232
NoFile)
@@ -433,10 +433,8 @@ def test_grid_in_non_int_chunksize(self):
433433

434434
def test_unacknowledged(self):
435435
# w=0 is prohibited.
436-
fs = gridfs.GridFS(rs_or_single_client(w=0).pymongo_test)
437-
438-
with self.assertRaises(InvalidOperation):
439-
fs.put(b'data')
436+
with self.assertRaises(ConfigurationError):
437+
gridfs.GridFS(rs_or_single_client(w=0).pymongo_test)
440438

441439

442440
class TestGridfsReplicaSet(TestReplicaSetClientBase):

0 commit comments

Comments
 (0)