Skip to content

Commit bab1d8e

Browse files
os_ops::readlines is updated (revision)
1 parent cc4361c commit bab1d8e

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

testgres/operations/local_ops.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,26 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
352352
Read lines from a local file.
353353
If num_lines is greater than 0, only the last num_lines lines will be read.
354354
"""
355+
assert type(num_lines) == int # noqa: E721
356+
assert type(filename) == str # noqa: E721
357+
assert type(binary) == bool # noqa: E721
358+
assert encoding is None or type(encoding) == str # noqa: E721
355359
assert num_lines >= 0
360+
361+
if binary:
362+
assert encoding is None
363+
pass
364+
elif encoding is None:
365+
encoding = get_default_encoding()
366+
assert type(encoding) == str # noqa: E721
367+
else:
368+
assert type(encoding) == str # noqa: E721
369+
pass
370+
356371
mode = 'rb' if binary else 'r'
357372
if num_lines == 0:
358373
with open(filename, mode, encoding=encoding) as file: # open in binary mode
359374
return file.readlines()
360-
361375
else:
362376
bufsize = 8192
363377
buffers = 1

testgres/operations/remote_ops.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,37 @@ def _read__binary(self, filename):
484484
return content
485485

486486
def readlines(self, filename, num_lines=0, binary=False, encoding=None):
487+
assert type(num_lines) == int # noqa: E721
488+
assert type(filename) == str # noqa: E721
489+
assert type(binary) == bool # noqa: E721
490+
assert encoding is None or type(encoding) == str # noqa: E721
491+
487492
if num_lines > 0:
488-
cmd = "tail -n {} {}".format(num_lines, filename)
493+
cmd = ["tail", "-n", str(num_lines), filename]
494+
else:
495+
cmd = ["cat", filename]
496+
497+
if binary:
498+
assert encoding is None
499+
pass
500+
elif encoding is None:
501+
encoding = get_default_encoding()
502+
assert type(encoding) == str # noqa: E721
489503
else:
490-
cmd = "cat {}".format(filename)
504+
assert type(encoding) == str # noqa: E721
505+
pass
491506

492507
result = self.exec_command(cmd, encoding=encoding)
508+
assert result is not None
493509

494-
if not binary and result:
495-
lines = result.decode(encoding or get_default_encoding()).splitlines()
510+
if binary:
511+
assert type(result) == bytes # noqa: E721
512+
lines = result.splitlines()
496513
else:
514+
assert type(result) == str # noqa: E721
497515
lines = result.splitlines()
498516

517+
assert type(lines) == list # noqa: E721
499518
return lines
500519

501520
def read_binary(self, filename, offset):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy