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-43103: Add ObservationInfo.can_see_sky property #73

Merged
merged 14 commits into from
Mar 28, 2024
Merged
11 changes: 6 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jobs:
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
# Need to clone everything to determine version from git.
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
Expand Down Expand Up @@ -54,9 +54,10 @@ jobs:
run: pytest -r a -v -n 3 --cov=astro_metadata_translator --cov=tests --cov-report=xml --cov-report=term --cov-branch

- name: Upload coverage to codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}

- name: Install documenteer
run: uv pip install -r doc/requirements.txt
Expand All @@ -81,13 +82,13 @@ jobs:
if: startsWith(github.ref, 'refs/tags/')

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
# Need to clone everything to embed the version.
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: "pip"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docstyle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
numpydoc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5

- name: Install numpydoc
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
20 changes: 15 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ version = { attr = "lsst_versions.get_lsst_version" }

[tool.black]
line-length = 110
target-version = ["py38"]
target-version = ["py311"]

[tool.isort]
profile = "black"
Expand All @@ -79,11 +79,23 @@ convention = "numpy"
# D104 - we do not require documentation in __init__.py files.
add-ignore = ["D107", "D105", "D102", "D104", "D100", "D200", "D205", "D400"]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"raise AssertionError",
"raise NotImplementedError",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

[tool.ruff]
target-version = "py311"
line-length = 110
exclude = [
"__init__.py",
]

[tool.ruff.lint]
ignore = [
"N999", # Invalid module name
"D107", # Document __init__ at class level.
Expand All @@ -92,23 +104,21 @@ ignore = [
"D100", # Modules are not required to include documentation.
"D205", # Does not understand if a summary is two lines long.
]
line-length = 110
select = [
"E", # pycodestyle
"F", # pyflakes
"N", # pep8-naming
"W", # pycodestyle
"D", # pydocstyle
]
target-version = "py310"
extend-select = [
"RUF100", # Warn about unused noqa
]

[tool.ruff.pycodestyle]
[tool.ruff.lint.pycodestyle]
max-doc-length = 79

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.numpydoc_validation]
Expand Down
30 changes: 13 additions & 17 deletions python/astro_metadata_translator/bin/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

__all__ = ("translate_or_dump_headers",)

import sys
import logging
import traceback
from collections.abc import Sequence
from typing import IO
Expand All @@ -29,6 +29,8 @@

from ..file_helpers import find_files, read_basic_metadata_from_file

log = logging.getLogger(__name__)

# Output mode choices
OUTPUT_MODES = ("auto", "verbose", "table", "yaml", "fixed", "yamlnative", "fixednative", "none")

