| 438 | pb.from_json(project_json) |
| 439 | return pb |
| 440 | def _load_sb3_file(path_to_file): |
| 441 | try: |
| 442 | with open(path_to_file, "r") as r: |
| 443 | return json.loads(r.read()) |
| 444 | except Exception as e: |
| 445 | with zipfile.ZipFile(path_to_file, 'r') as zip_ref: |
| 446 | # Check if the file exists in the zip |
| 447 | if "project.json" in zip_ref.namelist(): |
| 448 | # Read the file as bytes |
| 449 | with zip_ref.open("project.json") as file: |
| 450 | return json.loads(file.read()) |
| 451 | else: |
| 452 | raise ValueError("specified sb3 archive doesn't contain project.json") |
| 453 | def read_sb3_file(path_to_file): |
| 454 | pb = ProjectBody() |
| 455 | pb.from_json(_load_sb3_file(path_to_file)) |