From b0ae14852e062d2b2d67bdd165ac190ed5b52864 Mon Sep 17 00:00:00 2001 From: spalax Date: Sun, 10 Mar 2013 14:28:34 +0100 Subject: [PATCH] use recursive find method to get song and cover files With this, the songs hierarchy in the library can be different from songs/author/song.sg --- songbook.py | 9 +++++---- utils/__init__.py | 0 utils/resize-cover.py | 5 +++-- utils/rules.py | 4 +++- utils/songbook-gtab.py | 5 ++--- utils/utils.py | 13 +++++++++++++ 6 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/utils.py diff --git a/songbook.py b/songbook.py index f443947b..41b4b3df 100755 --- a/songbook.py +++ b/songbook.py @@ -4,7 +4,6 @@ import getopt, sys import os.path -import glob import re import json import locale @@ -12,6 +11,8 @@ import shutil import locale import platform +from utils.utils import recursiveFind + reTitle = re.compile('(?<=beginsong\\{)(.(? 0: out.write(formatDefinition('songslist', songslist(library, songs))) @@ -184,7 +185,7 @@ def makeDepend(sb, library, output): # check for deps (in sb data) deps = []; if sb["songs"] == "all": - deps += glob.glob(library + 'songs/*/*.sg') + deps += recursiveFind(os.path.join(library, 'songs'), '*.sg') else: deps += map(lambda x: library + "songs/" + x, sb["songs"]) diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/utils/resize-cover.py b/utils/resize-cover.py index c7f9daf3..21964487 100755 --- a/utils/resize-cover.py +++ b/utils/resize-cover.py @@ -6,10 +6,11 @@ #Description: Resize all covers to 128,128 thumbnails import Image -import glob + +from utils.utils import recursiveFind # Process song files -covers = glob.glob('songs/*/*.jpg') +covers = recursiveFind(os.path.join(library, 'songs'), '*.jpg') for filename in covers: source = Image.open(filename) diff --git a/utils/rules.py b/utils/rules.py index b1dfbb11..52b6e0b3 100755 --- a/utils/rules.py +++ b/utils/rules.py @@ -8,6 +8,8 @@ import logging import locale re.LOCALE +from utils.utils import recursiveFind + # the dictionary has target_word:replacement_word pairs word_dic = { ##: oe inclusion @@ -236,7 +238,7 @@ def main(): usage() sys.exit(2) - songfiles = glob.glob('songs/*/*.sg') + songfiles = recursiveFind(os.path.join(library, 'songs'), '*.sg') loglevel = "warning" for option, arg in opts: diff --git a/utils/songbook-gtab.py b/utils/songbook-gtab.py index 3a67355c..36a8e69d 100755 --- a/utils/songbook-gtab.py +++ b/utils/songbook-gtab.py @@ -2,9 +2,9 @@ # import sys -import glob import re from optparse import OptionParser +from utils.utils import recursiveFind # Pattern set to ignore latex command in title prefix gtabPattern = re.compile(r"\\gtab\{(.*)\}\{(.*)\}"); @@ -26,8 +26,7 @@ def main(): chords = dict() positions = dict() - songfiles = glob.glob('songs/*/*.sg') - + songfiles = recursiveFind(os.path.join(library, 'songs'), '*.sg') for file in songfiles: for line in open(file): result = gtabPattern.match(line) diff --git a/utils/utils.py b/utils/utils.py new file mode 100644 index 00000000..576c66b2 --- /dev/null +++ b/utils/utils.py @@ -0,0 +1,13 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# + +import fnmatch +import os + +def recursiveFind(root_directory, pattern): + matches = [] + for root, dirnames, filenames in os.walk(root_directory): + for filename in fnmatch.filter(filenames, pattern): + matches.append(os.path.join(root, filename)) + return matches