Expand Down Expand Up @@ -63,8 +65,7 @@ def read_file(
file: str,
hdrnum: int,
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
outstream: IO | None = None,
output_mode: str = "verbose",
write_heading: bool = False,
) -> bool:
Expand All @@ -82,10 +83,8 @@ def read_file(
a full traceback of the exception will be reported. If `False` prints
a one line summary of the error condition.
outstream : `io.StringIO`, optional
Output stream to use for standard messages. Defaults to `sys.stdout`.
errstream : `io.StringIO`, optional
Stream to send messages that would normally be sent to standard
error. Defaults to `sys.stderr`.
Output stream to use for standard messages. Defaults to `None` which
uses the default output stream.
output_mode : `str`, optional
Output mode to use. Must be one of "verbose", "none", "table",
"yaml", or "fixed". "yaml" and "fixed" can be modified with a
Expand All @@ -111,10 +110,10 @@ def read_file(

# This gets in the way in tabular mode
if output_mode != "table":
print(f"Analyzing {file}...", file=errstream)
log.info("Analyzing %s...", file)

try:
md = read_basic_metadata_from_file(file, hdrnum, errstream=errstream, can_raise=True)
md = read_basic_metadata_from_file(file, hdrnum, can_raise=True)
if md is None:
raise RuntimeError(f"Failed to read file {file} HDU={hdrnum}")

Expand Down Expand Up @@ -190,8 +189,7 @@ def translate_or_dump_headers(
regex: str,
hdrnum: int,
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
outstream: IO | None = None,
output_mode: str = "auto",
) -> tuple[list[str], list[str]]:
"""Read and translate metadata from the specified files.
Expand All @@ -210,11 +208,9 @@ def translate_or_dump_headers(
If there is an error reading the file and this parameter is `True`,
a full traceback of the exception will be reported. If `False` prints
a one line summary of the error condition.
outstream : `io.StringIO`, optional
Output stream to use for standard messages. Defaults to `sys.stdout`.
errstream : `io.StringIO`, optional
Stream to send messages that would normally be sent to standard
error. Defaults to `sys.stderr`.
outstream : `io.StringIO` or `None`, optional
Output stream to use for standard messages. Defaults to `None` which
uses the default output stream.
output_mode : `str`, optional
Output mode to use for the translated information.
"auto" switches based on how many files are found.
Expand All @@ -239,7 +235,7 @@ def translate_or_dump_headers(
okay = []
heading = True
for path in sorted(found_files):
isok = read_file(path, hdrnum, print_trace, outstream, errstream, output_mode, heading)
isok = read_file(path, hdrnum, print_trace, outstream, output_mode, heading)
heading = False
if isok:
okay.append(path)
Expand Down
19 changes: 10 additions & 9 deletions python/astro_metadata_translator/bin/writeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

from __future__ import annotations

__all__ = "write_index_files"
__all__ = ["write_index_files"]

import json
import logging
import os
import sys
from collections.abc import MutableMapping, Sequence
from typing import IO

Expand All @@ -33,8 +32,7 @@ def write_index_files(
print_trace: bool,
content_mode: str = "translated",
outpath: str | None = None,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
outstream: IO | None = None,
) -> tuple[list[str], list[str]]:
"""Process each file and create JSON index file.

Expand Down Expand Up @@ -67,10 +65,8 @@ def write_index_files(
and index file will be written to each directory in which files
are found.
outstream : `io.StringIO`, optional
Output stream to use for standard messages. Defaults to `sys.stdout`.
errstream : `io.StringIO`, optional
Stream to send messages that would normally be sent to standard
error. Defaults to `sys.stderr`.
Output stream to use for standard messages. Defaults to `None` which
uses the default output stream. Defaults to `sys.stdout`.

Returns
-------
Expand Down Expand Up @@ -104,7 +100,12 @@ def write_index_files(
# Extract translated metadata for each file in each directory
for directory, files_in_dir in files_per_directory.items():
output, this_okay, this_failed = index_files(
files_in_dir, directory, hdrnum, print_trace, content_mode, outstream, errstream
files_in_dir,
directory,
hdrnum,
print_trace,
content_mode,
outstream,
)

failed.extend(this_failed)
Expand Down
26 changes: 11 additions & 15 deletions python/astro_metadata_translator/bin/writesidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

__all__ = ("write_sidecar_files", "write_sidecar_file")

import logging
import os
import sys
import traceback
from collections.abc import Sequence
from typing import IO

from ..file_helpers import find_files, read_file_info

log = logging.getLogger(__name__)


def _split_ext(file: str) -> tuple[str, str]:
"""Split the extension from the file name and return it and the root.
Expand Down Expand Up @@ -54,8 +56,7 @@ def write_sidecar_file(
hdrnum: int,
content_mode: str,
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
outstream: IO | None = None,
) -> bool:
"""Write JSON summary to sidecar file.

Expand All @@ -76,10 +77,8 @@ def write_sidecar_file(
a one line summary of the error condition. If `None` the exception
will be allowed.
outstream : `io.StringIO`, optional
Output stream to use for standard messages. Defaults to `sys.stdout`.
errstream : `io.StringIO`, optional
Stream to send messages that would normally be sent to standard
error. Defaults to `sys.stderr`.
Output stream to use for standard messages. Defaults to `None` which
uses the default output stream.

Returns
-------
Expand All @@ -99,7 +98,6 @@ def write_sidecar_file(
content_type="json",
print_trace=print_trace,
outstream=outstream,
errstream=errstream,
)
if json_str is None:
return False
Expand All @@ -111,6 +109,7 @@ def write_sidecar_file(
newfile = root + ".json"
with open(newfile, "w") as fd:
print(json_str, file=fd)
log.debug("Writing sidecar file %s", newfile)

except Exception as e:
if print_trace is None:
Expand All @@ -129,8 +128,7 @@ def write_sidecar_files(
hdrnum: int,
content_mode: str,
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
outstream: IO | None = None,
) -> tuple[list[str], list[str]]:
"""Process each file and create sidecar file.

Expand All @@ -153,10 +151,8 @@ def write_sidecar_files(
a full traceback of the exception will be reported. If `False` prints
a one line summary of the error condition.
outstream : `io.StringIO`, optional
Output stream to use for standard messages. Defaults to `sys.stdout`.
errstream : `io.StringIO`, optional
Stream to send messages that would normally be sent to standard
error. Defaults to `sys.stderr`.
Output stream to use for standard messages. Defaults to `None` which
uses the default output stream.

Returns
-------
Expand All @@ -171,7 +167,7 @@ def write_sidecar_files(
failed = []
okay = []
for path in sorted(found_files):
isok = write_sidecar_file(path, hdrnum, content_mode, print_trace, outstream, errstream)
isok = write_sidecar_file(path, hdrnum, content_mode, print_trace, outstream)
if isok:
okay.append(path)
else:
Expand Down
10 changes: 9 additions & 1 deletion python/astro_metadata_translator/cli/astrometadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
# Default regex for finding data files
re_default = r"\.fit[s]?\b"

log = logging.getLogger("astro_metadata_translator")

PACKAGES_VAR = "METADATA_TRANSLATORS"

hdrnum_option = click.option(
Expand Down Expand Up @@ -95,7 +97,7 @@ def main(ctx: click.Context, log_level: int, traceback: bool, packages: Sequence
try:
importlib.import_module(m)
except (ImportError, ModuleNotFoundError):
logging.warn("Failed to import translator module: %s", m)
log.warning("Failed to import translator module: %s", m)


@main.command(help="Translate metadata in supplied files and report.")
Expand Down Expand Up @@ -182,8 +184,14 @@ def write_sidecar(ctx: click.Context, files: Sequence[str], hdrnum: int, regex:
for f in failed:
click.echo(f"\t{f}", err=True)

if not okay and not failed:
# No files found at all.
click.echo("Found no files matching regex.")
raise click.exceptions.Exit(1)

if not okay:
# Good status if anything was returned in okay
click.echo(f"No files processed successfully. Found {len(failed)}.", err=True)
raise click.exceptions.Exit(1)


Expand Down