Skip to content

Commit 4847621

Browse files
committed
Address review
1 parent d1e0ed0 commit 4847621

File tree

4 files changed

+122
-122
lines changed

4 files changed

+122
-122
lines changed

gridfs/asynchronous/grid_file.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,17 +1181,17 @@ def __setattr__(self, name: str, value: Any) -> None:
11811181
if name in self.__dict__ or name in self.__class__.__dict__:
11821182
object.__setattr__(self, name, value)
11831183
else:
1184-
if _IS_SYNC:
1185-
# All other attributes are part of the document in db.fs.files.
1186-
# Store them to be sent to server on close() or if closed, send
1187-
# them now.
1188-
self._file[name] = value
1189-
if self._closed:
1184+
# All other attributes are part of the document in db.fs.files.
1185+
# Store them to be sent to server on close() or if closed, send
1186+
# them now.
1187+
self._file[name] = value
1188+
if self._closed:
1189+
if _IS_SYNC:
11901190
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
1191-
else:
1192-
raise AttributeError(
1193-
"AsyncGridIn does not support __setattr__. Use AsyncGridIn.set() instead"
1194-
)
1191+
else:
1192+
raise AttributeError(
1193+
"AsyncGridIn does not support __setattr__ after being closed(). Set the attribute before closing the file or use AsyncGridIn.set() instead"
1194+
)
11951195

11961196
async def set(self, name: str, value: Any) -> None:
11971197
self._file[name] = value
@@ -1494,6 +1494,32 @@ async def __anext__(self) -> bytes:
14941494
async def to_list(self) -> list[bytes]:
14951495
return [x async for x in self] # noqa: C416, RUF100
14961496

1497+
async def readline(self, size: int = -1) -> bytes:
1498+
"""Read one line or up to `size` bytes from the file.
1499+
1500+
:param size: the maximum number of bytes to read
1501+
"""
1502+
return await self._read_size_or_line(size=size, line=True)
1503+
1504+
async def readlines(self, size: int = -1) -> list[bytes]:
1505+
"""Read one line or up to `size` bytes from the file.
1506+
1507+
:param size: the maximum number of bytes to read
1508+
"""
1509+
await self.open()
1510+
lines = []
1511+
remainder = int(self.length) - self._position
1512+
bytes_read = 0
1513+
while remainder > 0:
1514+
line = await self._read_size_or_line(line=True)
1515+
bytes_read += len(line)
1516+
lines.append(line)
1517+
remainder = int(self.length) - self._position
1518+
if 0 < size < bytes_read:
1519+
break
1520+
1521+
return lines
1522+
14971523
async def open(self) -> None:
14981524
if not self._file:
14991525
_disallow_transactions(self._session)
@@ -1613,32 +1639,6 @@ async def read(self, size: int = -1) -> bytes:
16131639
"""
16141640
return await self._read_size_or_line(size=size)
16151641

1616-
async def readline(self, size: int = -1) -> bytes:
1617-
"""Read one line or up to `size` bytes from the file.
1618-
1619-
:param size: the maximum number of bytes to read
1620-
"""
1621-
return await self._read_size_or_line(size=size, line=True)
1622-
1623-
async def readlines(self, size: int = -1) -> list[bytes]:
1624-
"""Read one line or up to `size` bytes from the file.
1625-
1626-
:param size: the maximum number of bytes to read
1627-
"""
1628-
await self.open()
1629-
lines = []
1630-
remainder = int(self.length) - self._position
1631-
bytes_read = 0
1632-
while remainder > 0:
1633-
line = await self._read_size_or_line(line=True)
1634-
bytes_read += len(line)
1635-
lines.append(line)
1636-
remainder = int(self.length) - self._position
1637-
if 0 < size < bytes_read:
1638-
break
1639-
1640-
return lines
1641-
16421642
def tell(self) -> int:
16431643
"""Return the current position of this file."""
16441644
return self._position

gridfs/synchronous/grid_file.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,17 +1171,17 @@ def __setattr__(self, name: str, value: Any) -> None:
11711171
if name in self.__dict__ or name in self.__class__.__dict__:
11721172
object.__setattr__(self, name, value)
11731173
else:
1174-
if _IS_SYNC:
1175-
# All other attributes are part of the document in db.fs.files.
1176-
# Store them to be sent to server on close() or if closed, send
1177-
# them now.
1178-
self._file[name] = value
1179-
if self._closed:
1174+
# All other attributes are part of the document in db.fs.files.
1175+
# Store them to be sent to server on close() or if closed, send
1176+
# them now.
1177+
self._file[name] = value
1178+
if self._closed:
1179+
if _IS_SYNC:
11801180
self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}})
1181-
else:
1182-
raise AttributeError(
1183-
"GridIn does not support __setattr__. Use GridIn.set() instead"
1184-
)
1181+
else:
1182+
raise AttributeError(
1183+
"GridIn does not support __setattr__ after being closed(). Set the attribute before closing the file or use GridIn.set() instead"
1184+
)
11851185

11861186
def set(self, name: str, value: Any) -> None:
11871187
self._file[name] = value
@@ -1484,6 +1484,32 @@ def __next__(self) -> bytes:
14841484
def to_list(self) -> list[bytes]:
14851485
return [x for x in self] # noqa: C416, RUF100
14861486

1487+
def readline(self, size: int = -1) -> bytes:
1488+
"""Read one line or up to `size` bytes from the file.
1489+
1490+
:param size: the maximum number of bytes to read
1491+
"""
1492+
return self._read_size_or_line(size=size, line=True)
1493+
1494+
def readlines(self, size: int = -1) -> list[bytes]:
1495+
"""Read one line or up to `size` bytes from the file.
1496+
1497+
:param size: the maximum number of bytes to read
1498+
"""
1499+
self.open()
1500+
lines = []
1501+
remainder = int(self.length) - self._position
1502+
bytes_read = 0
1503+
while remainder > 0:
1504+
line = self._read_size_or_line(line=True)
1505+
bytes_read += len(line)
1506+
lines.append(line)
1507+
remainder = int(self.length) - self._position
1508+
if 0 < size < bytes_read:
1509+
break
1510+
1511+
return lines
1512+
14871513
def open(self) -> None:
14881514
if not self._file:
14891515
_disallow_transactions(self._session)
@@ -1603,32 +1629,6 @@ def read(self, size: int = -1) -> bytes:
16031629
"""
16041630
return self._read_size_or_line(size=size)
16051631

