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

DM-39731: Use DATE-BEG #69

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions python/astro_metadata_translator/bin/translateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import logging
import sys
import traceback
from typing import IO, List, Sequence, Tuple
from collections.abc import Sequence
from typing import IO

import yaml

Expand Down Expand Up @@ -275,7 +276,7 @@ def process_files(
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
output_mode: str = "auto",
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Read and translate metadata from the specified files.

Parameters
Expand Down
9 changes: 5 additions & 4 deletions python/astro_metadata_translator/bin/writeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import logging
import os
import sys
from typing import IO, List, MutableMapping, Optional, Sequence, Tuple
from collections.abc import MutableMapping, Sequence
from typing import IO

Check warning on line 21 in python/astro_metadata_translator/bin/writeindex.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/bin/writeindex.py#L20-L21

Added lines #L20 - L21 were not covered by tests

from ..file_helpers import find_files
from ..indexing import index_files
Expand All @@ -31,10 +32,10 @@
hdrnum: int,
print_trace: bool,
content_mode: str = "translated",
outpath: Optional[str] = None,
outpath: str | None = None,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Process each file and create JSON index file.

The index file will have common information in the toplevel.
Expand Down Expand Up @@ -90,7 +91,7 @@

failed = []
okay = []
files_per_directory: MutableMapping[str, List[str]] = {}
files_per_directory: MutableMapping[str, list[str]] = {}

Check warning on line 94 in python/astro_metadata_translator/bin/writeindex.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/bin/writeindex.py#L94

Added line #L94 was not covered by tests

# Group each file by directory if no explicit output path
if outpath is None:
Expand Down
7 changes: 4 additions & 3 deletions python/astro_metadata_translator/bin/writesidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import os
import sys
import traceback
from typing import IO, List, Sequence, Tuple
from collections.abc import Sequence
from typing import IO

Check warning on line 20 in python/astro_metadata_translator/bin/writesidecar.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/bin/writesidecar.py#L19-L20

Added lines #L19 - L20 were not covered by tests

from ..file_helpers import find_files, read_file_info


def _split_ext(file: str) -> Tuple[str, str]:
def _split_ext(file: str) -> tuple[str, str]:

Check warning on line 25 in python/astro_metadata_translator/bin/writesidecar.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/bin/writesidecar.py#L25

Added line #L25 was not covered by tests
"""Split the extension from the file name and return it and the root.

Special case handling of .gz and other compression extensions.
Expand Down Expand Up @@ -120,7 +121,7 @@
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Process each file and create sidecar file.

Parameters
Expand Down
2 changes: 1 addition & 1 deletion python/astro_metadata_translator/cli/astrometadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import importlib
import logging
import os
from typing import Sequence
from collections.abc import Sequence

Check warning on line 19 in python/astro_metadata_translator/cli/astrometadata.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/cli/astrometadata.py#L19

Added line #L19 was not covered by tests

import click

Expand Down
19 changes: 8 additions & 11 deletions python/astro_metadata_translator/file_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import re
import sys
import traceback
from typing import IO, Any, Iterable, List, MutableMapping, Optional, Union
from collections.abc import Iterable, MutableMapping
from typing import IO, Any

from .headers import merge_headers
from .observationInfo import ObservationInfo
Expand All @@ -31,9 +32,7 @@
import lsst.daf.base # noqa: F401 need PropertyBase for readMetadata
from lsst.afw.fits import FitsError, readMetadata

def _read_fits_metadata(
file: str, hdu: int, can_raise: bool = False
) -> Optional[MutableMapping[str, Any]]:
def _read_fits_metadata(file: str, hdu: int, can_raise: bool = False) -> MutableMapping[str, Any] | None:

Check warning on line 35 in python/astro_metadata_translator/file_helpers.py

View check run for this annotation

Codecov / codecov/patch

python/astro_metadata_translator/file_helpers.py#L35

Added line #L35 was not covered by tests
"""Read a FITS header using afw.

Parameters
Expand Down Expand Up @@ -70,9 +69,7 @@
except ImportError:
from astropy.io import fits

def _read_fits_metadata(
file: str, hdu: int, can_raise: bool = False
) -> Optional[MutableMapping[str, Any]]:
def _read_fits_metadata(file: str, hdu: int, can_raise: bool = False) -> MutableMapping[str, Any] | None:
"""Read a FITS header using astropy."""

# For detailed docstrings see the afw implementation above
Expand All @@ -90,7 +87,7 @@
return header


def find_files(files: Iterable[str], regex: str) -> List[str]:
def find_files(files: Iterable[str], regex: str) -> list[str]:
"""Find files for processing.

Parameters
Expand Down Expand Up @@ -125,7 +122,7 @@

def read_basic_metadata_from_file(
file: str, hdrnum: int, errstream: IO = sys.stderr, can_raise: bool = True
) -> Optional[MutableMapping[str, Any]]:
) -> MutableMapping[str, Any] | None:
"""Read a raw header from a file, merging if necessary

Parameters
Expand Down Expand Up @@ -190,12 +187,12 @@
def read_file_info(
file: str,
hdrnum: int,
print_trace: Optional[bool] = None,
print_trace: bool | None = None,
content_mode: str = "translated",
content_type: str = "simple",
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Optional[Union[str, MutableMapping[str, Any], ObservationInfo]]:
) -> str | MutableMapping[str, Any] | ObservationInfo | None:
"""Read information from file

Parameters
Expand Down
30 changes: 14 additions & 16 deletions python/astro_metadata_translator/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import os
import posixpath
from collections import Counter
from collections.abc import Mapping
from typing import IO, Any, List, MutableMapping, Optional, Sequence, Tuple, Type, Union
from collections.abc import Mapping, MutableMapping, Sequence
from typing import IO, Any

import pkg_resources
import yaml
Expand All @@ -47,8 +47,8 @@ def merge_headers(
headers: Sequence[MutableMapping[str, Any]],
mode: str = "overwrite",
sort: bool = False,
first: Optional[Sequence[str]] = None,
last: Optional[Sequence[str]] = None,
first: Sequence[str] | None = None,
last: Sequence[str] | None = None,
) -> MutableMapping[str, Any]:
"""Merge multiple headers into a single dict.

Expand Down Expand Up @@ -245,8 +245,8 @@ def key_func(hdr: Mapping[str, Any]) -> Any:

def retain_value(
to_receive: MutableMapping[str, Any],
to_retain: Optional[Sequence[str]],
sources: Tuple[Mapping[str, Any], ...],
to_retain: Sequence[str] | None,
sources: tuple[Mapping[str, Any], ...],
) -> None:
if to_retain:
for k in to_retain:
Expand All @@ -263,7 +263,7 @@ def retain_value(
return merged


def _read_yaml(fh: IO[bytes], msg: str) -> Optional[Mapping[str, Any]]:
def _read_yaml(fh: IO[bytes], msg: str) -> Mapping[str, Any] | None:
"""Read YAML from file descriptor.

Parameters
Expand Down Expand Up @@ -294,9 +294,7 @@ def _read_yaml(fh: IO[bytes], msg: str) -> Optional[Mapping[str, Any]]:
return content


def _find_from_file(
header: MutableMapping[str, Any], paths: Sequence[str], target_file: str
) -> Optional[str]:
def _find_from_file(header: MutableMapping[str, Any], paths: Sequence[str], target_file: str) -> str | None:
"""Search file system for matching correction files.

Parameters
Expand Down Expand Up @@ -332,8 +330,8 @@ def _find_from_file(


def _find_from_resource(
header: MutableMapping[str, Any], package: Optional[str], resource_root: Optional[str], target_file: str
) -> Optional[str]:
header: MutableMapping[str, Any], package: str | None, resource_root: str | None, target_file: str
) -> str | None:
"""Search package resource for correction information.

Parameters
Expand Down Expand Up @@ -370,9 +368,9 @@ def _find_from_resource(

def fix_header(
header: MutableMapping[str, Any],
search_path: Optional[Union[str, Sequence[str]]] = None,
translator_class: Optional[Type[MetadataTranslator]] = None,
filename: Optional[str] = None,
search_path: str | Sequence[str] | None = None,
translator_class: type[MetadataTranslator] | None = None,
filename: str | None = None,
) -> bool:
"""Update, in place, the supplied header with known corrections.

Expand Down Expand Up @@ -468,7 +466,7 @@ class or else support automatic translation class determination.
log.debug("Checking for header correction file named %s", target_file)

# Work out the search path
paths: List[str] = []
paths: list[str] = []
if search_path is not None:
if isinstance(search_path, str):
# Allow a single path to be given as a string
Expand Down