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

Simplify unknown-extension error message #584

Merged

Conversation

maddenp-noaa
Copy link
Collaborator

Synopsis

When a file with an unrecognized extension is specified, uwtools provides information on the underlying KeyError that is not useful to users. This PR simplifies the information presented to the user.

Old behavior:

>>> template.render(input_file="template.jinja2", values_src="values.txt")
Cannot deduce format of 'values.txt' from unknown extension 'txt'
Traceback (most recent call last):
  File "/home/maddenp/git/uwtools/src/uwtools/utils/file.py", line 54, in get_file_format
    return FORMAT.formats()[suffix]
           ~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'txt'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/maddenp/git/uwtools/src/uwtools/api/template.py", line 50, in render
    result = _render(
             ^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/config/jinja2.py", line 176, in render
    values = _supplement_values(
             ^^^^^^^^^^^^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/config/jinja2.py", line 349, in _supplement_values
    values_format = values_format or get_file_format(values_src)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/utils/file.py", line 58, in get_file_format
    raise UWError(msg) from e
uwtools.exceptions.UWError: Cannot deduce format of 'values.txt' from unknown extension 'txt'

New behavior:

>>> template.render(input_file="template.jinja2", values_src="values.txt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/maddenp/git/uwtools/src/uwtools/api/template.py", line 50, in render
    result = _render(
             ^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/config/jinja2.py", line 176, in render
    values = _supplement_values(
             ^^^^^^^^^^^^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/config/jinja2.py", line 349, in _supplement_values
    values_format = values_format or get_file_format(values_src)
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/maddenp/git/uwtools/src/uwtools/utils/file.py", line 56, in get_file_format
    raise UWError(msg) from None
uwtools.exceptions.UWError: Cannot deduce format of 'values.txt' from unknown extension 'txt'

CLI behavior remains:

$ echo "{{ x }}" | uw template render --values-file values.txt
Cannot deduce format of 'values.txt' from unknown extension 'txt'

Type

  • Code maintenance (refactoring, etc. without behavior change)

Impact

  • This is a non-breaking change (existing functionality continues to work as expected)

Checklist

  • I have added myself and any co-authors to the PR's Assignees list.
  • I have reviewed the documentation and have made any updates necessitated by this change.

@maddenp-noaa maddenp-noaa merged commit 448318f into ufs-community:main Aug 20, 2024
2 checks passed
@maddenp-noaa maddenp-noaa deleted the simplify-unknown-extension-error branch August 20, 2024 20:21
Byrnetp added a commit that referenced this pull request Sep 3, 2024
* update_values() -> update_from() (#578)

* DRY out Driver._validate() (#579)

* Move execute() to its own API namespace, disambiguate (#580)

* Config classes to API (#582)

* Simplify unknown-extension error message (#584)

* UW-657 fs makedirs (#572)

* Docstring cleanup (#585)

* GH 586 bad stdin_ok prompt (#587)

* Fix issue with creating a metatask dep. (#589)

The rocoto schema is set up to allow metataskdep entries in the YAML, but the logic was not included in the tool to handle them. This addition fixes that.

* Add --show-schema support to drivers (#588)

* add file system notebook, unit tests, binder links

* Add missing newlines

* Add a table of contents

* Makefile and .gitignore changes from review

* Apply suggestions from code review

Co-authored-by: Paul Madden <136389411+maddenp-noaa@users.noreply.github.com>

* Apply review suggestions to all sections of the notebook

* Specific rmtree() targeting and output cell test update

* Apply suggestions from code review

Co-authored-by: Paul Madden <136389411+maddenp-noaa@users.noreply.github.com>

---------

Co-authored-by: Paul Madden <136389411+maddenp-noaa@users.noreply.github.com>
Co-authored-by: Christina Holt <56881914+christinaholtNOAA@users.noreply.github.com>
Byrnetp added a commit that referenced this pull request Oct 17, 2024
* update_values() -> update_from() (#578)

* DRY out Driver._validate() (#579)

* Move execute() to its own API namespace, disambiguate (#580)

* Config classes to API (#582)

* Simplify unknown-extension error message (#584)

* UW-657 fs makedirs (#572)

* Docstring cleanup (#585)

* GH 586 bad stdin_ok prompt (#587)

* Fix issue with creating a metatask dep. (#589)

The rocoto schema is set up to allow metataskdep entries in the YAML, but the logic was not included in the tool to handle them. This addition fixes that.

* Add --show-schema support to drivers (#588)

* Config notebook p2 and example notebook removal

* removing merge diff text

* pin python version for RtD

* improvements from code review

* add config.compare() format mismatch example

* Remove typo

Co-authored-by: Emily Carpenter <137525341+elcarpenterNOAA@users.noreply.github.com>

---------

Co-authored-by: Paul Madden <136389411+maddenp-noaa@users.noreply.github.com>
Co-authored-by: Christina Holt <56881914+christinaholtNOAA@users.noreply.github.com>
Co-authored-by: Emily Carpenter <137525341+elcarpenterNOAA@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants