moved exceptions to single file

This commit is contained in:
Jakub Trllo 2024-08-12 12:08:09 +02:00
parent 3b53093d12
commit fe8b57f1d3
4 changed files with 140 additions and 128 deletions

View file

@ -4,6 +4,20 @@ from .constants import (
PRE_CREATE_THUMBNAIL_KEY,
DEFAULT_VARIANT_VALUE,
)
from .exceptions import (
UnavailableSharedData,
ImmutableKeyError,
HostMissRequiredMethod,
ConvertorsOperationFailed,
ConvertorsFindFailed,
ConvertorsConversionFailed,
CreatorError,
CreatorsCreateFailed,
CreatorsCollectionFailed,
CreatorsSaveFailed,
CreatorsRemoveFailed,
CreatorsOperationFailed,
)
from .utils import (
get_last_versions_for_instances,
@ -17,8 +31,6 @@ from .product_name import (
)
from .creator_plugins import (
CreatorError,
BaseCreator,
Creator,
AutoCreator,

View file

@ -29,12 +29,23 @@ from ayon_core.pipeline import (
)
from ayon_core.pipeline.plugin_discover import DiscoverResult
from .exceptions import (
CreatorError,
ImmutableKeyError,
CreatorsCreateFailed,
CreatorsCollectionFailed,
CreatorsSaveFailed,
CreatorsRemoveFailed,
ConvertorsFindFailed,
ConvertorsConversionFailed,
UnavailableSharedData,
HostMissRequiredMethod,
)
from .creator_plugins import (
Creator,
AutoCreator,
discover_creator_plugins,
discover_convertor_plugins,
CreatorError,
)
# Changes of instances and context are send as tuple of 2 information
@ -42,68 +53,6 @@ UpdateData = collections.namedtuple("UpdateData", ["instance", "changes"])
_NOT_SET = object()
class UnavailableSharedData(Exception):
"""Shared data are not available at the moment when are accessed."""
pass
class ImmutableKeyError(TypeError):
"""Accessed key is immutable so does not allow changes or removals."""
def __init__(self, key, msg=None):
self.immutable_key = key
if not msg:
msg = "Key \"{}\" is immutable and does not allow changes.".format(
key
)
super(ImmutableKeyError, self).__init__(msg)
class HostMissRequiredMethod(Exception):
"""Host does not have implemented required functions for creation."""
def __init__(self, host, missing_methods):
self.missing_methods = missing_methods
self.host = host
joined_methods = ", ".join(
['"{}"'.format(name) for name in missing_methods]
)
dirpath = os.path.dirname(
os.path.normpath(inspect.getsourcefile(host))
)
dirpath_parts = dirpath.split(os.path.sep)
host_name = dirpath_parts.pop(-1)
if host_name == "api":
host_name = dirpath_parts.pop(-1)
msg = "Host \"{}\" does not have implemented method/s {}".format(
host_name, joined_methods
)
super(HostMissRequiredMethod, self).__init__(msg)
class ConvertorsOperationFailed(Exception):
def __init__(self, msg, failed_info):
super(ConvertorsOperationFailed, self).__init__(msg)
self.failed_info = failed_info
class ConvertorsFindFailed(ConvertorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to find incompatible products"
super(ConvertorsFindFailed, self).__init__(
msg, failed_info
)
class ConvertorsConversionFailed(ConvertorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to convert incompatible products"
super(ConvertorsConversionFailed, self).__init__(
msg, failed_info
)
def prepare_failed_convertor_operation_info(identifier, exc_info):
exc_type, exc_value, exc_traceback = exc_info
formatted_traceback = "".join(traceback.format_exception(
@ -117,59 +66,6 @@ def prepare_failed_convertor_operation_info(identifier, exc_info):
}
class CreatorsOperationFailed(Exception):
"""Raised when a creator process crashes in 'CreateContext'.
The exception contains information about the creator and error. The data
are prepared using 'prepare_failed_creator_operation_info' and can be
serialized using json.
Usage is for UI purposes which may not have access to exceptions directly
and would not have ability to catch exceptions 'per creator'.
Args:
msg (str): General error message.
failed_info (list[dict[str, Any]]): List of failed creators with
exception message and optionally formatted traceback.
"""
def __init__(self, msg, failed_info):
super(CreatorsOperationFailed, self).__init__(msg)
self.failed_info = failed_info
class CreatorsCollectionFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to collect instances"
super(CreatorsCollectionFailed, self).__init__(
msg, failed_info
)
class CreatorsSaveFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed update instance changes"
super(CreatorsSaveFailed, self).__init__(
msg, failed_info
)
class CreatorsRemoveFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to remove instances"
super(CreatorsRemoveFailed, self).__init__(
msg, failed_info
)
class CreatorsCreateFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to create instances"
super(CreatorsCreateFailed, self).__init__(
msg, failed_info
)
def prepare_failed_creator_operation_info(
identifier, label, exc_info, add_traceback=True
):

View file

@ -26,16 +26,6 @@ if TYPE_CHECKING:
from .context import CreateContext, CreatedInstance, UpdateData # noqa: F401
class CreatorError(Exception):
"""Should be raised when creator failed because of known issue.
Message of error should be user readable.
"""
def __init__(self, message):
super(CreatorError, self).__init__(message)
class ProductConvertorPlugin(ABC):
"""Helper for conversion of instances created using legacy creators.

View file

@ -0,0 +1,114 @@
import os
import inspect
class UnavailableSharedData(Exception):
"""Shared data are not available at the moment when are accessed."""
pass
class ImmutableKeyError(TypeError):
"""Accessed key is immutable so does not allow changes or removals."""
def __init__(self, key, msg=None):
self.immutable_key = key
if not msg:
msg = "Key \"{}\" is immutable and does not allow changes.".format(
key
)
super().__init__(msg)
class HostMissRequiredMethod(Exception):
"""Host does not have implemented required functions for creation."""
def __init__(self, host, missing_methods):
self.missing_methods = missing_methods
self.host = host
joined_methods = ", ".join(
['"{}"'.format(name) for name in missing_methods]
)
dirpath = os.path.dirname(
os.path.normpath(inspect.getsourcefile(host))
)
dirpath_parts = dirpath.split(os.path.sep)
host_name = dirpath_parts.pop(-1)
if host_name == "api":
host_name = dirpath_parts.pop(-1)
msg = "Host \"{}\" does not have implemented method/s {}".format(
host_name, joined_methods
)
super().__init__(msg)
class ConvertorsOperationFailed(Exception):
def __init__(self, msg, failed_info):
super().__init__(msg)
self.failed_info = failed_info
class ConvertorsFindFailed(ConvertorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to find incompatible products"
super().__init__(msg, failed_info)
class ConvertorsConversionFailed(ConvertorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to convert incompatible products"
super().__init__(msg, failed_info)
class CreatorError(Exception):
"""Should be raised when creator failed because of known issue.
Message of error should be artist friendly.
"""
pass
class CreatorsOperationFailed(Exception):
"""Raised when a creator process crashes in 'CreateContext'.
The exception contains information about the creator and error. The data
are prepared using 'prepare_failed_creator_operation_info' and can be
serialized using json.
Usage is for UI purposes which may not have access to exceptions directly
and would not have ability to catch exceptions 'per creator'.
Args:
msg (str): General error message.
failed_info (list[dict[str, Any]]): List of failed creators with
exception message and optionally formatted traceback.
"""
def __init__(self, msg, failed_info):
super().__init__(msg)
self.failed_info = failed_info
class CreatorsCollectionFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to collect instances"
super().__init__(msg, failed_info)
class CreatorsSaveFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed update instance changes"
super().__init__(msg, failed_info)
class CreatorsRemoveFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to remove instances"
super().__init__(msg, failed_info)
class CreatorsCreateFailed(CreatorsOperationFailed):
def __init__(self, failed_info):
msg = "Failed to create instances"
super().__init__(msg, failed_info)