Skip to content

Commit 22f72a0

Browse files
OsOperations::read_binary supports 'size' argument (#117)
Declarations: def read_binary( self, filename: str, offset: int, size: typing.Optional[int] = None, ) -> bytes: Tests are added.
1 parent ec970ce commit 22f72a0

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

src/local_ops.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,24 @@ def readlines(
706706
) # Adjust buffer size
707707
return
708708

709-
def read_binary(self, filename: str, offset: int) -> bytes:
709+
def read_binary(
710+
self,
711+
filename: str,
712+
offset: int,
713+
size: typing.Optional[int] = None,
714+
) -> bytes:
710715
assert type(filename) is str
711716
assert type(offset) is int
717+
assert size is None or type(size) is int
712718

713719
if offset < 0:
714720
raise ValueError("Negative 'offset' is not supported.")
721+
if size is not None and size < 0:
722+
raise ValueError("Negative 'size' is not supported.")
715723

716724
with open(filename, 'rb') as file: # open in a binary mode
717725
file.seek(offset, os.SEEK_SET)
718-
r = file.read()
726+
r = file.read(size)
719727
assert type(r) is bytes
720728
return r
721729

src/os_ops.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,17 @@ def readlines(
292292
assert num_lines >= 0
293293
raise NotImplementedError()
294294

295-
def read_binary(self, filename: str, offset: int) -> bytes:
295+
def read_binary(
296+
self,
297+
filename: str,
298+
offset: int,
299+
size: typing.Optional[int] = None,
300+
) -> bytes:
296301
assert type(filename) is str
297302
assert type(offset) is int
303+
assert size is None or type(size) is int
298304
assert offset >= 0
305+
assert size is None or size >= 0
299306
raise NotImplementedError()
300307

301308
def isfile(self, filename: str) -> bool:

src/remote_ops.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,27 @@ def readlines(
931931
assert type(lines) is list
932932
return lines
933933

934-
def read_binary(self, filename: str, offset: int) -> bytes:
934+
def read_binary(
935+
self,
936+
filename: str,
937+
offset: int,
938+
size: typing.Optional[int] = None,
939+
) -> bytes:
935940
assert type(filename) is str
936941
assert type(offset) is int
942+
assert size is None or type(size) is int
937943

938944
if offset < 0:
939945
raise ValueError("Negative 'offset' is not supported.")
946+
if size is not None and size < 0:
947+
raise ValueError("Negative 'size' is not supported.")
940948

941949
filename_q = __class__._quote_path(filename)
942950
cmd_p = ["tail", "-c", "+{}".format(offset + 1), filename_q]
943951

952+
if size is not None:
953+
cmd_p.append("| head -c {}".format(size))
954+
944955
cmd = " ".join(cmd_p)
945956

946957
r = self.exec_command(cmd)

tests/test_os_ops_common.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,42 @@ def test_read_binary__spec(
12501250
assert type(response5) is bytes
12511251
assert len(response5) == 0
12521252

1253+
response6 = os_ops.read_binary(filename, 0, 1)
1254+
assert type(response6) is bytes
1255+
assert len(response6) == 1
1256+
assert response6 == response0[:1]
1257+
1258+
r = os_ops.read_binary(filename, 10, 7)
1259+
assert type(r) is bytes
1260+
assert len(r) == 7
1261+
assert r == response0[10:17]
1262+
1263+
r = os_ops.read_binary(filename, 10, len(response0))
1264+
assert type(r) is bytes
1265+
assert len(r) == len(response0) - 10
1266+
assert r == response0[10:]
1267+
1268+
r = os_ops.read_binary(filename, 10, 2 * len(response0))
1269+
assert type(r) is bytes
1270+
assert len(r) == len(response0) - 10
1271+
assert r == response0[10:]
1272+
1273+
r = os_ops.read_binary(filename, len(response0) - 1, 2 * len(response0))
1274+
assert type(r) is bytes
1275+
assert len(r) == 1
1276+
assert r == response0[-1:]
1277+
assert r[0] == response0[-1]
1278+
1279+
r = os_ops.read_binary(filename, len(response0), 1)
1280+
assert type(r) is bytes
1281+
assert len(r) == 0
1282+
assert r == b''
1283+
1284+
r = os_ops.read_binary(filename, len(response0) + 1, 1)
1285+
assert type(r) is bytes
1286+
assert len(r) == 0
1287+
assert r == b''
1288+
12531289
os_ops.remove_file(filename)
12541290
return
12551291

@@ -1278,6 +1314,31 @@ def test_read_binary__spec__negative_offset(
12781314
os_ops.remove_file(filename)
12791315
return
12801316

1317+
def test_read_binary__spec__negative_size(
1318+
self,
1319+
os_ops_descr: OsOpsDescr,
1320+
name_with_surprize: tagNameWithSurprize,
1321+
):
1322+
"""
1323+
Test OsOperations::read_binary with negative size.
1324+
"""
1325+
assert type(os_ops_descr) is OsOpsDescr
1326+
assert isinstance(os_ops_descr.os_ops, OsOperations)
1327+
assert type(name_with_surprize) is __class__.tagNameWithSurprize
1328+
1329+
os_ops = os_ops_descr.os_ops
1330+
assert isinstance(os_ops, OsOperations)
1331+
1332+
filename = os_ops.mkstemp(name_with_surprize.value)
1333+
1334+
with pytest.raises(
1335+
ValueError,
1336+
match=re.escape("Negative 'size' is not supported.")):
1337+
os_ops.read_binary(filename, 0, size=-1)
1338+
1339+
os_ops.remove_file(filename)
1340+
return
1341+
12811342
def test_get_file_size(
12821343
self,
12831344
os_ops_descr: OsOpsDescr,

0 commit comments

Comments
 (0)