1606-
def readline(self, size: int = -1) -> bytes:
1607-
"""Read one line or up to `size` bytes from the file.
1608-
1609-
:param size: the maximum number of bytes to read
1610-
"""
1611-
return self._read_size_or_line(size=size, line=True)
1612-
1613-
def readlines(self, size: int = -1) -> list[bytes]:
1614-
"""Read one line or up to `size` bytes from the file.
1615-
1616-
:param size: the maximum number of bytes to read
1617-
"""
1618-
self.open()
1619-
lines = []
1620-
remainder = int(self.length) - self._position
1621-
bytes_read = 0
1622-
while remainder > 0:
1623-
line = self._read_size_or_line(line=True)
1624-
bytes_read += len(line)
1625-
lines.append(line)
1626-
remainder = int(self.length) - self._position
1627-
if 0 < size < bytes_read:
1628-
break
1629-
1630-
return lines
1631-
16321632
def tell(self) -> int:
16331633
"""Return the current position of this file."""
16341634
return self._position

test/asynchronous/test_grid_file.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -413,30 +413,31 @@ async def test_multi_chunk_file(self):
413413
g = AsyncGridOut(self.db.fs, f._id)
414414
self.assertEqual(random_string, await g.read())
415415

416-
# # TODO: https://jira.mongodb.org/browse/PYTHON-4708
417-
# async def test_small_chunks(self):
418-
# self.files = 0
419-
# self.chunks = 0
420-
#
421-
# async def helper(data):
422-
# f = AsyncGridIn(self.db.fs, chunkSize=1)
423-
# await f.write(data)
424-
# await f.close()
425-
#
426-
# self.files += 1
427-
# self.chunks += len(data)
428-
#
429-
# self.assertEqual(self.files, await self.db.fs.files.count_documents({}))
430-
# self.assertEqual(self.chunks, await self.db.fs.chunks.count_documents({}))
431-
#
432-
# g = AsyncGridOut(self.db.fs, f._id)
433-
# self.assertEqual(data, await g.read())
434-
#
435-
# g = AsyncGridOut(self.db.fs, f._id)
436-
# self.assertEqual(data, await g.read(10) + await g.read(10))
437-
# return True
438-
#
439-
# qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
416+
# TODO: https://jira.mongodb.org/browse/PYTHON-4708
417+
@async_client_context.require_sync
418+
async def test_small_chunks(self):
419+
self.files = 0
420+
self.chunks = 0
421+
422+
async def helper(data):
423+
f = AsyncGridIn(self.db.fs, chunkSize=1)
424+
await f.write(data)
425+
await f.close()
426+
427+
self.files += 1
428+
self.chunks += len(data)
429+
430+
self.assertEqual(self.files, await self.db.fs.files.count_documents({}))
431+
self.assertEqual(self.chunks, await self.db.fs.chunks.count_documents({}))
432+
433+
g = AsyncGridOut(self.db.fs, f._id)
434+
self.assertEqual(data, await g.read())
435+
436+
g = AsyncGridOut(self.db.fs, f._id)
437+
self.assertEqual(data, await g.read(10) + await g.read(10))
438+
return True
439+
440+
qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
440441

