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

Move find_fits_keyword to fits_support #396

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/stdatamodels/fits_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,38 @@
if isinstance(s, bytes):
s = s.decode("ascii")
return s


def find_fits_keyword(schema, keyword, return_result=False):
"""
Utility function to find a reference to a FITS keyword in a given
schema. This is intended for interactive use, and not for use
within library code.

Parameters
----------
schema : JSON schema fragment
The schema in which to search.

keyword : str
A FITS keyword name

Returns
-------
locations : list of str

Notes
-----
return_result is included for backward compatibility and is not used.
"""

def find_fits_keyword(subschema, path, combiner, ctx, recurse):
if len(path) and path[0] == "extra_fits":
return True

Check warning on line 959 in src/stdatamodels/fits_support.py

View check run for this annotation

Codecov / codecov/patch

src/stdatamodels/fits_support.py#L959

Added line #L959 was not covered by tests
if subschema.get("fits_keyword") == keyword:
results.append(".".join(path))

results = []
mschema.walk_schema(schema, find_fits_keyword, results)

return results
4 changes: 2 additions & 2 deletions src/stdatamodels/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,9 @@ def find_fits_keyword(self, keyword, return_result=True):
the schema where this FITS keyword is used. Each element
is a dot-separated path.
"""
from . import schema
from . import fits_support

return schema.find_fits_keyword(self.schema, keyword)
return fits_support.find_fits_keyword(self.schema, keyword)

def search_schema(self, substring):
"""
Expand Down
32 changes: 0 additions & 32 deletions src/stdatamodels/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,6 @@
import re


# return_result included for backward compatibility
def find_fits_keyword(schema, keyword, return_result=False):
"""
Utility function to find a reference to a FITS keyword in a given
schema. This is intended for interactive use, and not for use
within library code.

Parameters
----------
schema : JSON schema fragment
The schema in which to search.

keyword : str
A FITS keyword name

Returns
-------
locations : list of str
"""

def find_fits_keyword(subschema, path, combiner, ctx, recurse):
if len(path) and path[0] == "extra_fits":
return True
if subschema.get("fits_keyword") == keyword:
results.append(".".join(path))

results = []
walk_schema(schema, find_fits_keyword, results)

return results


class SearchSchemaResults(list):
def __repr__(self):
import textwrap
Expand Down
5 changes: 5 additions & 0 deletions tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,8 @@ def test_fitsrec_for_non_schema_data(tmp_path):
)
fn = tmp_path / "test.fits"
m.save(fn)


def test_find_fits_keyword():
with FitsModel() as x:
assert x.find_fits_keyword("TELESCOP") == ["meta.telescope"]
5 changes: 0 additions & 5 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ def test_ad_hoc_attributes(filename, tmp_path):
assert dm2.meta.foo == {"a": 42, "b": ["a", "b", "c"]}


def test_find_fits_keyword():
with FitsModel() as x:
assert x.find_fits_keyword("TELESCOP") == ["meta.telescope"]


def test_search_schema():
with BasicModel() as x:
results = x.search_schema("origin")
Expand Down
Loading