Skip to content

Commit

Permalink
Merge pull request #567 from pressler-vsc/open_phase_history_obj
Browse files Browse the repository at this point in the history
(integration/1.3.60) Enable open_phase_history to take a file-like object
  • Loading branch information
pressler-vsc authored Oct 30, 2024
2 parents b5f07b1 + 29daf5e commit cbf9428
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Since essentially every (squash merge) commit corresponds to a release, specific
release points are not being annotated in GitHub.

## [1.3.60]
### Added
- Support for file objects in `sarpy.io.phase_history.converter.open_phase_history`
### Fixed
- Typo in SIDD 2.0+ DigitalElevationData/Geoposition/CoordinateSystemType enum (GGS -> GCS)
- Account for timezones in generated datetimes
Expand Down
2 changes: 1 addition & 1 deletion sarpy/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'__license__', '__copyright__']

from sarpy.__details__ import __classification__, _post_identifier
_version_number = '1.3.60a4'
_version_number = '1.3.60a5'

__version__ = _version_number + _post_identifier

Expand Down
9 changes: 5 additions & 4 deletions sarpy/io/phase_history/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
__author__ = "Thomas McCullough"

import os
from typing import Callable
from typing import BinaryIO, Callable, Union

from sarpy.io.general.base import SarpyIOError, check_for_openers
from sarpy.io.general.utils import is_file_like
from sarpy.io.phase_history.base import CPHDTypeReader


Expand Down Expand Up @@ -53,13 +54,13 @@ def parse_openers() -> None:
check_for_openers('sarpy.io.phase_history', register_opener)


def open_phase_history(file_name: str) -> CPHDTypeReader:
def open_phase_history(file_name: Union[str, BinaryIO]) -> CPHDTypeReader:
"""
Given a file, try to find and return the appropriate reader object.
Parameters
----------
file_name : str
file_name : str|BinaryIO
Returns
-------
Expand All @@ -70,7 +71,7 @@ def open_phase_history(file_name: str) -> CPHDTypeReader:
SarpyIOError
"""

if not os.path.exists(file_name):
if (not is_file_like(file_name)) and (not os.path.exists(file_name)):
raise SarpyIOError('File {} does not exist.'.format(file_name))
# parse openers, if not already done
parse_openers()
Expand Down
19 changes: 14 additions & 5 deletions tests/io/phase_history/test_cphd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import copy
import pathlib

Expand All @@ -21,12 +22,20 @@
CI2_CPHD = None


# contextlib.nullcontext not available in python 3.6
@contextlib.contextmanager
def nullcontext(enter_result=None):
yield enter_result


@pytest.mark.parametrize("cm", (nullcontext, lambda x: open(x, "rb")))
@pytest.mark.parametrize('cphd_path', CPHD_FILE_TYPES.get('CPHD',[]))
def test_cphd_read(cphd_path):
reader = sarpy.io.phase_history.converter.open_phase_history(cphd_path)
assert isinstance(reader, CPHDReader)
assert reader.reader_type == 'CPHD'
_assert_read_sizes(reader)
def test_cphd_read(cphd_path, cm):
with cm(cphd_path) as arg:
reader = sarpy.io.phase_history.converter.open_phase_history(arg)
assert isinstance(reader, CPHDReader)
assert reader.reader_type == 'CPHD'
_assert_read_sizes(reader)


def _assert_read_sizes(reader):
Expand Down

0 comments on commit cbf9428

Please sign in to comment.