441442
async def test_seek(self):
442443
f = AsyncGridIn(self.db.fs, chunkSize=3)
@@ -850,7 +851,6 @@ async def test_zip(self):
850851
self.assertEqual(1, await self.db.fs.chunks.count_documents({}))
851852

852853
g = AsyncGridOut(self.db.fs, f._id)
853-
await g.open()
854854
z = zipfile.ZipFile(g)
855855
self.assertSequenceEqual(z.namelist(), ["test.txt"])
856856
self.assertEqual(z.read("test.txt"), b"hello world")

test/test_grid_file.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -411,30 +411,31 @@ def test_multi_chunk_file(self):
411411
g = GridOut(self.db.fs, f._id)
412412
self.assertEqual(random_string, g.read())
413413

414-
# # TODO: https://jira.mongodb.org/browse/PYTHON-4708
415-
# def test_small_chunks(self):
416-
# self.files = 0
417-
# self.chunks = 0
418-
#
419-
# def helper(data):
420-
# f = GridIn(self.db.fs, chunkSize=1)
421-
# f.write(data)
422-
# f.close()
423-
#
424-
# self.files += 1
425-
# self.chunks += len(data)
426-
#
427-
# self.assertEqual(self.files, self.db.fs.files.count_documents({}))
428-
# self.assertEqual(self.chunks, self.db.fs.chunks.count_documents({}))
429-
#
430-
# g = GridOut(self.db.fs, f._id)
431-
# self.assertEqual(data, g.read())
432-
#
433-
# g = GridOut(self.db.fs, f._id)
434-
# self.assertEqual(data, g.read(10) + g.read(10))
435-
# return True
436-
#
437-
# qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
414+
# TODO: https://jira.mongodb.org/browse/PYTHON-4708
415+
@client_context.require_sync
416+
def test_small_chunks(self):
417+
self.files = 0
418+
self.chunks = 0
419+
420+
def helper(data):
421+
f = GridIn(self.db.fs, chunkSize=1)
422+
f.write(data)
423+
f.close()
424+
425+
self.files += 1
426+
self.chunks += len(data)
427+
428+
self.assertEqual(self.files, self.db.fs.files.count_documents({}))
429+
self.assertEqual(self.chunks, self.db.fs.chunks.count_documents({}))
430+
431+
g = GridOut(self.db.fs, f._id)
432+
self.assertEqual(data, g.read())
433+
434+
g = GridOut(self.db.fs, f._id)
435+
self.assertEqual(data, g.read(10) + g.read(10))
436+
return True
437+
438+
qcheck.check_unittest(self, helper, qcheck.gen_string(qcheck.gen_range(0, 20)))
438439

439440
def test_seek(self):
440441
f = GridIn(self.db.fs, chunkSize=3)
@@ -848,7 +849,6 @@ def test_zip(self):
848849
self.assertEqual(1, self.db.fs.chunks.count_documents({}))
849850

850851
g = GridOut(self.db.fs, f._id)
851-
g.open()
852852
z = zipfile.ZipFile(g)
853853
self.assertSequenceEqual(z.namelist(), ["test.txt"])
854854
self.assertEqual(z.read("test.txt"), b"hello world")

0 commit comments

Comments
 (0)