Browse Source

Can now convert several files in a single command

pull/108/head
Louis 9 years ago
parent
commit
354e3582c8
  1. 42
      patacrep/songs/convert/__main__.py

42
patacrep/songs/convert/__main__.py

@ -3,6 +3,7 @@
See the :meth:`__usage` method for more information.
"""
import os
import logging
import sys
@ -12,17 +13,28 @@ from patacrep import files
LOGGER = logging.getLogger(__name__)
def __usage():
return "python3 -m patacrep.songs.convert chordpro latex FILE"
return "python3 -m patacrep.songs.convert INPUTFORMAT OUTPUTFORMAT FILES"
def yesno(prompt):
while True:
answer = input("{} [yn] ".format(prompt))
if answer.strip().lower() == "y":
return True
if answer.strip().lower() == "n":
return False
def confirm(destname):
return yesno("File '{}' already exist. Overwrite?".format(destname))
if __name__ == "__main__":
if len(sys.argv) != 4:
if len(sys.argv) < 4:
LOGGER.error("Invalid number of arguments.")
LOGGER.error("Usage: %s", __usage())
sys.exit(1)
source = sys.argv[1]
dest = sys.argv[2]
file = sys.argv[3]
song_files = sys.argv[3:]
song_parsers = files.load_plugins(
datadirs=DEFAULT_CONFIG.get('datadir', []),
@ -38,11 +50,23 @@ if __name__ == "__main__":
)
sys.exit(1)
song = song_parsers[source](".", file, DEFAULT_CONFIG)
try:
print(song.render(dest))
except NotImplementedError:
LOGGER.error("Cannot convert to format '%s'.", dest)
sys.exit(1)
for file in song_files:
song = song_parsers[source](".", file, DEFAULT_CONFIG)
try:
converted = song.render(dest)
destname = "{}.{}".format(".".join(file.split(".")[:-1]), dest)
if os.path.exists(destname):
if not confirm(destname):
continue
with open(destname, "w") as destfile:
destfile.write(converted)
except NotImplementedError:
LOGGER.error("Cannot convert to format '%s'.", dest)
sys.exit(1)
except KeyboardInterrupt:
print()
LOGGER.info("Aborted by user.")
sys.exit(0)
sys.exit(0)

Loading…
Cancel
Save