MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / write

Method write

gridfs/synchronous/grid_file.py:1326–1385  ·  view source on GitHub ↗

Write data to the file. There is no return value. `data` can be either a string of bytes or a file-like object (implementing :meth:`read`). If the file has an :attr:`encoding` attribute, `data` can also be a :class:`str` instance, which will be encoded as :at

(self, data: Any)

Source from the content-addressed store, hash-verified

1324 return False
1325
1326 def write(self, data: Any) -> None:
1327 """Write data to the file. There is no return value.
1328
1329 `data` can be either a string of bytes or a file-like object
1330 (implementing :meth:`read`). If the file has an
1331 :attr:`encoding` attribute, `data` can also be a
1332 :class:`str` instance, which will be encoded as
1333 :attr:`encoding` before being written.
1334
1335 Due to buffering, the data may not actually be written to the
1336 database until the :meth:`close` method is called. Raises
1337 :class:`ValueError` if this file is already closed. Raises
1338 :class:`TypeError` if `data` is not an instance of
1339 :class:`bytes`, a file-like object, or an instance of :class:`str`.
1340 Unicode data is only allowed if the file has an :attr:`encoding`
1341 attribute.
1342
1343 :param data: string of bytes or file-like object to be written
1344 to the file
1345 """
1346 if self._closed:
1347 raise ValueError("cannot write to a closed file")
1348
1349 try:
1350 # file-like
1351 read = data.read
1352 except AttributeError:
1353 # string
1354 if not isinstance(data, (str, bytes)):
1355 raise TypeError("can only write strings or file-like objects") from None
1356 if isinstance(data, str):
1357 try:
1358 data = data.encode(self.encoding)
1359 except AttributeError:
1360 raise TypeError(
1361 "must specify an encoding for file in order to write str"
1362 ) from None
1363 read = io.BytesIO(data).read
1364
1365 if inspect.iscoroutinefunction(read):
1366 self._write_async(read)
1367 else:
1368 if self._buffer.tell() > 0:
1369 # Make sure to flush only when _buffer is complete
1370 space = self.chunk_size - self._buffer.tell()
1371 if space:
1372 try:
1373 to_write = read(space)
1374 except BaseException:
1375 self.abort()
1376 raise
1377 self._buffer.write(to_write)
1378 if len(to_write) < space:
1379 return # EOF or incomplete
1380 self._flush_buffer()
1381 to_write = read(self.chunk_size)
1382 while to_write and len(to_write) == self.chunk_size:
1383 self._flush_data(to_write)

Callers 15

writelinesMethod · 0.95
test_basicMethod · 0.95
test_md5Method · 0.95
test_write_file_likeMethod · 0.95
test_closeMethod · 0.95
test_closedMethod · 0.95
test_multi_chunk_fileMethod · 0.95
helperMethod · 0.95
test_seekMethod · 0.95

Calls 7

_write_asyncMethod · 0.95
abortMethod · 0.95
_flush_bufferMethod · 0.95
_flush_dataMethod · 0.95
encodeMethod · 0.80
readFunction · 0.50
tellMethod · 0.45

Tested by 15

test_basicMethod · 0.76
test_md5Method · 0.76
test_write_file_likeMethod · 0.76
test_closeMethod · 0.76
test_closedMethod · 0.76
test_multi_chunk_fileMethod · 0.76
helperMethod · 0.76
test_seekMethod · 0.76
test_tellMethod · 0.76