Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some cleanup: Allow all IOBase derived file streams #1106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/asammdf/mdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from enum import Enum
from functools import reduce
import gzip
from io import BufferedIOBase, BytesIO
from io import IOBase
import logging
import os
from pathlib import Path
Expand Down Expand Up @@ -233,10 +233,9 @@ def __init__(

if name:
if is_file_like(name):
if isinstance(name, (BytesIO, BufferedIOBase)):
if isinstance(name, IOBase):
original_name = None
file_stream = name
do_close = False

elif isinstance(name, bz2.BZ2File):
original_name = Path(name._fp.name)
Expand All @@ -245,17 +244,13 @@ def __init__(
file_stream = open(tmp_name, "rb")
name = tmp_name

do_close = True

elif isinstance(name, gzip.GzipFile):
original_name = Path(name.name)
tmp_name = get_temporary_filename(original_name, dir=temporary_folder)
tmp_name.write_bytes(name.read())
file_stream = open(tmp_name, "rb")
name = tmp_name

do_close = True

elif FSSPEF_AVAILABLE and isinstance(name, fsspec.spec.AbstractBufferedFile):
original_name = "AzureFile"
file_stream = name
Expand All @@ -282,7 +277,6 @@ def __init__(
move(output, name)

file_stream = open(name, "rb")
do_close = True

else:
name = original_name = Path(name)
Expand All @@ -305,22 +299,20 @@ def __init__(
move(output, name)

file_stream = open(name, "rb")
do_close = True

file_stream.seek(0)
magic_header = file_stream.read(8)

if magic_header.strip() not in (b"MDF", b"UnFinMF"):
if do_close:
file_stream.close()
file_stream.close()
raise MdfException(f'"{name}" is not a valid ASAM MDF file: magic header is {magic_header}')

file_stream.seek(8)
version = file_stream.read(4).decode("ascii").strip(" \0")
if not version:
_, version = get_measurement_timestamp_and_version(file_stream)

if do_close:
if not isinstance(name, IOBase):
file_stream.close()

kwargs["original_name"] = original_name
Expand Down
Loading