Skip to content

Commit

Permalink
Merge pull request music-encoding#1458 from rettinghaus/pathlib
Browse files Browse the repository at this point in the history
build: drop os and use pathlib in generate-examples.py
  • Loading branch information
musicEnfanthen authored May 15, 2024
2 parents fe2b438 + b21578c commit 5bc7405
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions utils/examples/generate-examples.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import argparse
import os
import sys
import pprint
import logging
from pathlib import Path

import verovio

IMAGES_PATH = "../../build/assets/images/GeneratedImages"
IMAGES_PATH = Path("../../build/assets/images/GeneratedImages")

VRV_OPTIONS: dict = {
'scale': 10,
Expand Down Expand Up @@ -62,39 +62,31 @@
log.debug("Running Verovio with the following options: %s", pprint.pformat(VRV_OPTIONS))
tk.setOptions(VRV_OPTIONS)

for file in os.listdir(IMAGES_PATH):
# Skip everything that is not an .mei or .xml file
if not file.endswith(".mei"):
continue

mei_file = os.path.join(IMAGES_PATH, file)
svg_file = os.path.join(IMAGES_PATH, f"{file}.svg")
for mei_file in IMAGES_PATH.glob("*.mei"):
svg_file = mei_file.with_suffix(".svg")

if os.path.exists(svg_file) and os.path.exists(mei_file) and not args.clean:
log.info("This example already exists, and we're not running in cleaning mode. Skipping.")
if svg_file.exists() and mei_file.exists() and not args.clean:
log.info("%s already exists, and we're not running in cleaning mode. Skipping.", svg_file.name)
continue

# Download the MEI file from the given url
mei_example: str = ""
with open(mei_file) as f:
mei_example = f.read()
mei_example: str = mei_file.read_text()

if not tk.loadData(mei_example):
log.error("Failed to load %s", mei_file)
log.error("Failed to load %s", mei_file.name)
success = False
continue

svg: str = tk.renderToSVG(1)

if svg:
with open(svg_file, 'w') as f:
f.write(svg)
log.info("Successfully rendered %s", svg_file)
svg_file.write_text(svg)
log.info("Successfully rendered %s", svg_file.name)
else:
log.error("Failed to render %s", mei_file)
log.error("Failed to render %s", mei_file.name)
success = False

log.debug("Finished processing %s", mei_file)
log.debug("Finished processing %s", mei_file.name)

if not success:
sys.exit(1)
Expand Down

0 comments on commit 5bc7405

Please sign in to comment.