Browse Source

factorize valid_filename

pull/232/head
Oliverpool 9 years ago
parent
commit
7464dda625
  1. 13
      patacrep/tools/__init__.py
  2. 12
      patacrep/tools/cache/__main__.py
  3. 14
      patacrep/tools/content/__main__.py

13
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))

12
patacrep/tools/cache/__main__.py

@ -9,18 +9,10 @@ import textwrap
from patacrep import errors from patacrep import errors
from patacrep.songbook import open_songbook from patacrep.songbook import open_songbook
from .. import existing_file
LOGGER = logging.getLogger("patatools.cache") 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(): def commandline_parser():
"""Return a command line parser.""" """Return a command line parser."""
@ -45,7 +37,7 @@ def commandline_parser():
'songbook', 'songbook',
metavar="SONGBOOK", metavar="SONGBOOK",
help=textwrap.dedent("""Songbook file to be used to look for cache path."""), help=textwrap.dedent("""Songbook file to be used to look for cache path."""),
type=filename, type=existing_file,
) )
clean.set_defaults(command=do_clean) clean.set_defaults(command=do_clean)

14
patacrep/tools/content/__main__.py

@ -3,7 +3,6 @@
import argparse import argparse
import logging import logging
import os import os
import shutil
import sys import sys
import textwrap import textwrap
import yaml import yaml
@ -11,18 +10,10 @@ import yaml
from patacrep import errors from patacrep import errors
from patacrep.songbook import open_songbook from patacrep.songbook import open_songbook
from patacrep.build import Songbook from patacrep.build import Songbook
from .. import existing_file
LOGGER = logging.getLogger("patatools.content") 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(): def commandline_parser():
"""Return a command line parser.""" """Return a command line parser."""
@ -47,7 +38,7 @@ def commandline_parser():
'songbook', 'songbook',
metavar="SONGBOOK", metavar="SONGBOOK",
help=textwrap.dedent("""Songbook file to be used to look for content items."""), 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) 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)) print(yaml.safe_dump(content_items, allow_unicode=True, default_flow_style=False))
def normalize_song_path(file_entry, ref_dir): def normalize_song_path(file_entry, ref_dir):
"""Normalize the 'song' value, relative to ref_dir"""
if 'song' in file_entry: if 'song' in file_entry:
file_entry['song'] = os.path.relpath(file_entry['song'], ref_dir) file_entry['song'] = os.path.relpath(file_entry['song'], ref_dir)
return file_entry return file_entry

Loading…
Cancel
Save