-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make special (global) webviz_settings argument available to plugins (#…
…368)
- Loading branch information
Showing
13 changed files
with
191 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import cast | ||
|
||
import pytest | ||
|
||
from webviz_config import WebvizConfigTheme, WebvizSettings | ||
|
||
|
||
def test_construction_and_basic_access() -> None: | ||
# pylint: disable=unidiomatic-typecheck | ||
the_shared_settings = {"somenumber": 10, "somestring": "abc"} | ||
the_theme = WebvizConfigTheme("dummyThemeName") | ||
settings_obj = WebvizSettings(the_shared_settings, the_theme) | ||
|
||
copy_of_shared_settings = settings_obj.shared_settings | ||
assert copy_of_shared_settings is not the_shared_settings | ||
assert type(copy_of_shared_settings) == type(the_shared_settings) | ||
assert copy_of_shared_settings == the_shared_settings | ||
the_shared_settings["somestring"] = "MODIFIED" | ||
assert copy_of_shared_settings != the_shared_settings | ||
|
||
copy_of_theme = settings_obj.theme | ||
assert copy_of_theme is not the_theme | ||
assert type(copy_of_theme) == type(the_theme) | ||
assert copy_of_theme.__dict__ == the_theme.__dict__ | ||
the_theme.theme_name = "MODIFIED" | ||
assert copy_of_theme.__dict__ != the_theme.__dict__ | ||
|
||
|
||
def test_construction_with_invalid_types() -> None: | ||
with pytest.raises(TypeError): | ||
theme = WebvizConfigTheme("dummyThemeName") | ||
_settings_obj = WebvizSettings(cast(dict, None), theme) | ||
|
||
with pytest.raises(TypeError): | ||
shared_settings = {"somenumber": 10, "somestring": "abc"} | ||
_settings_obj = WebvizSettings(shared_settings, cast(WebvizConfigTheme, None)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import copy | ||
from typing import Dict, Any, Mapping | ||
|
||
from ._theme_class import WebvizConfigTheme | ||
|
||
|
||
class WebvizSettings: | ||
"""This class contains global Webviz settings that will be made available | ||
to all plugins through the special argument named webviz_settings. | ||
""" | ||
|
||
def __init__(self, shared_settings: Dict[str, Any], theme: WebvizConfigTheme): | ||
if not isinstance(shared_settings, dict): | ||
raise TypeError("shared_settings must be of type dict") | ||
|
||
if not isinstance(theme, WebvizConfigTheme): | ||
raise TypeError("theme must be of type WebvizConfigTheme") | ||
|
||
self._shared_settings = shared_settings | ||
self._theme = theme | ||
|
||
@property | ||
def shared_settings(self) -> Mapping[str, Any]: | ||
return copy.deepcopy(self._shared_settings) | ||
|
||
@property | ||
def theme(self) -> WebvizConfigTheme: | ||
return copy.deepcopy(self._theme) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
webviz_config/utils/_deprecate_webviz_settings_attribute_in_dash_app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any | ||
import warnings | ||
|
||
import dash | ||
|
||
|
||
def _get_deprecated_webviz_settings(self: Any) -> dict: | ||
warnings.warn( | ||
"Accessing webviz_settings through the Dash application object has been deprecated, " | ||
"see https://github.com/equinor/webviz-config/pull/368", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
# pylint: disable=protected-access | ||
return self._deprecated_webviz_settings | ||
|
||
|
||
def deprecate_webviz_settings_attribute_in_dash_app() -> None: | ||
"""Helper that monkey patches dash.Dash application class so that access to | ||
the webviz_settings via the Dash application instance attribute is reported | ||
as being deprecated. | ||
""" | ||
dash.Dash.webviz_settings = property( | ||
_get_deprecated_webviz_settings, | ||
None, | ||
None, | ||
"Property to mark webviz_settings access as deprecated", | ||
) |