Class to read data out of GridFS.
| 1439 | |
| 1440 | |
| 1441 | class GridOut(GRIDOUT_BASE_CLASS): # type: ignore |
| 1442 | |
| 1443 | """Class to read data out of GridFS.""" |
| 1444 | |
| 1445 | def __init__( |
| 1446 | self, |
| 1447 | root_collection: Collection[Any], |
| 1448 | file_id: Optional[int] = None, |
| 1449 | file_document: Optional[Any] = None, |
| 1450 | session: Optional[ClientSession] = None, |
| 1451 | ) -> None: |
| 1452 | """Read a file from GridFS |
| 1453 | |
| 1454 | Application developers should generally not need to |
| 1455 | instantiate this class directly - instead see the methods |
| 1456 | provided by :class:`~gridfs.GridFS`. |
| 1457 | |
| 1458 | Either `file_id` or `file_document` must be specified, |
| 1459 | `file_document` will be given priority if present. Raises |
| 1460 | :class:`TypeError` if `root_collection` is not an instance of |
| 1461 | :class:`~pymongo.collection.Collection`. |
| 1462 | |
| 1463 | :param root_collection: root collection to read from |
| 1464 | :param file_id: value of ``"_id"`` for the file to read |
| 1465 | :param file_document: file document from |
| 1466 | `root_collection.files` |
| 1467 | :param session: a |
| 1468 | :class:`~pymongo.client_session.ClientSession` to use for all |
| 1469 | commands |
| 1470 | |
| 1471 | .. versionchanged:: 3.8 |
| 1472 | For better performance and to better follow the GridFS spec, |
| 1473 | :class:`GridOut` now uses a single cursor to read all the chunks in |
| 1474 | the file. |
| 1475 | |
| 1476 | .. versionchanged:: 3.6 |
| 1477 | Added ``session`` parameter. |
| 1478 | |
| 1479 | .. versionchanged:: 3.0 |
| 1480 | Creating a GridOut does not immediately retrieve the file metadata |
| 1481 | from the server. Metadata is fetched when first needed. |
| 1482 | """ |
| 1483 | if not isinstance(root_collection, Collection): |
| 1484 | raise TypeError( |
| 1485 | f"root_collection must be an instance of Collection, not {type(root_collection)}" |
| 1486 | ) |
| 1487 | _disallow_transactions(session) |
| 1488 | |
| 1489 | root_collection = _clear_entity_type_registry(root_collection) |
| 1490 | |
| 1491 | super().__init__() |
| 1492 | |
| 1493 | self._chunks = root_collection.chunks |
| 1494 | self._files = root_collection.files |
| 1495 | self._file_id = file_id |
| 1496 | self._buffer = EMPTY |
| 1497 | # Start position within the current buffered chunk. |
| 1498 | self._buffer_pos = 0 |