scratchattach 2.1.9__py3-none-any.whl → 2.1.10a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. scratchattach/__init__.py +28 -25
  2. scratchattach/cloud/__init__.py +2 -0
  3. scratchattach/cloud/_base.py +454 -282
  4. scratchattach/cloud/cloud.py +171 -168
  5. scratchattach/editor/__init__.py +21 -0
  6. scratchattach/editor/asset.py +199 -0
  7. scratchattach/editor/backpack_json.py +117 -0
  8. scratchattach/editor/base.py +142 -0
  9. scratchattach/editor/block.py +507 -0
  10. scratchattach/editor/blockshape.py +353 -0
  11. scratchattach/editor/build_defaulting.py +47 -0
  12. scratchattach/editor/comment.py +74 -0
  13. scratchattach/editor/commons.py +243 -0
  14. scratchattach/editor/extension.py +43 -0
  15. scratchattach/editor/field.py +90 -0
  16. scratchattach/editor/inputs.py +132 -0
  17. scratchattach/editor/meta.py +106 -0
  18. scratchattach/editor/monitor.py +175 -0
  19. scratchattach/editor/mutation.py +317 -0
  20. scratchattach/editor/pallete.py +91 -0
  21. scratchattach/editor/prim.py +170 -0
  22. scratchattach/editor/project.py +273 -0
  23. scratchattach/editor/sbuild.py +2837 -0
  24. scratchattach/editor/sprite.py +586 -0
  25. scratchattach/editor/twconfig.py +113 -0
  26. scratchattach/editor/vlb.py +134 -0
  27. scratchattach/eventhandlers/_base.py +99 -92
  28. scratchattach/eventhandlers/cloud_events.py +110 -103
  29. scratchattach/eventhandlers/cloud_recorder.py +26 -21
  30. scratchattach/eventhandlers/cloud_requests.py +460 -452
  31. scratchattach/eventhandlers/cloud_server.py +246 -244
  32. scratchattach/eventhandlers/cloud_storage.py +135 -134
  33. scratchattach/eventhandlers/combine.py +29 -27
  34. scratchattach/eventhandlers/filterbot.py +160 -159
  35. scratchattach/eventhandlers/message_events.py +41 -40
  36. scratchattach/other/other_apis.py +284 -212
  37. scratchattach/other/project_json_capabilities.py +475 -546
  38. scratchattach/site/_base.py +64 -46
  39. scratchattach/site/activity.py +414 -122
  40. scratchattach/site/backpack_asset.py +118 -84
  41. scratchattach/site/classroom.py +430 -142
  42. scratchattach/site/cloud_activity.py +107 -103
  43. scratchattach/site/comment.py +220 -190
  44. scratchattach/site/forum.py +400 -399
  45. scratchattach/site/project.py +806 -787
  46. scratchattach/site/session.py +1134 -867
  47. scratchattach/site/studio.py +611 -609
  48. scratchattach/site/user.py +835 -837
  49. scratchattach/utils/commons.py +243 -148
  50. scratchattach/utils/encoder.py +157 -156
  51. scratchattach/utils/enums.py +197 -190
  52. scratchattach/utils/exceptions.py +233 -206
  53. scratchattach/utils/requests.py +67 -59
  54. {scratchattach-2.1.9.dist-info → scratchattach-2.1.10a1.dist-info}/METADATA +155 -146
  55. scratchattach-2.1.10a1.dist-info/RECORD +62 -0
  56. {scratchattach-2.1.9.dist-info → scratchattach-2.1.10a1.dist-info}/WHEEL +1 -1
  57. {scratchattach-2.1.9.dist-info → scratchattach-2.1.10a1.dist-info/licenses}/LICENSE +21 -21
  58. scratchattach-2.1.9.dist-info/RECORD +0 -40
  59. {scratchattach-2.1.9.dist-info → scratchattach-2.1.10a1.dist-info}/top_level.txt +0 -0
