-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
257 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import IO | ||
|
||
GZIP_MAGIC = b"\x1f\x8b" | ||
|
||
|
||
def first_n_bytes(fileobj: IO, n: int = 2) -> bytes: | ||
n_bytes = fileobj.read(n) | ||
fileobj.seek(0) | ||
return n_bytes if isinstance(n_bytes, bytes) else n_bytes.encode() | ||
|
||
|
||
def is_compressed(fileobj: IO) -> bool: | ||
n_bytes = first_n_bytes(fileobj) | ||
return n_bytes == GZIP_MAGIC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
11737-1 294 89 294 - 31171-1 292 87 292 205 205 0 NM:i:0 ms:i:410 AS:i:410 nn:i:0 tp:A:S cm:i:37 s1:i:193 de:f:0 rl:i:91 cg:Z:205M |
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from tempfile import TemporaryFile | ||
|
||
import pytest | ||
|
||
from pafpy.utils import GZIP_MAGIC, first_n_bytes, is_compressed | ||
|
||
|
||
class TestFirstNBytes: | ||
def test_empty_file_returns_empty(self): | ||
contents = b"" | ||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
actual = first_n_bytes(fileobj) | ||
expected = contents | ||
|
||
assert actual == expected | ||
|
||
def test_one_byte_returns_one_byte(self): | ||
contents = b"1" | ||
n = 2 | ||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
actual = first_n_bytes(fileobj, n=n) | ||
expected = contents | ||
|
||
assert actual == expected | ||
|
||
def test_two_bytes_returns_two_bytes(self): | ||
contents = b"12" | ||
n = 2 | ||
|
||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
actual = first_n_bytes(fileobj, n=n) | ||
expected = contents | ||
|
||
assert actual == expected | ||
|
||
def test_more_bytes_returns_two_bytes(self): | ||
contents = b"12345" | ||
n = 2 | ||
|
||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
actual = first_n_bytes(fileobj, n=n) | ||
expected = contents[:n] | ||
|
||
assert actual == expected | ||
|
||
def test_text_stream_returns_bytes(self): | ||
contents = "12345" | ||
n = 3 | ||
|
||
with TemporaryFile("w+") as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
actual = first_n_bytes(fileobj, n=n) | ||
expected = contents[:n].encode() | ||
|
||
assert actual == expected | ||
|
||
def test_nonreadable_object_raises_error(self): | ||
fileobj = b"12345" | ||
n = 3 | ||
|
||
with pytest.raises(AttributeError) as err: | ||
first_n_bytes(fileobj, n=n) | ||
assert err.match("has no attribute 'read'") | ||
|
||
|
||
class TestIsCompressed: | ||
def test_empty_file(self): | ||
contents = b"" | ||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
assert not is_compressed(fileobj) | ||
|
||
def test_non_compressed(self): | ||
contents = b"not compressed" | ||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
assert not is_compressed(fileobj) | ||
|
||
def test_compressed(self): | ||
contents = GZIP_MAGIC + b" is compressed" | ||
with TemporaryFile() as fileobj: | ||
fileobj.write(contents) | ||
fileobj.seek(0) | ||
assert is_compressed(fileobj) |