-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add store api to manipulate properties
- Loading branch information
Showing
11 changed files
with
147 additions
and
0 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
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
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,67 @@ | ||
""" | ||
A store that allows to manipulate and swap individual material properties with custom | ||
implementations. | ||
:meth:`register_pluggable` is a decorator to use above all functions defining material | ||
properties. | ||
Users can then replace the original implementation by their own implementations using | ||
``replace_implementation(new_impl: Callable)``, and also switch back to the original | ||
implementation using ``reset_implementation()`` on the decorated function object. The | ||
original implementation is always available as ``original_impl()``. | ||
Apart from the decorator, this store only provides a single user-facing function: a | ||
global way to reset all implementations to their initial state using | ||
:meth:`reset_all_to_original`. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import wraps | ||
from types import MethodType | ||
from typing import Callable | ||
|
||
_optical_property_store = [] | ||
|
||
|
||
def register_pluggable(fn: Callable) -> Callable: | ||
"""Decorator that registers this function as a pluggable property function.""" | ||
|
||
# create the new wrapper object. | ||
@wraps(fn) | ||
def wrap(*args, **kwargs): | ||
return wrap._impl(*args, **kwargs) | ||
|
||
wrap._impl = fn | ||
wrap._orig_impl = fn | ||
|
||
# add "instance methods" of the new wrapper. | ||
def reset_implementation(self) -> None: | ||
"""Reset to the original function implementation.""" | ||
self._impl = self._orig_impl | ||
|
||
def replace_implementation(self, new_impl: Callable) -> None: | ||
"""Replace the underlying function implementation.""" | ||
self._impl = new_impl | ||
|
||
def is_original(self) -> bool: | ||
return self._impl == self._orig_impl | ||
|
||
def original_impl(self) -> Callable: | ||
return self._orig_impl | ||
|
||
wrap.reset_implementation = MethodType(reset_implementation, wrap) | ||
wrap.replace_implementation = MethodType(replace_implementation, wrap) | ||
wrap.is_original = MethodType(is_original, wrap) | ||
wrap.original_impl = MethodType(original_impl, wrap) | ||
|
||
# store the wrapper to use it later. | ||
_optical_property_store.append(wrap) | ||
|
||
return wrap | ||
|
||
|
||
def reset_all_to_original() -> None: | ||
"""Reset all pluggable material property functions to their original implementations.""" | ||
for entry in _optical_property_store: | ||
entry.reset_implementation() |
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
Oops, something went wrong.