Skip to content

Commit 19abd78

Browse files
committed
fixup consolidate dispatch registers
1 parent 3a0947d commit 19abd78

1 file changed

Lines changed: 17 additions & 38 deletions

File tree

space_packet_parser/generators/utils.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22

33
import datetime as dt
44
import io
5+
import logging
56
import socket
67
import time
78
from functools import singledispatch
89
from os import PathLike
910
from pathlib import Path
1011
from typing import Optional, Union
1112

12-
from space_packet_parser.common import logger
13+
logger = logging.getLogger(__name__)
1314

1415

1516
@singledispatch
1617
def _read_packet_file(packet_file) -> Union[bytes, io.BufferedIOBase, io.RawIOBase]:
17-
"""Read a packet file or file-like object and return a bytes-reader suitable for passing to a generator.
18+
"""Read a packet file or file-like object and return an object suitable for passing to a generator.
19+
20+
Specifically this function prepares the input for use with _setup_binary_reader.
1821
1922
Parameters
2023
----------
2124
packet_file : Union[str, Path, PathLike, BinaryIO, bytes]
2225
2326
Notes
2427
-----
25-
This function handles pathlike objects, turning them into objects compatible with generators.
28+
This function handles strings and pathlike objects but it reads the full file into memory for the generator.
2629
This alleviates the need for generators to internally handle opening and closing files.
30+
For a more memory efficient approach, pass an opened file object.
2731
"""
2832
raise OSError(f"Unable to open and read packet_file type: {type(packet_file)}")
2933

3034

31-
@_read_packet_file.register
32-
def _(packet_file: io.BufferedIOBase) -> io.BufferedIOBase:
33-
"""File-like object, this can be passed directly to a generator."""
34-
return packet_file
35-
36-
37-
@_read_packet_file.register
38-
def _(packet_file: io.RawIOBase) -> io.RawIOBase:
35+
@_read_packet_file.register(io.BufferedIOBase)
36+
@_read_packet_file.register(io.RawIOBase)
37+
def _(packet_file: Union[io.BufferedIOBase, io.RawIOBase]) -> Union[io.BufferedIOBase, io.RawIOBase]:
3938
"""File-like object, this can be passed directly to a generator."""
4039
return packet_file
4140

@@ -48,12 +47,7 @@ def _(packet_file: bytes) -> bytes:
4847

4948
@_read_packet_file.register
5049
def _(packet_file: str) -> bytes:
51-
"""String file path, open and read bytes.
52-
53-
Notes
54-
-----
55-
This will not work for PathLike objects that are not strings, use the Path or Path
56-
"""
50+
"""String file path, open and read bytes."""
5751
with open(packet_file, "rb") as f:
5852
return f.read()
5953

@@ -83,7 +77,7 @@ def _(packet_file: PathLike) -> bytes:
8377

8478

8579
@singledispatch
86-
def _setup_binary_reader(binary_data, buffer_read_size_bytes=None):
80+
def _setup_binary_reader(binary_data, buffer_read_size_bytes=None) -> tuple:
8781
"""Helper to set up reading from binary_data (file, socket, bytes) for a packet generator.
8882
8983
Parameters
@@ -104,24 +98,9 @@ def _setup_binary_reader(binary_data, buffer_read_size_bytes=None):
10498
raise OSError(f"Unrecognized data source: {binary_data}")
10599

106100

107-
@_setup_binary_reader.register
108-
def _(binary_data: io.BufferedIOBase, buffer_read_size_bytes=None):
109-
"""Set up a binary reader from a file-like object."""
110-
read_buffer = b""
111-
if buffer_read_size_bytes is None:
112-
# Default to a full read of the file
113-
buffer_read_size_bytes = -1
114-
total_length_bytes = binary_data.seek(0, io.SEEK_END)
115-
binary_data.seek(0, 0)
116-
read_bytes_from_source = binary_data.read
117-
logger.info(
118-
f"Creating packet generator from a filelike object, {binary_data}. Total length is {total_length_bytes} bytes"
119-
)
120-
return read_buffer, total_length_bytes, read_bytes_from_source, buffer_read_size_bytes
121-
122-
123-
@_setup_binary_reader.register
124-
def _(binary_data: io.RawIOBase, buffer_read_size_bytes=None):
101+
@_setup_binary_reader.register(io.BufferedIOBase)
102+
@_setup_binary_reader.register(io.RawIOBase)
103+
def _(binary_data: Union[io.BufferedIOBase, io.RawIOBase], buffer_read_size_bytes=None) -> tuple:
125104
"""Set up a binary reader from a file-like object."""
126105
read_buffer = b""
127106
if buffer_read_size_bytes is None:
@@ -137,7 +116,7 @@ def _(binary_data: io.RawIOBase, buffer_read_size_bytes=None):
137116

138117

139118
@_setup_binary_reader.register
140-
def _(binary_data: socket.socket, buffer_read_size_bytes=None):
119+
def _(binary_data: socket.socket, buffer_read_size_bytes=None) -> tuple:
141120
"""Set up a binary reader from a socket object."""
142121
read_buffer = b""
143122
total_length_bytes = None # We don't know how long it is
@@ -150,7 +129,7 @@ def _(binary_data: socket.socket, buffer_read_size_bytes=None):
150129

151130

152131
@_setup_binary_reader.register
153-
def _(binary_data: bytes, buffer_read_size_bytes=None):
132+
def _(binary_data: bytes, buffer_read_size_bytes=None) -> tuple:
154133
"""Set up a binary reader from a bytes object."""
155134
read_buffer = b""
156135
read_buffer = binary_data

0 commit comments

Comments
 (0)