diff --git a/patacrep/tools/__init__.py b/patacrep/tools/__init__.py index e69de29b..84a58f68 100644 --- a/patacrep/tools/__init__.py +++ b/patacrep/tools/__init__.py @@ -0,0 +1,13 @@ +"""Common functions for patatools""" + +import argparse +import os + +def existing_file(name): + """Check that argument is an existing, readable file name. + + Return the argument for convenience. + """ + if os.path.isfile(name) and os.access(name, os.R_OK): + return name + raise argparse.ArgumentTypeError("Cannot read file '{}'.".format(name)) \ No newline at end of file diff --git a/patacrep/tools/cache/__main__.py b/patacrep/tools/cache/__main__.py index f48de224..e5aada68 100644 --- a/patacrep/tools/cache/__main__.py +++ b/patacrep/tools/cache/__main__.py @@ -9,18 +9,10 @@ import textwrap from patacrep import errors from patacrep.songbook import open_songbook +from .. import existing_file LOGGER = logging.getLogger("patatools.cache") -def filename(name): - """Check that argument is an existing, readable file name. - - Return the argument for convenience. - """ - if os.path.isfile(name) and os.access(name, os.R_OK): - return name - raise argparse.ArgumentTypeError("Cannot read file '{}'.".format(name)) - def commandline_parser(): """Return a command line parser.""" @@ -45,7 +37,7 @@ def commandline_parser(): 'songbook', metavar="SONGBOOK", help=textwrap.dedent("""Songbook file to be used to look for cache path."""), - type=filename, + type=existing_file, ) clean.set_defaults(command=do_clean) diff --git a/patacrep/tools/content/__main__.py b/patacrep/tools/content/__main__.py index 4bc45410..0f040e38 100644 --- a/patacrep/tools/content/__main__.py +++ b/patacrep/tools/content/__main__.py @@ -3,7 +3,6 @@ import argparse import logging import os -import shutil import sys import textwrap import yaml @@ -11,18 +10,10 @@ import yaml from patacrep import errors from patacrep.songbook import open_songbook from patacrep.build import Songbook +from .. import existing_file LOGGER = logging.getLogger("patatools.content") -def filename(name): - """Check that argument is an existing, readable file name. - - Return the argument for convenience. - """ - if os.path.isfile(name) and os.access(name, os.R_OK): - return name - raise argparse.ArgumentTypeError("Cannot read file '{}'.".format(name)) - def commandline_parser(): """Return a command line parser.""" @@ -47,7 +38,7 @@ def commandline_parser(): 'songbook', metavar="SONGBOOK", help=textwrap.dedent("""Songbook file to be used to look for content items."""), - type=filename, + type=existing_file, ) content_items.set_defaults(command=do_content_items) @@ -69,6 +60,7 @@ def do_content_items(namespace): print(yaml.safe_dump(content_items, allow_unicode=True, default_flow_style=False)) def normalize_song_path(file_entry, ref_dir): + """Normalize the 'song' value, relative to ref_dir""" if 'song' in file_entry: file_entry['song'] = os.path.relpath(file_entry['song'], ref_dir) return file_entry