Skip to content

Commit

Permalink
Incorret theme should not prevent napari from start (napari#5605)
Browse files Browse the repository at this point in the history
# Description
<!-- What does this pull request (PR) do? Why is it necessary? -->
<!-- Tell us about your new feature, improvement, or fix! -->
<!-- If your change includes user interface changes, please add an
image, or an animation "An image is worth a thousand words!" -->
<!-- You can use https://www.cockos.com/licecap/ or similar to create
animations -->

For the current implementation, the error in theme registration prevents
the napari form from starting. It may be problematic for bundle users.

In this PR I add `try: ... except` to handle an error during theme
registration and convert it to logging exceptions. I use logging because
it happened before creating GUI.

## Type of change
<!-- Please delete options that are not relevant. -->
- [x] Bug-fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

# References
<!-- What resources, documentation, and guides were used in the creation
of this PR? -->
<!-- If this is a bug-fix or otherwise resolves an issue, reference it
here with "closes #(issue)" -->

# How has this been tested?
<!-- Please describe the tests that you ran to verify your changes. -->
- [ ] example: the test suite for my feature covers cases x, y, and z
- [ ] example: all tests pass with my change
- [ ] example: I check if my changes works with both PySide and PyQt
backends
      as there are small differences between the two Qt bindings.  

Install `napari-gruvbox`, `pygments==2.6` (bellow 2.9) and start napari 

Example error message:
```python-traceback
11:52:01 ERROR Registration theme failed.
1 validation error for Theme
syntax_style
  Incorrect `syntax_style` value: gruvbox-dark provided. Please use one of the following:  default, emacs, friendly, colorful, autumn, murphy, manni, monokai, perldoc, pastie, borland, trac, native, fruity, bw, vim, vs, tango, rrt, xcode, igor, paraiso-light, paraiso-dark, lovelace, algol, algol_nu, arduino, rainbow_dash, abap, solarized-dark, solarized-light, sas, stata, stata-light, stata-dark, inkpot (type=assertion_error)
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/napari/utils/theme.py", line 391, in _install_npe2_themes
    register_theme(theme.id, theme_dict, manifest.name)
  File "/home/czaki/Projekty/napari/napari/utils/theme.py", line 266, in register_theme
    theme = Theme(**theme)
  File "/home/czaki/Projekty/napari/napari/utils/events/evented_model.py", line 200, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Theme
syntax_style
  Incorrect `syntax_style` value: gruvbox-dark provided. Please use one of the following:  default, emacs, friendly, colorful, autumn, murphy, manni, monokai, perldoc, pastie, borland, trac, native, fruity, bw, vim, vs, tango, rrt, xcode, igor, paraiso-light, paraiso-dark, lovelace, algol, algol_nu, arduino, rainbow_dash, abap, solarized-dark, solarized-light, sas, stata, stata-light, stata-dark, inkpot (type=assertion_error)
11:52:01 ERROR Registration theme failed.
1 validation error for Theme
syntax_style
  Incorrect `syntax_style` value: gruvbox-light provided. Please use one of the following:  default, emacs, friendly, colorful, autumn, murphy, manni, monokai, perldoc, pastie, borland, trac, native, fruity, bw, vim, vs, tango, rrt, xcode, igor, paraiso-light, paraiso-dark, lovelace, algol, algol_nu, arduino, rainbow_dash, abap, solarized-dark, solarized-light, sas, stata, stata-light, stata-dark, inkpot (type=assertion_error)
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/napari/utils/theme.py", line 391, in _install_npe2_themes
    register_theme(theme.id, theme_dict, manifest.name)
  File "/home/czaki/Projekty/napari/napari/utils/theme.py", line 266, in register_theme
    theme = Theme(**theme)
  File "/home/czaki/Projekty/napari/napari/utils/events/evented_model.py", line 200, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Theme
syntax_style
  Incorrect `syntax_style` value: gruvbox-light provided. Please use one of the following:  default, emacs, friendly, colorful, autumn, murphy, manni, monokai, perldoc, pastie, borland, trac, native, fruity, bw, vim, vs, tango, rrt, xcode, igor, paraiso-light, paraiso-dark, lovelace, algol, algol_nu, arduino, rainbow_dash, abap, solarized-dark, solarized-light, sas, stata, stata-light, stata-dark, inkpot (type=assertion_error)
```

## Final checklist:
- [ ] My PR is the minimum possible work for the desired functionality
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] If I included new strings, I have used `trans.` to make them
localizable.
For more information see our [translations
guide](https://napari.org/developers/translations.html).

---------

Co-authored-by: Lorenzo Gaifas <brisvag@gmail.com>
  • Loading branch information
Czaki and brisvag authored Mar 8, 2023
1 parent ff1e56e commit 3ec4be1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
49 changes: 49 additions & 0 deletions napari/utils/_tests/test_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
import sys

import pytest
from npe2 import PluginManager, PluginManifest
from npe2 import __version__ as npe2_version
from npe2.manifest.schema import ContributionPoints
from packaging.version import parse as parse_version
from pydantic import ValidationError

from napari.resources._icons import PLUGIN_FILE_NAME
from napari.settings import get_settings
from napari.utils.theme import (
Theme,
_install_npe2_themes,
available_themes,
get_theme,
is_theme_available,
Expand Down Expand Up @@ -160,3 +165,47 @@ def mock_install_theme(_themes):
assert is_theme_available("test_blue")
assert len(available_themes()) == n_themes + 1
assert "test_blue" in available_themes()


@pytest.mark.skipif(
parse_version(npe2_version) < parse_version("0.6.2"),
reason="requires npe2 0.6.2 for syntax style contributions",
)
def test_theme_registration(monkeypatch, caplog):
data = {"dark": get_theme("dark", as_dict=False)}

manifest = PluginManifest(
name="theme_test",
display_name="Theme Test",
contributions=ContributionPoints(
themes=[
{
"id": "theme1",
"label": "Theme 1",
"type": "dark",
"syntax_style": "native",
"colors": {},
},
{
"id": "theme2",
"label": "Theme 2",
"type": "dark",
"syntax_style": "does_not_exist",
"colors": {},
},
]
),
)

def mock_iter_manifests(disabled):
return [manifest]

monkeypatch.setattr(
PluginManager.instance(), "iter_manifests", mock_iter_manifests
)
monkeypatch.setattr("napari.utils.theme._themes", data)
_install_npe2_themes(data)

assert "theme1" in data
assert "theme2" not in data
assert "Registration theme failed" in caplog.text
6 changes: 5 additions & 1 deletion napari/utils/theme.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# syntax_style for the console must be one of the supported styles from
# pygments - see here for examples https://help.farbox.com/pygments.html
import logging
import re
import warnings
from ast import literal_eval
Expand Down Expand Up @@ -386,7 +387,10 @@ def _install_npe2_themes(themes=None):
theme_colors = theme.colors.dict(exclude_unset=True)
theme_dict.update(theme_info)
theme_dict.update(theme_colors)
register_theme(theme.id, theme_dict, manifest.name)
try:
register_theme(theme.id, theme_dict, manifest.name)
except ValueError as e:
logging.exception("Registration theme failed.\n%s", e)


_install_npe2_themes(_themes)
Expand Down

0 comments on commit 3ec4be1

Please sign in to comment.