@@ -293,30 +293,38 @@ def __exit__(self, exc_type, exc_val, exc_tb):
293
293
class GridOut (object ):
294
294
"""Class to read data out of GridFS.
295
295
"""
296
- def __init__ (self , root_collection , file_id ):
296
+ def __init__ (self , root_collection , file_id = None , file_document = None ):
297
297
"""Read a file from GridFS
298
298
299
299
Application developers should generally not need to
300
300
instantiate this class directly - instead see the methods
301
301
provided by :class:`~gridfs.GridFS`.
302
302
303
- Raises :class:`TypeError` if `root_collection` is not an instance of
303
+ Either `file_id` or `file_document` must be specified,
304
+ `file_document` will be given priority if present. Raises
305
+ :class:`TypeError` if `root_collection` is not an instance of
304
306
:class:`~pymongo.collection.Collection`.
305
307
306
308
:Parameters:
307
309
- `root_collection`: root collection to read from
308
310
- `file_id`: value of ``"_id"`` for the file to read
311
+ - `file_document`: file document from `root_collection.files`
312
+
313
+ .. versionadded:: 1.8.1+
314
+ The `file_document` parameter.
309
315
"""
310
316
if not isinstance (root_collection , Collection ):
311
317
raise TypeError ("root_collection must be an "
312
318
"instance of Collection" )
313
319
314
320
self .__chunks = root_collection .chunks
315
- self ._file = root_collection .files .find_one ({"_id" : file_id })
321
+
322
+ files = root_collection .files
323
+ self ._file = file_document or files .find_one ({"_id" : file_id })
316
324
317
325
if not self ._file :
318
326
raise NoFile ("no file in gridfs collection %r with _id %r" %
319
- (root_collection , file_id ))
327
+ (files , file_id ))
320
328
321
329
self .__buffer = ""
322
330
self .__position = 0
0 commit comments