Add __name__ == "__main__" construct

This commit is contained in:
Mark Cornick 2022-08-18 00:30:28 -04:00
parent 7f7c3f03d1
commit dd7fa0f5b8

View File

@ -26,53 +26,58 @@
import re import re
import sys import sys
cue_file = sys.argv[1]
meta_file = re.sub(r"cue$", "meta", cue_file)
try: def cue_to_meta(cue_file):
with open(cue_file) as f: meta_file = re.sub(r"cue$", "meta", cue_file)
cue = f.readlines()
except FileNotFoundError:
sys.stderr.write("{} not found\n".format(cue_file))
sys.exit(1)
except IsADirectoryError:
sys.stderr.write("{} is a directory\n".format(cue_file))
sys.exit(1)
except PermissionError:
sys.stderr.write("No permission to read {}\n".format(cue_file))
sys.exit(1)
except OSError:
sys.stderr.write("OS error trying to read {}\n".format(cue_file))
sys.exit(1)
title = None try:
performer = None with open(cue_file) as f:
timestamp = None cue = f.readlines()
except FileNotFoundError:
sys.stderr.write("{} not found\n".format(cue_file))
sys.exit(1)
except IsADirectoryError:
sys.stderr.write("{} is a directory\n".format(cue_file))
sys.exit(1)
except PermissionError:
sys.stderr.write("No permission to read {}\n".format(cue_file))
sys.exit(1)
except OSError:
sys.stderr.write("OS error trying to read {}\n".format(cue_file))
sys.exit(1)
try: title = None
with open(meta_file, "w") as f: performer = None
for line in cue: timestamp = None
title_re = re.search(r'^ TITLE "(.*)"$', line)
performer_re = re.search(r'^ PERFORMER "(.*)"$', line) try:
index_re = re.search(r"^ INDEX 01 (.*)$", line) with open(meta_file, "w") as f:
if title_re: for line in cue:
title = title_re[1] title_re = re.search(r'^ TITLE "(.*)"$', line)
if performer_re: performer_re = re.search(r'^ PERFORMER "(.*)"$', line)
performer = performer_re[1] index_re = re.search(r"^ INDEX 01 (.*)$", line)
if index_re: if title_re:
time_re = re.search(r"^(..):(..):..$", index_re[1]) title = title_re[1]
timestamp = "00{}{}".format(time_re[1], time_re[2]) if performer_re:
if title and performer and timestamp: performer = performer_re[1]
f.write("{}:{} - {}\n".format(timestamp, performer, title)) if index_re:
title = None time_re = re.search(r"^(..):(..):..$", index_re[1])
performer = None timestamp = "00{}{}".format(time_re[1], time_re[2])
timestamp = None if title and performer and timestamp:
except IsADirectoryError: f.write("{}:{} - {}\n".format(timestamp, performer, title))
sys.stderr.write("{} is a directory\n".format(meta_file)) title = None
sys.exit(1) performer = None
except PermissionError: timestamp = None
sys.stderr.write("No permission to write {}\n".format(meta_file)) except IsADirectoryError:
sys.exit(1) sys.stderr.write("{} is a directory\n".format(meta_file))
except OSError: sys.exit(1)
sys.stderr.write("OS error trying to write {}\n".format(meta_file)) except PermissionError:
sys.exit(1) sys.stderr.write("No permission to write {}\n".format(meta_file))
sys.exit(1)
except OSError:
sys.stderr.write("OS error trying to write {}\n".format(meta_file))
sys.exit(1)
if __name__ == "__main__":
cue_to_meta(sys.argv[1])