Skip to content

Commit

Permalink
Merge pull request #172 from damusss/mp3-error
Browse files Browse the repository at this point in the history
Raise an exception with common unsupported audio formats
  • Loading branch information
pmp-p authored Aug 20, 2024
2 parents 761d849 + 03d61ca commit fff5410
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Serving python files from [/data/git/pygbag/test/build/web]
with no security/performance in mind, i'm just a test tool : don't rely on me
usage: __main__.py [-h] [--bind ADDRESS] [--PYBUILD PYBUILD] [--app_name APP_NAME] [--ume_block UME_BLOCK] [--can_close CAN_CLOSE] [--cache CACHE] [--package PACKAGE] [--title TITLE] [--version VERSION] [--build] [--html] [--no_opt] [--archive] [--icon ICON] [--cdn CDN] [--template TEMPLATE] [--ssl SSL]
[--port [PORT]]
[--port [PORT]] [--disable-sound-format-error]
options:
-h, --help show this help message and exit
Expand All @@ -103,6 +103,7 @@ options:
--template TEMPLATE index.html template [default:default.tmpl]
--ssl SSL enable ssl with server.pem and key.pem
--port [PORT] Specify alternate port [default: 8000]
--disable-sound-format-error audio files with a common unsupported format found in the assets won't raise an exception
```

unlisted developper options:
Expand Down
7 changes: 7 additions & 0 deletions src/pygbag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ async def main_run(app_folder, mainscript, cdn=DEFAULT_CDN):
help="Specify alternate port [default: 8000]",
)

parser.add_argument(
"--disable-sound-format-error",
action="store_true",
default=False,
help="audio files with a common unsupported format found in the assets won't raise an exception",
)

args = parser.parse_args()

# when in browser IDE everything should be done in allowed folder
Expand Down
31 changes: 27 additions & 4 deletions src/pygbag/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from .html_embed import html_embed

COUNTER = 0
UNSUPPORTED_CACHE = []
OGG_CACHE = []


class REPLAY:
Expand All @@ -21,15 +23,24 @@ async def pack_files(zf, packlist, zfolders, target_folder):
global COUNTER

for asset in packlist:
asset_name = str(asset)[1:]

if "--disable-sound-format-error" not in sys.argv:
suffix = Path(asset_name).suffix[1:].lower()
if suffix in ["mp3", "wav", "aiff"]:
UNSUPPORTED_CACHE.append((asset_name.replace(f".{suffix}", ""), suffix))
elif suffix == "ogg":
OGG_CACHE.append(asset_name.replace(".ogg", ""))

zpath = list(zfolders)
zpath.insert(0, str(target_folder))
zpath.append(str(asset)[1:])
zpath.append(asset_name)

zip_content = target_folder / str(asset)[1:]
print(f"\t{target_folder} : {str(asset)[1:]}")
zip_content = target_folder / asset_name
print(f"\t{target_folder} : {asset_name}")

zpath = list(zfolders)
zpath.append(str(asset)[1:].replace("-pygbag.", "."))
zpath.append(asset_name.replace("-pygbag.", "."))

if not zip_content.is_file():
print("32: ERROR", zip_content)
Expand Down Expand Up @@ -113,6 +124,18 @@ async def archive(apkname, target_folder, build_dir=None):
# pack_files(zf, Path.cwd(), ["assets"], target_folder)
await pack_files(zf, packlist, ["assets"], target_folder)

for unsupported_name, suffix in UNSUPPORTED_CACHE:
found_ogg = False
for ogg_name in OGG_CACHE:
if ogg_name in [unsupported_name, f"{unsupported_name}-pygbag"]:
found_ogg = True
break
if not found_ogg:
raise RuntimeError(
f"Audio file '{unsupported_name}.{suffix}' in '{target_folder}' has a common unsupported format. "
"Use OGG format instead. Suppress this error with the '--disable-sound-format-error' option."
)

print(f"packing {COUNTER} files complete")


Expand Down

0 comments on commit fff5410

Please sign in to comment.