From 1e7c9db988434bb2b27e7a73b1a4af102497b576 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Thu, 26 Jun 2025 09:25:20 +0200 Subject: [PATCH] define context change reason enum --- client/ayon_core/host/__init__.py | 3 +++ client/ayon_core/host/constants.py | 15 +++++++++++++++ client/ayon_core/host/host.py | 8 +++++--- client/ayon_core/host/interfaces/workfiles.py | 9 +++------ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 client/ayon_core/host/constants.py diff --git a/client/ayon_core/host/__init__.py b/client/ayon_core/host/__init__.py index b252b03d76..ef5c324028 100644 --- a/client/ayon_core/host/__init__.py +++ b/client/ayon_core/host/__init__.py @@ -1,3 +1,4 @@ +from .constants import ContextChangeReason from .host import ( HostBase, ) @@ -15,6 +16,8 @@ from .dirmap import HostDirmap __all__ = ( + "ContextChangeReason", + "HostBase", "IWorkfileHost", diff --git a/client/ayon_core/host/constants.py b/client/ayon_core/host/constants.py new file mode 100644 index 0000000000..2564c5d54d --- /dev/null +++ b/client/ayon_core/host/constants.py @@ -0,0 +1,15 @@ +from enum import Enum + + +class StrEnum(str, Enum): + """A string-based Enum class that allows for string comparison.""" + + def __str__(self) -> str: + return self.value + + +class ContextChangeReason(StrEnum): + """Reasons for context change in the host.""" + undefined = "undefined" + workfile_open = "workfile.opened" + workfile_save = "workfile.saved" diff --git a/client/ayon_core/host/host.py b/client/ayon_core/host/host.py index d562fcbe65..554b694240 100644 --- a/client/ayon_core/host/host.py +++ b/client/ayon_core/host/host.py @@ -12,6 +12,8 @@ import ayon_api from ayon_core.lib import emit_event +from .constants import ContextChangeReason + if typing.TYPE_CHECKING: from ayon_core.pipeline import Anatomy @@ -28,7 +30,7 @@ class ContextChangeData: project_entity: dict[str, Any] folder_entity: dict[str, Any] task_entity: dict[str, Any] - reason: Optional[str] + reason: ContextChangeReason anatomy: Anatomy @@ -172,7 +174,7 @@ class HostBase(ABC): folder_entity: dict[str, Any], task_entity: dict[str, Any], *, - reason: Optional[str] = None, + reason: ContextChangeReason = ContextChangeReason.undefined, project_entity: Optional[dict[str, Any]] = None, anatomy: Optional[Anatomy] = None, ) -> "HostContextData": @@ -190,7 +192,7 @@ class HostBase(ABC): Args: folder_entity (Optional[dict[str, Any]]): Folder entity. task_entity (Optional[dict[str, Any]]): Task entity. - reason (Optional[str]): Reason for context change. + reason (ContextChangeReason): Reason for context change. project_entity (Optional[dict[str, Any]]): Project entity data. anatomy (Optional[Anatomy]): Anatomy instance for the project. diff --git a/client/ayon_core/host/interfaces/workfiles.py b/client/ayon_core/host/interfaces/workfiles.py index 6b11c2fce6..559660a6e9 100644 --- a/client/ayon_core/host/interfaces/workfiles.py +++ b/client/ayon_core/host/interfaces/workfiles.py @@ -14,15 +14,12 @@ import ayon_api import arrow from ayon_core.lib import emit_event +from ayon_core.host.constants import ContextChangeReason if typing.TYPE_CHECKING: from ayon_core.pipeline import Anatomy -WORKFILE_OPEN_REASON = "workfile.opened" -WORKFILE_SAVE_REASON = "workfile.saved" - - def deprecated(reason): def decorator(func): message = f"Call to deprecated function {func.__name__} ({reason})." @@ -388,9 +385,9 @@ class IWorkfileHost: self.set_current_context( folder_entity, task_entity, - reason=WORKFILE_SAVE_REASON, project_entity=project_entity, anatomy=anatomy, + reason=ContextChangeReason.workfile_save, ) self.save_workfile(filepath) @@ -458,9 +455,9 @@ class IWorkfileHost: self.set_current_context( folder_entity, task_entity, - reason=WORKFILE_OPEN_REASON, project_entity=project_entity, anatomy=anatomy, + reason=ContextChangeReason.workfile_open, ) self.open_workfile(filepath)