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

Loading…
Cancel
Save