From 9be3072e4e367f0929987b83a74dabf5f32a5af0 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 9 Jan 2016 23:11:07 +0100 Subject: [PATCH] Turn `patacrep.songs.convert` script into a `patatools` sub-command --- patacrep/tools/convert/__init__.py | 0 patacrep/{songs => tools}/convert/__main__.py | 31 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 patacrep/tools/convert/__init__.py rename patacrep/{songs => tools}/convert/__main__.py (75%) diff --git a/patacrep/tools/convert/__init__.py b/patacrep/tools/convert/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/patacrep/songs/convert/__main__.py b/patacrep/tools/convert/__main__.py similarity index 75% rename from patacrep/songs/convert/__main__.py rename to patacrep/tools/convert/__main__.py index 19d7041e..2d308b4a 100644 --- a/patacrep/songs/convert/__main__.py +++ b/patacrep/tools/convert/__main__.py @@ -1,7 +1,4 @@ -"""Conversion between formats - -See the :meth:`__usage` method for more information. -""" +"""`patatools.convent` command: convert between song formats""" import os import logging @@ -11,27 +8,32 @@ from patacrep import files from patacrep.songs import DEFAULT_CONFIG from patacrep.utils import yesno -LOGGER = logging.getLogger(__name__) +LOGGER = logging.getLogger("patatools.convert") +SUBCOMMAND_DESCRIPTION = "Convert between song formats" -def __usage(): - return "python3 -m patacrep.songs.convert INPUTFORMAT OUTPUTFORMAT FILES" +def _usage(): + return "patatools convert INPUTFORMAT OUTPUTFORMAT FILES" def confirm(destname): + """Ask whether destination name should be overwrited.""" while True: try: return yesno(input("File '{}' already exist. Overwrite? [yn] ".format(destname))) except ValueError: continue -if __name__ == "__main__": - if len(sys.argv) < 4: +def main(args=None): + """Main function: run from command line.""" + if args is None: + args = sys.argv[1:] + if len(args) < 3: LOGGER.error("Invalid number of arguments.") - LOGGER.error("Usage: %s", __usage()) + LOGGER.error("Usage: %s", _usage()) sys.exit(1) - source = sys.argv[1] - dest = sys.argv[2] - song_files = sys.argv[3:] + source = args[0] + dest = args[1] + song_files = args[2:] renderers = files.load_plugins( datadirs=DEFAULT_CONFIG.get('datadir', []), @@ -73,3 +75,6 @@ if __name__ == "__main__": sys.exit(0) sys.exit(0) + +if __name__ == "__main__": + main()