|
@ -194,47 +194,63 @@ class Song: |
|
|
""" |
|
|
""" |
|
|
raise NotImplementedError() |
|
|
raise NotImplementedError() |
|
|
|
|
|
|
|
|
def unprefixed_title(title, prefixes): |
|
|
def get_datadirs(self, subdir=None): |
|
|
"""Remove the first prefix of the list in the beginning of title (if any). |
|
|
"""Return an iterator of existing datadirs (with eventually a subdir) |
|
|
""" |
|
|
""" |
|
|
for prefix in prefixes: |
|
|
for directory in self.config['datadir']: |
|
|
match = re.compile(r"^(%s)\b\s*(.*)$" % prefix, re.LOCALE).match(title) |
|
|
fullpath = os.path.join(directory, subdir) |
|
|
if match: |
|
|
if os.path.isdir(fullpath): |
|
|
return match.group(2) |
|
|
yield fullpath |
|
|
return title |
|
|
|
|
|
|
|
|
|
|
|
def search_image(image, chordprofile, config): |
|
|
def search_file(self, filename, extensions=None, directories=None): |
|
|
"""Return the file name of an image, so that LaTeX will find it. |
|
|
"""Search for a file name. |
|
|
|
|
|
|
|
|
:param str image: The name, as provided in the chordpro file. |
|
|
:param str filename: The name, as provided in the chordpro file (with or without extension). |
|
|
:param str chordprofile: The name of the file including this image. |
|
|
:param list extensions: Possible extensions (with '.') |
|
|
:param dict config: Songbook configuration dictionary. |
|
|
:param iterator directories: Other directories where to search for the file |
|
|
|
|
|
The directory where the Song file is stored is added to the list. |
|
|
|
|
|
|
|
|
The image can be: |
|
|
Returns None if nothing found. |
|
|
|
|
|
""" |
|
|
|
|
|
if extensions is None: |
|
|
|
|
|
extensions = [''] |
|
|
|
|
|
if directories is None: |
|
|
|
|
|
directories = [] |
|
|
|
|
|
|
|
|
- in the same directory as the including song file; |
|
|
songdir = os.path.dirname(self.fullpath) |
|
|
- 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. |
|
|
for directory in [songdir] + list(directories): |
|
|
""" |
|
|
for extension in extensions: |
|
|
# Image is in the same folder as its song |
|
|
fullpath = os.path.join(directory, filename + extension) |
|
|
texdir = os.path.dirname(chordprofile) |
|
|
if os.path.isfile(fullpath): |
|
|
if os.path.exists(os.path.join(texdir, image)): |
|
|
return fullpath |
|
|
return os.path.join(texdir, image) |
|
|
return None |
|
|
|
|
|
|
|
|
# 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 |
|
|
def search_image(self, filename): |
|
|
for directory in config['datadir']: |
|
|
"""Search for an image file""" |
|
|
if os.path.exists(os.path.join(directory, 'img', image)): |
|
|
datadir_img = self.get_datadirs('img') |
|
|
return os.path.join(directory, 'img', image) |
|
|
filepath = self.search_file(filename, ['', '.jpg', '.png'], datadir_img) |
|
|
|
|
|
return filepath if filepath else filename |
|
|
|
|
|
|
|
|
# Could not find image |
|
|
@property |
|
|
return image |
|
|
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']) |
|
|
|
|
|
return filepath if filepath else filename |
|
|
|
|
|
|
|
|
|
|
|
def unprefixed_title(title, prefixes): |
|
|
|
|
|
"""Remove the first prefix of the list in the beginning of title (if any). |
|
|
|
|
|
""" |
|
|
|
|
|
for prefix in prefixes: |
|
|
|
|
|
match = re.compile(r"^(%s)\b\s*(.*)$" % prefix, re.LOCALE).match(title) |
|
|
|
|
|
if match: |
|
|
|
|
|
return match.group(2) |
|
|
|
|
|
return title |
|
|