Browse Source

Merge branch 'split-songs' of git://git.lohrun.net/songbook

remotes/origin/HEAD
Romain Goffe 12 years ago
parent
commit
f098811a4e
  1. 110
      songbook.py

110
songbook.py

@ -16,56 +16,63 @@ reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+')
reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+')
class Song:
def __init__(self, title, artist, album, path):
self.title = title
self.artist = artist
self.album = album
self.path = path
def __repr__(self):
return repr((self.title, self.artist, self.album, self.path))
def copyCovers():
'''
Copy all covers found in songs/ hierarchy into a same folder. This
allows a much faster search for pdflatex since the \graphicspath
macro now only contains a single directory instead of quite a long
list to search through.
'''
#create "covers/" directory if it does not exist
d = os.path.dirname("covers/")
if not os.path.exists(d):
os.makedirs(d)
covers = list(set(glob.glob('songs/*/*.jpg')))
for cover in covers:
f = "covers/" + os.path.basename(cover)
if(os.path.exists(f) == False):
shutil.copy(cover, f)
def __init__(self, title, artist, album, path):
self.title = title
self.artist = artist
self.album = album
self.path = path
def __repr__(self):
return repr((self.title, self.artist, self.album, self.path))
from xdg.BaseDirectory import *
print 'xdg_data_home: %s' % xdg_data_home
print 'xdg_data_dirs: %s' % xdg_data_dirs
print 'xdg_config_home: %s' % xdg_config_home
print 'xdg_config_dirs: %s' % xdg_config_dirs
print 'xdg_cache_home: %s' % xdg_cache_home
songbook_cache_home = os.path.join(xdg_cache_home, 'songbook')
def makeCoverCache(cachePath):
'''
Copy all pictures found in the libraries into a unique cache
folder.
'''
# create the cache directory if it does not exist
if not os.path.exists(cachePath):
os.makedirs(cachePath)
# copy pictures file into the cache directory
covers = glob.glob('songs/*/*.jpg')
for cover in covers:
coverPath = os.path.join(cachePath, os.path.basename(cover))
shutil.copy(cover, coverPath)
def matchRegexp(reg, iterable):
return [ m.group(1) for m in (reg.match(l) for l in iterable) if m ]
def songslist(songs):
song_objects = []
for s in songs:
path = 'songs/'+s
with open(path, 'r+') as f:
data = f.read()
title = reTitle.search(data).group(0)
artist = reArtist.search(data.replace("{","")).group(0)
match = reAlbum.search(data.replace("{",""))
if match:
album = match.group(0)
else:
album = ''
song_objects.append(Song(title, artist, album, path))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.title))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.album))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.artist))
result = [ '\\input{{{0}}}'.format(song.path.replace("\\","/").strip()) for song in song_objects ]
return '\n'.join(result)
song_objects = []
for s in songs:
path = 'songs/'+s
with open(path, 'r+') as f:
data = f.read()
title = reTitle.search(data).group(0)
artist = reArtist.search(data.replace("{","")).group(0)
match = reAlbum.search(data.replace("{",""))
if match:
album = match.group(0)
else:
album = ''
song_objects.append(Song(title, artist, album, path))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.title))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.album))
song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.artist))
result = [ '\\input{{{0}}}'.format(song.path.replace("\\","/").strip()) for song in song_objects ]
return '\n'.join(result)
def parseTemplate(template):
embeddedJsonPattern = re.compile(r"^%%:")
@ -161,12 +168,10 @@ def makeTexFile(sb, output):
def makeDepend(sb, output):
name = output[:-2]
#dependsPattern = re.compile(r"^[^%]*(?:include|input)\{(.*?)\}")
indexPattern = re.compile(r"^[^%]*\\(?:newauthor|new)index\{.*\}\{(.*?)\}")
lilypondPattern = re.compile(r"^[^%]*\\(?:lilypond)\{(.*?)\}")
# check for deps (in sb data)
#deps = matchRegexp(dependsPattern, [ v for v in sb.itervalues() if type(v) is not list ])
deps = [];
if sb["songs"] == "all":
deps += glob.glob('songs/*/*.sg')
@ -206,7 +211,7 @@ def main():
try:
opts, args = getopt.getopt(sys.argv[1:],
"hs:o:d",
["help","songbook=","output=","depend"])
["help","songbook=","output=","depend","cache"])
except getopt.GetoptError, err:
# print help and exit
print str(err)
@ -216,11 +221,14 @@ def main():
songbook = None
depend = False
output = None
cache = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("--cache"):
cache = True
elif o in ("-s", "--songbook"):
songbook = a
elif o in ("-d", "--depend"):
@ -230,13 +238,13 @@ def main():
else:
assert False, "unhandled option"
if songbook and output:
if cache:
makeCoverCache(os.path.join(songbook_cache_home, 'images'))
elif songbook and output:
f = open(songbook)
sb = json.load(f)
f.close()
copyCovers()
if depend:
makeDepend(sb, output)
else:

Loading…
Cancel
Save