File: generic_type.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (64 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Any, TypeVar

from moto.core.common_models import BaseModel
from moto.core.utils import camelcase_to_underscores


class GenericType(BaseModel):
    def __init__(self, name: str, version: str, **kwargs: Any):
        self.name = name
        self.version = version
        self.status = "REGISTERED"
        if "description" in kwargs:
            self.description = kwargs.pop("description")
        for key, value in kwargs.items():
            self.__setattr__(key, value)
        # default values set to none
        for key in self._configuration_keys:
            attr = camelcase_to_underscores(key)
            if not hasattr(self, attr):
                self.__setattr__(attr, None)
        if not hasattr(self, "task_list"):
            self.task_list = None

    def __repr__(self) -> str:
        cls = self.__class__.__name__
        attrs = f"name: {self.name}, version: {self.version}, status: {self.status}"
        return f"{cls}({attrs})"

    @property
    def kind(self) -> str:
        raise NotImplementedError()

    @property
    def _configuration_keys(self) -> list[str]:
        raise NotImplementedError()

    def to_short_dict(self) -> dict[str, str]:
        return {"name": self.name, "version": self.version}

    def to_medium_dict(self) -> dict[str, Any]:
        hsh: dict[str, Any] = {
            f"{self.kind}Type": self.to_short_dict(),
            "creationDate": 1420066800,
            "status": self.status,
        }
        if self.status == "DEPRECATED":
            hsh["deprecationDate"] = 1422745200
        if hasattr(self, "description"):
            hsh["description"] = self.description
        return hsh

    def to_full_dict(self) -> dict[str, Any]:
        hsh: dict[str, Any] = {"typeInfo": self.to_medium_dict(), "configuration": {}}
        if self.task_list:
            hsh["configuration"]["defaultTaskList"] = {"name": self.task_list}
        for key in self._configuration_keys:
            attr = camelcase_to_underscores(key)
            if not getattr(self, attr):
                continue
            hsh["configuration"][key] = getattr(self, attr)
        return hsh


TGenericType = TypeVar("TGenericType", bound=GenericType)