Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit de085f0

Browse files
committed
feat: add costume/sound names
1 parent cdb11f3 commit de085f0

3 files changed

Lines changed: 875 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ authors = [{ name = "faretek", email = "r@faretek.dev" }]
77
requires-python = ">=3.12"
88
dependencies = [
99
"gitpython>=3.1.46",
10+
"python-slugify>=8.0.4",
11+
"scratchattach==3.0.0b2",
1012
"tomlkit>=0.14.0",
1113
]
1214

src/sb2git/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
import tomlkit
44
import dataclasses
55
import zipfile
6+
import json
67
import typing as t
8+
import scratchattach.editor # this is slow but easier
79
from pathlib import Path
10+
from slugify import slugify
811
from datetime import datetime
912

1013

1114
@dataclasses.dataclass
1215
class Asset:
1316
md5: str = ""
14-
# names: list[str] = dataclasses.field(default_factory=list)
17+
names: set[str] = dataclasses.field(default_factory=set)
1518
ext: str = ""
1619
content_written: bool = False
1720
# content: bytes = dataclasses.field(default=b"", repr=False)
@@ -100,13 +103,25 @@ def sortfunc(f: Path):
100103
assets: dict[str, Asset] = {}
101104
for file in files:
102105
with zipfile.ZipFile(file) as archive:
106+
if file.name.endswith(".sb3"):
107+
body = scratchattach.editor.Project.from_json(
108+
json.loads(archive.read("project.json"))
109+
) # using from json so assets aren't loaded and no extra zipfiling
110+
else:
111+
assert file.name.endswith(".sprite3")
112+
body = scratchattach.editor.Sprite.from_json(
113+
json.loads(archive.read("sprite.json"))
114+
)
103115
for file in archive.filelist:
104116
if file.filename.endswith(".json"):
105117
continue
106118
asset = assets.get(file.filename, Asset())
107119
asset.ext = file.filename.split(".")[-1]
108120
asset.md5 = file.filename.removesuffix(f".{asset.ext}")
109-
# asset.names.append() # TODO: read sprite.json or project.json
121+
122+
for aobj in body.assets:
123+
if aobj.file_name == file.filename:
124+
asset.names.add(aobj.name)
110125

111126
if not asset.content_written:
112127
(assetpath / file.filename).write_bytes(archive.read(file.filename))
@@ -120,6 +135,10 @@ def sortfunc(f: Path):
120135
print(asset)
121136
table.add("md5", asset.md5)
122137
table.add("ext", asset.ext)
138+
table.add("names", list(asset.names))
139+
table.add(
140+
"chosen_name", slugify(next(iter(asset.names))) if asset.names else "foo"
141+
)
123142

124143
asset_arr.append(table)
125144
# store file toml
@@ -135,7 +154,7 @@ def sortfunc(f: Path):
135154
file_arr.append(table)
136155

137156
content = {"files": file_arr, "assets": asset_arr}
138-
with outpath.open("w") as f:
157+
with outpath.open("w", encoding="utf-8") as f:
139158
tomlkit.dump(content, f)
140159

141160

0 commit comments

Comments
 (0)