Browse Source

Cleanup methods for search image and cover

pull/98/head
Oliverpool 9 years ago
parent
commit
996652401e
  1. 67
      patacrep/songs/__init__.py

67
patacrep/songs/__init__.py

@ -194,28 +194,6 @@ class Song:
"""
raise NotImplementedError()
def get_cover_fullpath(self, datadir, default=None):
"""Return the fullpath of the cover file if found"""
filename = str(self.data.get('cov', ''))
if not filename:
return default
# Temporary hack: what should it contain exactly?
config = {
'datadir': datadir,
'filename': '',
}
extensions = ['', '.jpg', '.png']
for extension in extensions:
fullpath = search_image(filename + extension, self.fullpath, config)
if fullpath:
return fullpath
# How to handle when the cover should have been there ?
# raise FileNotFoundError()
return default
def get_datadirs(self, subdir=None):
"""Return a list of existing datadirs (with eventually a subdir)
"""
@ -258,6 +236,15 @@ class Song:
filepath = self.search_file(filename, ['', '.jpg', '.png'], datadir_img)
return filepath if filepath else filename
@property
def cover_filepath(self):
"""Get the path to the cover file (or None if not found)"""
filename = str(self.data.get('cov', ''))
if not filename:
return None
datadir_img = self.get_datadirs('img')
return self.search_file(filename, ['', '.jpg', '.png'], datadir_img)
def search_partition(self, filename):
"""Search for a lilypond file"""
filepath = self.search_file(filename, ['', '.ly'])
@ -271,39 +258,3 @@ def unprefixed_title(title, prefixes):
if match:
return match.group(2)
return title
def search_image(image, chordprofile, config, default=None):
"""Return the file name of an image, so that LaTeX will find it.
:param str image: The name, as provided in the chordpro file.
:param str chordprofile: The name of the file including this image.
:param dict config: Songbook configuration dictionary.
The image can be:
- in the same directory as the including song file;
- in the same directory as the main LaTeX file;
- in some of the `DATADIR/img` directories.
If image is not found, the `image` argument is returned.
"""
# Image is in the same folder as its song
texdir = os.path.dirname(chordprofile)
if os.path.exists(os.path.join(texdir, image)):
return os.path.join(texdir, image)
# Image is in the same directory as the main tex file
rootdir = os.path.dirname(os.path.join(
os.getcwd(),
config['filename'],
))
if os.path.exists(os.path.join(rootdir, image)):
return image
# Image is in a datadir
for directory in config['datadir']:
if os.path.exists(os.path.join(directory, 'img', image)):
return os.path.join(directory, 'img', image)
# Could not find image
return default

Loading…
Cancel
Save