|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from hashlib import md5 |
| 5 | +import requests |
| 6 | + |
| 7 | +from . import base, commons, sprite, build_defaulting |
| 8 | + |
| 9 | + |
| 10 | +@dataclass(init=True, repr=True) |
| 11 | +class AssetFile: |
| 12 | + filename: str |
| 13 | + _data: bytes = field(repr=False, default=None) |
| 14 | + _md5: str = field(repr=False, default=None) |
| 15 | + |
| 16 | + @property |
| 17 | + def data(self): |
| 18 | + if self._data is None: |
| 19 | + # Download and cache |
| 20 | + rq = requests.get(f"https://assets.scratch.mit.edu/internalapi/asset/{self.filename}/get/") |
| 21 | + if rq.status_code != 200: |
| 22 | + raise ValueError(f"Can't download asset {self.filename}\nIs not uploaded to scratch! Response: {rq.text}") |
| 23 | + |
| 24 | + self._data = rq.content |
| 25 | + |
| 26 | + return self._data |
| 27 | + |
| 28 | + @property |
| 29 | + def md5(self): |
| 30 | + if self._md5 is None: |
| 31 | + self._md5 = md5(self.data).hexdigest() |
| 32 | + |
| 33 | + return self._md5 |
| 34 | + |
| 35 | + |
| 36 | +class Asset(base.SpriteSubComponent): |
| 37 | + def __init__(self, |
| 38 | + name: str = "costume1", |
| 39 | + file_name: str = "b7853f557e4426412e64bb3da6531a99.svg", |
| 40 | + _sprite: sprite.Sprite = build_defaulting.SPRITE_DEFAULT): |
| 41 | + """ |
| 42 | + Represents a generic asset. Can be a sound or an image. |
| 43 | + https://en.scratch-wiki.info/wiki/Scratch_File_Format#Assets |
| 44 | + """ |
| 45 | + try: |
| 46 | + asset_id, data_format = file_name.split('.') |
| 47 | + except ValueError: |
| 48 | + raise ValueError(f"Invalid file name: {file_name}, # of '.' in {file_name} ({file_name.count('.')}) != 2; " |
| 49 | + f"(too many/few values to unpack)") |
| 50 | + self.name = name |
| 51 | + |
| 52 | + self.id = asset_id |
| 53 | + self.data_format = data_format |
| 54 | + |
| 55 | + super().__init__(_sprite) |
| 56 | + |
| 57 | + def __repr__(self): |
| 58 | + return f"Asset<{self.name!r}>" |
| 59 | + |
| 60 | + @property |
| 61 | + def folder(self): |
| 62 | + return commons.get_folder_name(self.name) |
| 63 | + |
| 64 | + @property |
| 65 | + def name_nfldr(self): |
| 66 | + return commons.get_name_nofldr(self.name) |
| 67 | + |
| 68 | + @property |
| 69 | + def file_name(self): |
| 70 | + return f"{self.id}.{self.data_format}" |
| 71 | + |
| 72 | + @property |
| 73 | + def md5ext(self): |
| 74 | + return self.file_name |
| 75 | + |
| 76 | + @property |
| 77 | + def parent(self): |
| 78 | + if self.project is None: |
| 79 | + return self.sprite |
| 80 | + else: |
| 81 | + return self.project |
| 82 | + |
| 83 | + @property |
| 84 | + def asset_file(self) -> AssetFile: |
| 85 | + for asset_file in self.parent.asset_data: |
| 86 | + if asset_file.filename == self.file_name: |
| 87 | + return asset_file |
| 88 | + |
| 89 | + # No pre-existing asset file object; create one and add it to the project |
| 90 | + asset_file = AssetFile(self.file_name) |
| 91 | + self.project.asset_data.append(asset_file) |
| 92 | + return asset_file |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def from_json(data: dict): |
| 96 | + _name = data.get("name") |
| 97 | + _file_name = data.get("md5ext") |
| 98 | + if _file_name is None: |
| 99 | + if "dataFormat" in data and "assetId" in data: |
| 100 | + _id = data["assetId"] |
| 101 | + _data_format = data["dataFormat"] |
| 102 | + _file_name = f"{_id}.{_data_format}" |
| 103 | + |
| 104 | + return Asset(_name, _file_name) |
| 105 | + |
| 106 | + def to_json(self) -> dict: |
| 107 | + return { |
| 108 | + "name": self.name, |
| 109 | + |
| 110 | + "assetId": self.id, |
| 111 | + "md5ext": self.file_name, |
| 112 | + "dataFormat": self.data_format, |
| 113 | + } |
| 114 | + |
| 115 | + """ |
| 116 | + @staticmethod |
| 117 | + def from_file(fp: str, name: str = None): |
| 118 | + image_types = ("png", "jpg", "jpeg", "svg") |
| 119 | + sound_types = ("wav", "mp3") |
| 120 | + |
| 121 | + # Should save data as well so it can be uploaded to scratch if required (add to project asset data) |
| 122 | + ... |
| 123 | + """ |
| 124 | + |
| 125 | + |
| 126 | +class Costume(Asset): |
| 127 | + def __init__(self, |
| 128 | + name: str = "Cat", |
| 129 | + file_name: str = "b7853f557e4426412e64bb3da6531a99.svg", |
| 130 | + |
| 131 | + bitmap_resolution=None, |
| 132 | + rotation_center_x: int | float = 48, |
| 133 | + rotation_center_y: int | float = 50, |
| 134 | + _sprite: sprite.Sprite = build_defaulting.SPRITE_DEFAULT): |
| 135 | + """ |
| 136 | + A costume. An asset with additional properties |
| 137 | + https://en.scratch-wiki.info/wiki/Scratch_File_Format#Costumes |
| 138 | + """ |
| 139 | + super().__init__(name, file_name, _sprite) |
| 140 | + |
| 141 | + self.bitmap_resolution = bitmap_resolution |
| 142 | + self.rotation_center_x = rotation_center_x |
| 143 | + self.rotation_center_y = rotation_center_y |
| 144 | + |
| 145 | + @staticmethod |
| 146 | + def from_json(data): |
| 147 | + _asset_load = Asset.from_json(data) |
| 148 | + |
| 149 | + bitmap_resolution = data.get("bitmapResolution") |
| 150 | + |
| 151 | + rotation_center_x = data["rotationCenterX"] |
| 152 | + rotation_center_y = data["rotationCenterY"] |
| 153 | + return Costume(_asset_load.name, _asset_load.file_name, |
| 154 | + |
| 155 | + bitmap_resolution, rotation_center_x, rotation_center_y) |
| 156 | + |
| 157 | + def to_json(self) -> dict: |
| 158 | + _json = super().to_json() |
| 159 | + _json.update({ |
| 160 | + "bitmapResolution": self.bitmap_resolution, |
| 161 | + "rotationCenterX": self.rotation_center_x, |
| 162 | + "rotationCenterY": self.rotation_center_y |
| 163 | + }) |
| 164 | + return _json |
| 165 | + |
| 166 | + |
| 167 | +class Sound(Asset): |
| 168 | + def __init__(self, |
| 169 | + name: str = "pop", |
| 170 | + file_name: str = "83a9787d4cb6f3b7632b4ddfebf74367.wav", |
| 171 | + |
| 172 | + rate: int = None, |
| 173 | + sample_count: int = None, |
| 174 | + _sprite: sprite.Sprite = build_defaulting.SPRITE_DEFAULT): |
| 175 | + """ |
| 176 | + A sound. An asset with additional properties |
| 177 | + https://en.scratch-wiki.info/wiki/Scratch_File_Format#Sounds |
| 178 | + """ |
| 179 | + super().__init__(name, file_name, _sprite) |
| 180 | + |
| 181 | + self.rate = rate |
| 182 | + self.sample_count = sample_count |
| 183 | + |
| 184 | + @staticmethod |
| 185 | + def from_json(data): |
| 186 | + _asset_load = Asset.from_json(data) |
| 187 | + |
| 188 | + rate = data.get("rate") |
| 189 | + sample_count = data.get("sampleCount") |
| 190 | + return Sound(_asset_load.name, _asset_load.file_name, rate, sample_count) |
| 191 | + |
| 192 | + def to_json(self) -> dict: |
| 193 | + _json = super().to_json() |
| 194 | + commons.noneless_update(_json, { |
| 195 | + "rate": self.rate, |
| 196 | + "sampleCount": self.sample_count |
| 197 | + }) |
| 198 | + return _json |
0 commit comments