diff --git a/src/yt_napari/_ds_cache.py b/src/yt_napari/_ds_cache.py index 882850f..51ef771 100644 --- a/src/yt_napari/_ds_cache.py +++ b/src/yt_napari/_ds_cache.py @@ -1,3 +1,7 @@ +import os.path +from os import PathLike +from typing import Optional + import yt from yt_napari import _special_loaders @@ -42,11 +46,11 @@ def check_then_load(self, filename: str, cache_if_not_found: bool = True): if self.exists(filename): ytnapari_log.info(f"loading {filename} from cache.") return self.get_ds(filename) - elif filename.startswith("_ytnapari") and hasattr(_special_loaders, filename): + elif callable_name := _check_for_special(filename): # the filename is actually a function handle! get it, call it - # this allows yt-napari to to use all the yt fake datasets in + # this allows yt-napari to use all the yt fake datasets in # testing without saving them to disk. - ds_callable = getattr(_special_loaders, filename) + ds_callable = getattr(_special_loaders, callable_name) ds = ds_callable() else: ds = yt.load(filename) @@ -57,3 +61,12 @@ def check_then_load(self, filename: str, cache_if_not_found: bool = True): dataset_cache = DatasetCache() + + +def _check_for_special(filename: PathLike) -> Optional[str]: + # check if a "filename" is one of our short-circuiting special loaders + # and return the function name if it is valid. + basename = os.path.basename(filename) + if basename.startswith("_ytnapari") and hasattr(_special_loaders, basename): + return str(basename) + return None diff --git a/src/yt_napari/_tests/test_metadata_widget.py b/src/yt_napari/_tests/test_metadata_widget.py new file mode 100644 index 0000000..a1e1cdd --- /dev/null +++ b/src/yt_napari/_tests/test_metadata_widget.py @@ -0,0 +1,25 @@ +from yt_napari._widget_matadata import LayersList, MetadataWidget + + +def test_widget_reader(make_napari_viewer): + viewer = make_napari_viewer() + r = MetadataWidget(napari_viewer=viewer) + r.metadata_input_container.filename.value = "_ytnapari_load_grid" + r.inspect_file() + r.inspect_file() # do it again to hit that clear statement + + assert "stream" in r.field_lists.keys() + + r.field_lists["stream"].expand() + r.field_lists["stream"].expand() + + assert "domain_left_edge" in r.array_vals.keys() + r.array_vals["domain_left_edge"].update_units("km") + r.deleteLater() + + +def test_layer_list(): + ll = LayersList("test_layer", range(0, 10), expand=False) + ll.expand() + assert ll.currently_expanded is True + ll.deleteLater() diff --git a/src/yt_napari/_widget_matadata.py b/src/yt_napari/_widget_matadata.py index 1f78f4b..622a88e 100644 --- a/src/yt_napari/_widget_matadata.py +++ b/src/yt_napari/_widget_matadata.py @@ -46,8 +46,10 @@ def inspect_file(self): self.layout().removeWidget(list_widget) list_widget.setParent(None) self.widgets_to_clear = [] - + self.field_lists = {} + self.array_vals = {} py_kwargs = {} + _gui_utilities.translator.get_pydantic_kwargs( self.metadata_input_container, _data_model.MetadataModel, py_kwargs ) @@ -62,6 +64,7 @@ def inspect_file(self): for attr, val in meta_data_dict.items(): if isinstance(val, unyt_array): newid = UnytArrayQWidget(attr, val) + self.array_vals[attr] = newid else: newid = QLabel(f"{attr}: {str(val)}") self.widgets_to_clear.append(newid) @@ -72,6 +75,7 @@ def inspect_file(self): for ftype, fields in fields_by_type.items(): new_field_list = LayersList(ftype, fields, ilist < 3) ilist += 1 + self.field_lists[ftype] = new_field_list self.widgets_to_clear.append(new_field_list) self.layout().addWidget(new_field_list) @@ -80,14 +84,6 @@ def inspect_file(self): # https://stackoverflow.com/questions/11077793/is-there-a-standard-component-for-collapsible-panel-in-qt -class CustomQStandardItem(QStandardItem): - def __init__(self, icon, text): - super().__init__(icon, text) - - def dropMimeData(self, data, action, row, column, parent): - pass - - class LayersList(QWidget): """ LayerList class which acts as collapsable list. @@ -113,7 +109,7 @@ def __init__(self, name, layers, expand=True): self.main_layout.addWidget(self.layer_list) self.expand_button.clicked.connect(self.expand) self.setLayout(self.main_layout) - self.resized_size = 16.5 * len(layers) + self.resized_size = int(16 * len(layers)) if not expand: self.expand()