Implement the 'ignore case' flag (see below).

Chri [2010-07-13 12:32]
Implement the 'ignore case' flag (see below).

TODO: Don't build the list of existing files if not using this flag.
Filename
pl.py
diff --git a/pl.py b/pl.py
index 79a5fe2..49c1151 100755
--- a/pl.py
+++ b/pl.py
@@ -63,7 +63,7 @@ def main():
     valid_actions = ['check', 'create']
     parser = optparse.OptionParser()
     parser.usage = "%prog [OPTIONS...] ACTION PLAYLIST [OTHER_PLAYLIST]\n" + \
-       "   [-defhtV] [--delete] [--error-stop] [--fix]\n" + \
+       "   [-defhitV] [--delete] [--error-stop] [--fix] [--ignore-case]\n" + \
        "   [-n NB_SUGGS] [-p PREFIX] [-s SCORE] [-t TARGET]\n" + \
        "   [--prefix PREFIX] [--score SCORE] [--target TARGET]\n" + \
        "   [--help] [--version]\n" + \
@@ -92,6 +92,9 @@ def main():
     parser.add_option('-f', '--fix', default=False,
        action='store_true', dest='fix',
        help="check - prompt for fixes among suggestions")
+    parser.add_option('-i', '--ignore-case', default=False,
+       action='store_true', dest='ignore_case',
+       help="check - don't be case sensitive")
     parser.add_option('-n', '--nb-suggs', default=0,
        action='store', dest='nb_suggs', type='int',
        help="check - max number of suggestions to display")
@@ -150,8 +153,12 @@ def main():

     if action == 'check':
         bad_playlists = []
+        # build the list of present files
+        existing_files = music_files_list(options.target, options.prefix)
+        if options.ignore_case:
+            existing_files = [elt.lower() for elt in existing_files]
         for playlist in playlists:
-            if os.path.isfile(playlist) and not playlist_is_ok(playlist, options):
+            if os.path.isfile(playlist) and not playlist_is_ok(playlist, options, existing_files):
                 bad_playlists.append(playlist)
                 if options.error_stop:
                     break
@@ -162,16 +169,14 @@ def main():
             else:
                 print "Everything is OK."
     elif action == 'create':
-        pl_list = music_files_list(options.target)
-        pl_list = [elt.replace(options.target, options.prefix) \
-                   for elt in pl_list]
+        pl_list = music_files_list(options.target, options.prefix)
         pl_file = open(playlists[0], 'a')
         pl_file.write('\n' + '\n'.join(pl_list))
     else:
         print "This unknown action should have been caught as an error."
         print "Please report this bug precising '%s not implemented'." % action

-def playlist_is_ok(playlist, options):
+def playlist_is_ok(playlist, options, existing_files=None):
     items = []
     try:
         pl_file = open(playlist, 'r')
@@ -196,9 +201,10 @@ def playlist_is_ok(playlist, options):
             item = item.strip()
             # ignore empty and commented lines
             if item and not item.startswith('#'):
-                path_to_check = options.target + item
-                if not os.path.isfile(path_to_check):
-                    missing_items.add(path_to_check)
+                if options.ignore_case:
+                    item = item.lower()
+                if not item in existing_files:
+                    missing_items.add(item)
                     if options.fix:
                         new_playlist.append(REPLACE_MARKER + item)
                 else:
@@ -307,13 +313,15 @@ def playlist_is_ok(playlist, options):
                     if options.fix:
                         print "Those entries were fixed."

-def music_files_list(path):
+def music_files_list(path, prefix=None):
     filenames = []
     for root, dirs, files in os.walk(path):
         for filename in files:
             # [1:] because we must remove the dot from the extension
             if os.path.splitext(filename)[1][1:] in MUSIC_EXT:
                 filenames.append(os.path.join(root, filename))
+    if prefix is not None:
+        filenames = [elt.replace(path, prefix) for elt in filenames]
     filenames.sort()
     return filenames
ViewGit