mapping-commons/sssom-py

parse_sssom_table should take as an input a String, URI, or filepath

matentzn opened this issue · 2 comments

Right now, parse_sssom_table can only do URI and filepath, but for various use cases like the SSSOM Validator UI, we need to be able to read the file from string.

I want to use this opportunity to clean up this method a bit, which is super convoluted as well.

cthoyt commented

This appears to be done already

def _open_input(input: Union[str, Path, TextIO]) -> io.StringIO:
"""Transform a URL, a filepath (from pathlib), or a string (with file contents) to a StringIO object.
:param input: A string representing a URL, a filepath, or file contents,
or a Path object representing a filepath.
:return: A StringIO object containing the input data.
"""
# If the import already is a StrinIO, return it
if isinstance(input, io.StringIO):
return input
elif isinstance(input, Path):
input = str(input)
if isinstance(input, str):
if input.startswith("http://") or input.startswith("https://"):
# It's a URL
data = requests.get(input, timeout=30).content
return io.StringIO(data.decode("utf-8"))
elif "\n" in input or "\r" in input:
# It's string data
return io.StringIO(input)
else:
# It's a local file path
with open(input, "r") as file:
file_content = file.read()
return io.StringIO(file_content)
raise IOError(f"Could not determine the type of input {input}")

Yep thats right!