dottes/auto_render.py

82 lines
2.3 KiB
Python

#!/usr/bin/python3 -u
import logging
import pathlib
import subprocess
import sys
import time
win_root = pathlib.Path(r"C:\Users\louis\GitKraken\dottes")
lin_root = pathlib.Path("~/dottes")
file_update_times = {}
def run_continuous():
logging.info("Run continous")
pwd = pathlib.Path()
while True:
for obj_path in pwd.rglob("*"):
obj_path_str = str(obj_path)
if not obj_path.is_file():
continue
if not obj_path_str.lower().endswith(".abc"):
continue
if obj_path_str not in file_update_times:
file_update_times[obj_path_str] = obj_path.lstat().st_mtime
else:
if obj_path.lstat().st_mtime > file_update_times[obj_path_str]:
file_update_times[obj_path_str] = obj_path.lstat().st_mtime
convert_single(lin_path=obj_path_str)
def convert_single(win_path: str = None, lin_path: str = None):
if win_path:
input_path = win_path
win_abc_file_path = pathlib.Path(win_path)
lin_abc_file_path = lin_root / win_abc_file_path.relative_to(win_root)
elif lin_path:
input_path = lin_path
lin_abc_file_path = pathlib.Path(lin_path)
else:
raise Exception("Argument win_path or lin_path must be given")
logging.info(f"Converting {input_path}")
out_file_name = lin_abc_file_path.name.rsplit(".", 1)[0] + ".mid"
output_path = lin_abc_file_path.parent / out_file_name
if output_path.exists():
output_path.unlink()
r = subprocess.run(
[
"abc2midi",
lin_abc_file_path.as_posix(),
"-o", output_path.as_posix()
]
)
if r.returncode != 0:
logging.error(f"Error whilst convering {lin_abc_file_path}")
else:
logging.info(f"Converted {input_path} > {output_path}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
if len(sys.argv) > 1:
if sys.platform == "win32":
convert_single(win_path=sys.argv[1])
elif sys.platform == "linux":
convert_single(lin_path=sys.argv[1])
else:
raise Exception(f"Unknown platform '{sys.platform}', don't know how to interpret path")
else:
run_continuous()