Skip to content

Commit

Permalink
Cache generated fallback enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
priitlatt committed Aug 30, 2024
1 parent b00caae commit 9745977
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/codemagic/models/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations

import contextlib
import enum
import re
from typing import Any
from typing import Dict
from typing import Tuple

from codemagic.utilities import log

Expand All @@ -19,6 +24,8 @@ class ResourceEnumMeta(enum.EnumMeta):
graceful_fallback = True
enable_name_transformation = False

__graceful_fallback_cache: Dict[Tuple[str, Any], Any] = {}

def __call__(cls, value, *args, **kwargs): # noqa: N805
try:
return super().__call__(value, *args, **kwargs)
Expand All @@ -29,11 +36,21 @@ def __call__(cls, value, *args, **kwargs): # noqa: N805
logger = log.get_logger(cls, log_to_stream=False)
logger.warning("Undefined Resource enumeration: %s", ve)
try:
enum_class = ResourceEnum(f"Graceful{cls.__name__}", {value: value})
return enum_class(value)
return cls._create_graceful_fallback_instance(value)
except TypeError:
raise ve

def _create_graceful_fallback_instance(cls, value): # noqa: N805
cache_key = (cls.__name__, value)

try:
enum_class = cls.__graceful_fallback_cache[cache_key]
except KeyError:
enum_class = ResourceEnum(f"Graceful{cls.__name__}", {value: value})
cls.__graceful_fallback_cache[cache_key] = enum_class

return enum_class(value)

def _transform_class_name(cls): # noqa: N805
"""
If enabled, transform CamelCase class name 'ClassName' to more
Expand Down

0 comments on commit 9745977

Please sign in to comment.