Skip to content

Commit

Permalink
Allow translator based header fix up to go wrong
Browse files Browse the repository at this point in the history
It's possible that some headers are so broken that the
code trying to fix them up can't work out something.
A date is a good example since an error parsing a date
in a header will cause many date-based corrections to
fail.

Log the problem and hope that a file-based correction
is put in place that allows the translator code to
function properly.
  • Loading branch information
timj committed Dec 12, 2019
1 parent 2264edb commit c7565e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion python/astro_metadata_translator/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ class or else support automatic translation class determination.
modified = _find_from_resource(header, *translator.resource_root(), target_file)

# Allow a translation class to do local fixups
translator_modified = translator_class.fix_header(header)
# Allow it to fail but log the failure
try:
translator_modified = translator_class.fix_header(header)
except Exception as e:
log.fatal("Ignoring translator header fixup of %s %s: %s",
instrument, obsid, str(e))
translator_modified = False

return modified | translator_modified
16 changes: 16 additions & 0 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def fix_header(cls, header):
return True


class AlsoNotDecamTranslator(DecamTranslator):
"""This is a DECam translator with override list of header corrections
that fails."""
name = None

@classmethod
def fix_header(cls, header):
raise RuntimeError("Failure to work something out from header")


class HeadersTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -360,6 +370,12 @@ def test_translator_fix_header(self):
self.assertTrue(fixed)
self.assertEqual(header["DTSITE"], "hi")

header["DTSITE"] = "reset"
with self.assertLogs("astro_metadata_translator", level="FATAL"):
fixed = fix_header(header, translator_class=AlsoNotDecamTranslator)
self.assertFalse(fixed)
self.assertEqual(header["DTSITE"], "reset")


if __name__ == "__main__":
unittest.main()

0 comments on commit c7565e5

Please sign in to comment.