Skip to content

Commit 5b233ef

Browse files
committed
Cleanup interspersed logic
1 parent 521e946 commit 5b233ef

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/sssom/parsers.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def _open_input(p: PathOrIO) -> TextIO:
9494
if isinstance(p, str) and (p.startswith("http://") or p.startswith("https://")):
9595
# It's a URL
9696
data = requests.get(p, timeout=30).content
97+
# TODO handle gzipped remote content
9798
return io.StringIO(data.decode("utf-8"))
9899

99100
# squash a path to a string so we don't have to duplicate logic below
@@ -151,13 +152,20 @@ def _separate_metadata_and_table_from_stream(stream: TextIO):
151152
return table_component, metadata_component
152153

153154

154-
def _read_pandas_and_metadata(stream: TextIO, sep: Optional[str] = None):
155+
def _read_pandas_and_metadata(file_path: PathOrIO, sep: Optional[str] = None):
155156
"""Read a tabular data file by wrapping func:`pd.read_csv` to handles comment lines correctly.
156157
157-
:param stream: The file to read. If no separator is given, this file should be named.
158+
:param file_path: The file path or stream to read
158159
:param sep: File separator for pandas
159160
:return: A pandas dataframe
160161
"""
162+
if sep is None:
163+
sep = _infer_separator(file_path)
164+
165+
if isinstance(file_path, (str, Path)):
166+
raise_for_bad_path(file_path)
167+
168+
stream = _open_input(file_path)
161169
table_stream, metadata_stream = _separate_metadata_and_table_from_stream(stream)
162170

163171
try:
@@ -317,12 +325,8 @@ def parse_sssom_table(
317325
"""
318326
if kwargs:
319327
logging.warning("unhandled keyword arguments passed: %s", kwargs)
320-
if isinstance(file_path, Path) or isinstance(file_path, str):
321-
raise_for_bad_path(file_path)
322-
stream = _open_input(file_path)
323-
if sep is None:
324-
sep = _infer_separator(file_path)
325-
df, sssom_metadata = _read_pandas_and_metadata(stream, sep)
328+
329+
df, sssom_metadata = _read_pandas_and_metadata(file_path, sep)
326330
if meta is None:
327331
meta = {}
328332

0 commit comments

Comments
 (0)