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. See the :meth:`__usage` method for more information.
""" """
import os
import logging import logging
import sys import sys
@ -12,17 +13,28 @@ from patacrep import files
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def __usage(): 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 __name__ == "__main__":
if len(sys.argv) != 4: if len(sys.argv) < 4:
LOGGER.error("Invalid number of arguments.") LOGGER.error("Invalid number of arguments.")
LOGGER.error("Usage: %s", __usage()) LOGGER.error("Usage: %s", __usage())
sys.exit(1) sys.exit(1)
source = sys.argv[1] source = sys.argv[1]
dest = sys.argv[2] dest = sys.argv[2]
file = sys.argv[3] song_files = sys.argv[3:]
song_parsers = files.load_plugins( song_parsers = files.load_plugins(
datadirs=DEFAULT_CONFIG.get('datadir', []), datadirs=DEFAULT_CONFIG.get('datadir', []),
@ -38,11 +50,23 @@ if __name__ == "__main__":
) )
sys.exit(1) sys.exit(1)
song = song_parsers[source](".", file, DEFAULT_CONFIG) for file in song_files:
try: song = song_parsers[source](".", file, DEFAULT_CONFIG)
print(song.render(dest)) try:
except NotImplementedError: converted = song.render(dest)
LOGGER.error("Cannot convert to format '%s'.", dest) destname = "{}.{}".format(".".join(file.split(".")[:-1]), dest)
sys.exit(1) 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) sys.exit(0)

Loading…
Cancel
Save