File: models.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 (75 lines) | stat: -rw-r--r-- 2,420 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
65
66
67
68
69
70
71
72
73
74
75
import hashlib
from collections import OrderedDict
from typing import Any

from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel

from .exceptions import ClientError


class Object(BaseModel):
    def __init__(
        self, path: str, body: str, etag: str, storage_class: str = "TEMPORAL"
    ):
        self.path = path
        self.body = body
        self.content_sha256 = hashlib.sha256(body.encode("utf-8")).hexdigest()
        self.etag = etag
        self.storage_class = storage_class

    def to_dict(self) -> dict[str, Any]:
        return {
            "ETag": self.etag,
            "Name": self.path,
            "Type": "FILE",
            "ContentLength": 123,
            "StorageClass": self.storage_class,
            "Path": self.path,
            "ContentSHA256": self.content_sha256,
        }


class MediaStoreDataBackend(BaseBackend):
    def __init__(self, region_name: str, account_id: str):
        super().__init__(region_name, account_id)
        self._objects: dict[str, Object] = OrderedDict()

    def put_object(
        self, body: str, path: str, storage_class: str = "TEMPORAL"
    ) -> Object:
        """
        The following parameters are not yet implemented: ContentType, CacheControl, UploadAvailability
        """
        new_object = Object(
            path=path, body=body, etag="etag", storage_class=storage_class
        )
        self._objects[path] = new_object
        return new_object

    def delete_object(self, path: str) -> None:
        if path not in self._objects:
            raise ClientError(
                "ObjectNotFoundException", f"Object with id={path} not found"
            )
        del self._objects[path]

    def get_object(self, path: str) -> Object:
        """
        The Range-parameter is not yet supported.
        """
        objects_found = [item for item in self._objects.values() if item.path == path]
        if len(objects_found) == 0:
            raise ClientError(
                "ObjectNotFoundException", f"Object with id={path} not found"
            )
        return objects_found[0]

    def list_items(self) -> list[dict[str, Any]]:
        """
        The Path- and MaxResults-parameters are not yet supported.
        """
        return [c.to_dict() for c in self._objects.values()]


mediastoredata_backends = BackendDict(MediaStoreDataBackend, "mediastore-data")