@@ -1,84 +1,118 @@
1
- import time
2
- from ._base import BaseSiteComponent
3
- from ..utils.requests import Requests as requests
4
- from ..utils import exceptions
5
-
6
- class BackpackAsset(BaseSiteComponent):
7
- """
8
- Represents an asset from the backpack.
9
-
10
- Attributes:
11
-
12
- :.id:
13
-
14
- :.type: The asset type (costume, script etc.)
15
-
16
- :.mime: The format in which the content of the backpack asset is saved
17
-
18
- :.name: The name of the backpack asset
19
-
20
- :.filename: Filename of the file containing the content of the backpack asset
21
-
22
- :.thumbnail_url: Link that leads to the asset's thumbnail (the image shown in the backpack UI)
23
-
24
- :.download_url: Link that leads to a file containing the content of the backpack asset
25
- """
26
-
27
- def __init__(self, **entries):
28
- # Set attributes every BackpackAsset object needs to have:
29
- self._session = None
30
-
31
- # Update attributes from entries dict:
32
- self.__dict__.update(entries)
33
-
34
- def update(self):
35
- print("Warning: BackpackAsset objects can't be updated")
36
- return False # Objects of this type cannot be updated
37
-
38
- def _update_from_dict(self, data) -> bool:
39
- try: self.id = data["id"]
40
- except Exception: pass
41
- try: self.type = data["type"]
42
- except Exception: pass
43
- try: self.mime = data["mime"]
44
- except Exception: pass
45
- try: self.name = data["name"]
46
- except Exception: pass
47
- try: self.filename = data["body"]
48
- except Exception: pass
49
- try: self.thumbnail_url = "https://backpack.scratch.mit.edu/"+data["thumbnail"]
50
- except Exception: pass
51
- try: self.download_url = "https://backpack.scratch.mit.edu/"+data["body"]
52
- except Exception: pass
53
- return True
54
-
55
- def download(self, *, dir=""):
56
- """
57
- Downloads the asset content to the given directory. The given filename is equal to the value saved in the .filename attribute.
58
-
59
- Args:
60
- dir (str): The path of the directory the file will be saved in.
61
- """
62
- if not (dir.endswith("/") or dir.endswith("\\")):
63
- dir = dir+"/"
64
- try:
65
- response = requests.get(
66
- self.download_url,
67
- timeout=10,
68
- )
69
- open(f"{dir}{self.filename}", "wb").write(response.content)
70
- except Exception as e:
71
- raise (
72
- exceptions.FetchError(
73
- "Failed to download asset: "+str(e)
74
- )
75
- )
76
-
77
- def delete(self):
78
- self._assert_auth()
79
-
80
- return requests.delete(
81
- f"https://backpack.scratch.mit.edu/{self._session.username}/{self.id}",
82
- headers = self._session._headers,
83
- timeout = 10,
84
- ).json()
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import time
5
+ import logging
6
+
7
+ from ._base import BaseSiteComponent
8
+ from ..utils import exceptions
9
+ from ..utils.requests import Requests as requests
10
+
11
+
12
+
13
+ class BackpackAsset(BaseSiteComponent):
14
+ """
15
+ Represents an asset from the backpack.
16
+
17
+ Attributes:
18
+
19
+ :.id:
20
+
21
+ :.type: The asset type (costume, script etc.)
22
+
23
+ :.mime: The format in which the content of the backpack asset is saved
24
+
25
+ :.name: The name of the backpack asset
26
+
27
+ :.filename: Filename of the file containing the content of the backpack asset
28
+
29
+ :.thumbnail_url: Link that leads to the asset's thumbnail (the image shown in the backpack UI)
30
+
31
+ :.download_url: Link that leads to a file containing the content of the backpack asset
32
+ """
33
+
34
+ def __init__(self, **entries):
35
+ # Set attributes every BackpackAsset object needs to have:
36
+ self._session = None
37
+
38
+ # Update attributes from entries dict:
39
+ self.__dict__.update(entries)
40
+
41
+ def update(self):
42
+ print("Warning: BackpackAsset objects can't be updated")
43
+ return False # Objects of this type cannot be updated
44
+
45
+
46
+ def _update_from_dict(self, data) -> bool:
47
+ try:
48
+ self.id = data["id"]
49
+ except Exception:
50
+ pass
51
+ try:
52
+ self.type = data["type"]
53
+ except Exception:
54
+ pass
55
+ try:
56
+ self.mime = data["mime"]
57
+ except Exception:
58
+ pass
59
+ try:
60
+ self.name = data["name"]
61
+ except Exception:
62
+ pass
63
+ try:
64
+ self.filename = data["body"]
65
+ except Exception:
66
+ pass
67
+ try:
68
+ self.thumbnail_url = "https://backpack.scratch.mit.edu/" + data["thumbnail"]
69
+ except Exception:
70
+ pass
71
+ try:
72
+ self.download_url = "https://backpack.scratch.mit.edu/" + data["body"]
73
+ except Exception:
74
+ pass
75
+ return True
76
+
77
+ @property
78
+ def _data_bytes(self) -> bytes:
79
+ try:
80
+ return requests.get(self.download_url).content
81
+ except Exception as e:
82
+ raise exceptions.FetchError(f"Failed to download asset: {e}")
83
+
84
+ @property
85
+ def file_ext(self):
86
+ return self.filename.split(".")[-1]
87
+
88
+ @property
89
+ def is_json(self):
90
+ return self.file_ext == "json"
91
+
92
+ @property
93
+ def data(self) -> dict | list | int | None | str | bytes | float:
94
+ if self.is_json:
95
+ return json.loads(self._data_bytes)
96
+ else:
97
+ # It's either a zip
98
+ return self._data_bytes
99
+
100
+ def download(self, *, fp: str = ''):
101
+ """
102
+ Downloads the asset content to the given directory. The given filename is equal to the value saved in the .filename attribute.
103
+
104
+ Args:
105
+ fp (str): The path of the directory the file will be saved in.
106
+ """
107
+ if not (fp.endswith("/") or fp.endswith("\\")):
108
+ fp = fp + "/"
109
+ open(f"{fp}{self.filename}", "wb").write(self._data_bytes)
110
+
111
+ def delete(self):
112
+ self._assert_auth()
113
+
114
+ return requests.delete(
115
+ f"https://backpack.scratch.mit.edu/{self._session.username}/{self.id}",
116
+ headers=self._session._headers,
117
+ timeout=10,
118
+ ).json()