From a54cac3b1866531379c91813a6f2fb989a149a38 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Mon, 23 Oct 2023 12:34:41 -0700 Subject: [PATCH 1/2] Allow header text files to be in JSON format This allows JSON sidecar files to be used with astrometadata translate. --- python/astro_metadata_translator/file_helpers.py | 6 +++--- python/astro_metadata_translator/tests.py | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/python/astro_metadata_translator/file_helpers.py b/python/astro_metadata_translator/file_helpers.py index 30968e4e..b9c1ba56 100644 --- a/python/astro_metadata_translator/file_helpers.py +++ b/python/astro_metadata_translator/file_helpers.py @@ -127,8 +127,8 @@ def read_basic_metadata_from_file( Parameters ---------- file : `str` - Name of file to read. Can be FITS or YAML. YAML must be a simple - top-level dict. + Name of file to read. Can be FITS, YAML or JSON. YAML or JSON must be + a simple top-level dict. hdrnum : `int` Header number to read. Only relevant for FITS. If greater than 1 it will be merged with the primary header. If a negative number is @@ -149,7 +149,7 @@ def read_basic_metadata_from_file( The header as a dict. Can be `None` if there was a problem reading the file. """ - if file.endswith(".yaml"): + if file.endswith(".yaml") or file.endswith(".json"): try: md = read_test_file( file, diff --git a/python/astro_metadata_translator/tests.py b/python/astro_metadata_translator/tests.py index 55fd630f..ea8eb77c 100644 --- a/python/astro_metadata_translator/tests.py +++ b/python/astro_metadata_translator/tests.py @@ -13,6 +13,7 @@ __all__ = ("read_test_file", "MetadataAssertHelper") +import json import os import pickle import warnings @@ -83,7 +84,13 @@ def read_test_file(filename: str, dir: str | None = None) -> MutableMapping[str, if dir is not None and not os.path.isabs(filename): filename = os.path.join(dir, filename) with open(filename) as fd: - header = yaml.load(fd, Loader=Loader) + if filename.endswith(".yaml"): + header = yaml.load(fd, Loader=Loader) + elif filename.endswith(".json"): + header = json.load(fd) + else: + raise RuntimeError(f"Unrecognized file format: {filename}") + # Cannot directly check for Mapping because PropertyList is not one if not hasattr(header, "items"): raise ValueError(f"Contents of YAML file {filename} are not a mapping, they are {type(header)}") From 78e3a55c1ef80836e55929fa4d52fdde6612f0b1 Mon Sep 17 00:00:00 2001 From: Tim Jenness Date: Mon, 23 Oct 2023 12:39:48 -0700 Subject: [PATCH 2/2] Remove type ignore that is no longer needed --- python/astro_metadata_translator/headers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/astro_metadata_translator/headers.py b/python/astro_metadata_translator/headers.py index c5c75c69..423840b5 100644 --- a/python/astro_metadata_translator/headers.py +++ b/python/astro_metadata_translator/headers.py @@ -351,7 +351,7 @@ def _find_from_resource( Name of resource read. `None` if no corrections found. """ if package is not None and resource_root is not None: - resource_path = resources.files(package).joinpath(resource_root, target_file) # type: ignore + resource_path = resources.files(package).joinpath(resource_root, target_file) if resource_path.is_file(): resource_uri = f"resource://{package}/{posixpath.join(resource_root, target_file)}" log.debug("Applying header corrections from package resource %s", resource_uri)