[xiph-cvs] cvs commit: positron/positron/db PAI.py WOID.py

Stan Seibert volsung at xiph.org
Sat Sep 6 16:02:55 PDT 2003



volsung     03/09/06 19:02:55

  Modified:    positron add_file.py audiofile.py cmd_add.py cmd_clear.py
                        cmd_pack.py cmd_rebuild.py cmd_sync.py
               positron/db PAI.py WOID.py
  Log:
  Much improved sorting.  Patch from Brett Smith <bcsmit1 at engr.uky.edu>.

Revision  Changes    Path
1.4       +18 -2     positron/positron/add_file.py

Index: add_file.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/add_file.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- add_file.py	9 Jun 2003 01:04:46 -0000	1.3
+++ add_file.py	6 Sep 2003 23:02:55 -0000	1.4
@@ -3,6 +3,7 @@
 # add_file.py - utilities for file addition
 #
 # Copyright (C) 2003, Xiph.org Foundation
+# Copyright (C) 2003 Brett Smith <bretts at canonical.org>
 #
 # This file is part of positron.
 #
@@ -70,10 +71,25 @@
         util.copy_file(sourcename, targetname)
 
     # Create DB entry
+    destination = neuros.hostpath_to_neurospath(targetname)
     record = (metadata["title"], None, metadata["artist"], metadata["album"],
               metadata["genre"], recording, metadata["length"],
-              metadata["size"] // 1024,
-              neuros.hostpath_to_neurospath(targetname))
+              metadata["size"] // 1024, destination)
     # Add entry to database
     neuros.db["audio"].add_record(record)
     
+    try:
+        filename = path.join(*neuros.mountpoint_parts +
+                             [neuros.DB_DIR, 'tracks.txt'])
+        tracknum_file = file(filename, 'a')
+        tracknum = metadata.get('tracknumber', None)
+        if tracknum is None:
+            tracknum = metadata.get('title', targetname)
+            try:
+                tracknum.lower()
+            except AttributeError:
+                pass
+        tracknum_file.write('%s\t%s\n' % (destination, tracknum))
+        tracknum_file.close()
+    except IOError:
+        pass  # Fail silently if we can't write the file.

<p><p>1.5       +5 -3      positron/positron/audiofile.py

Index: audiofile.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/audiofile.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- audiofile.py	10 Jun 2003 21:58:10 -0000	1.4
+++ audiofile.py	6 Sep 2003 23:02:55 -0000	1.5
@@ -50,7 +50,8 @@
                  "title" : mp3info.title,
                  "artist" : mp3info.artist,
                  "album" : mp3info.album,
-                 "genre" : mp3info.genre }
+                 "genre" : mp3info.genre,
+                 "tracknumber" : mp3info.track}
 
         # Convert empty string entries to nulls
         for key in info.keys():
@@ -81,11 +82,12 @@
                  "title" : None,
                  "artist" : None,
                  "album" : None,
-                 "genre" : None }
+                 "genre" : None,
+                 "tracknumber" : None}
 
         actual_keys = map(string.lower, vc.keys())
         
-        for tag in ("title","artist","album","genre"):
+        for tag in ("title","artist","album","genre","tracknumber"):
             if tag in actual_keys:
                 value = vc[tag]
                 # Force these to be single valued

<p><p>1.5       +2 -1      positron/positron/cmd_add.py

Index: cmd_add.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/cmd_add.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- cmd_add.py	12 Jun 2003 20:42:14 -0000	1.4
+++ cmd_add.py	6 Sep 2003 23:02:55 -0000	1.5
@@ -103,7 +103,8 @@
 
     if config.sort_database:
         print "\nSorting tracks..."
-        audio_db.sort()
+        audio_db.sort(path.join(*neuros.mountpoint_parts +
+                                [neuros.DB_DIR, 'tracks.txt']))
         
     print "Done!"
     neuros.close_db("audio")

<p><p>1.2       +8 -0      positron/positron/cmd_clear.py

Index: cmd_clear.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/cmd_clear.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- cmd_clear.py	22 May 2003 04:42:52 -0000	1.1
+++ cmd_clear.py	6 Sep 2003 23:02:55 -0000	1.2
@@ -32,6 +32,8 @@
 import neuros as neuros_module
 import util
 
+import os
+
 def run(config, neuros, args):
     if len(args) == 0:
         args = ["audio", "unidedhisi", "idedhisi", "failedhisi"]
@@ -45,3 +47,9 @@
         except neuros_module.Error, e:
             print "Error:", e
     
+    try:
+        filename = os.path.join(*neuros.mountpoint_parts +
+                                [neuros.DB_DIR, 'tracks.txt'])
+        os.remove(filename)
+    except OSError:
+        pass

<p><p>1.4       +2 -1      positron/positron/cmd_pack.py

Index: cmd_pack.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/cmd_pack.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- cmd_pack.py	19 Jun 2003 17:57:55 -0000	1.3
+++ cmd_pack.py	6 Sep 2003 23:02:55 -0000	1.4
@@ -42,7 +42,8 @@
             database.pack()
 
             if config.sort_database:
-                database.sort()
+                database.sort(path.join(*neuros.mountpoint_parts +
+                                        [neuros.DB_DIR, 'tracks.txt']))
                 
             neuros.close_db(arg)
         except neuros_module.Error, e:

<p><p>1.5       +12 -2     positron/positron/cmd_rebuild.py

Index: cmd_rebuild.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/cmd_rebuild.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- cmd_rebuild.py	9 Jun 2003 01:04:46 -0000	1.4
+++ cmd_rebuild.py	6 Sep 2003 23:02:55 -0000	1.5
@@ -83,6 +83,14 @@
             print "  "+d
             neuros.new_db(d)
 
+        print "Clearing track number cache..."
+
+        try:
+            os.remove(path.join(*neuros.mountpoint_parts +
+                                [neuros.DB_DIR, 'tracks.txt']))
+        except Exception:
+            pass    # Silently fail
+        
         print "\nFinding existing audio files on the Neuros..."
         # Now we need to find all the files to readd them to the database.
         filelist = [item[1:] for item in
@@ -110,7 +118,8 @@
             add_track(neuros, None, track, metadata,
                       recording=recording_source(track))
         if config.sort_database:
-            audio_db.sort()
+            audio_db.sort(path.join(*neuros.mountpoint_parts +
+                                    [neuros.DB_DIR, 'tracks.txt']))
         neuros.close_db("audio")
 
         print "\nAdding HiSi clips to unidedhisi database..."
@@ -122,7 +131,8 @@
                       neuros.hostpath_to_neurospath(track))
             unidedhisi_db.add_record(record)
         if config.sort_database:
-            unidedhisi_db.sort()
+            unidedhisi_db.sort(path.join(*neuros.mountpoint_parts +
+                                         [neuros.DB_DIR, 'tracks.txt']))
         neuros.close_db("unidedhisi")
         
     except neuros_module.Error, e:

<p><p>1.6       +2 -1      positron/positron/cmd_sync.py

Index: cmd_sync.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/cmd_sync.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- cmd_sync.py	19 Jun 2003 17:57:55 -0000	1.5
+++ cmd_sync.py	6 Sep 2003 23:02:55 -0000	1.6
@@ -168,7 +168,8 @@
         print " Done."
 
     if config.sort_database:
-        audio_db.sort()
+        audio_db.sort(path.join(*neuros.mountpoint_parts +
+                                [neuros.DB_DIR, 'tracks.txt']))
         
     neuros.close_db("audio")
     

<p><p>1.2       +56 -0     positron/positron/db/PAI.py

Index: PAI.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/db/PAI.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PAI.py	22 May 2003 04:42:52 -0000	1.1
+++ PAI.py	6 Sep 2003 23:02:55 -0000	1.2
@@ -3,6 +3,7 @@
 # db/PAI.py - PAI functions
 #
 # Copyright (C) 2003, Xiph.org Foundation
+# Copyright (C) 2003 Brett Smith <bretts at canonical.org>
 #
 # This file is part of positron.
 #
@@ -17,6 +18,44 @@
 import struct
 from util import *
 
+def pai_get_tracknums(filename):
+    tracknums = {}
+    try:
+        inputfile = file(filename)
+        while 1:
+            data = inputfile.readline()
+            if not data:
+                break
+            data = data.split('\t', 1)
+            tracknums[data[0]] = data[1]
+        inputfile.close()
+    except IOError:
+        pass
+    return tracknums
+
+
+def pai_sort_by_title(root_mdb, mdb_ptr):
+    return root_mdb.read_record_at(mdb_ptr)[0]['data'][0].lower()
+
+
+def pai_sort_by_filename(root_mdb, mdb_ptr):
+    return root_mdb.read_record_at(mdb_ptr)[0]['extra'][2][0].lower()
+
+
+def pai_sort_by_tracknum(root_mdb, tracknums, mdb_ptr):
+    filename = root_mdb.read_record_at(mdb_ptr)[0]['extra'][2][0]
+    tracknum = tracknums.get(filename, '')
+    try:
+        return int(tracknum, 10)
+    except (TypeError, ValueError):
+        return tracknum
+
+    
+pai_sorters = {'Album': pai_sort_by_tracknum,
+               'Artist': pai_sort_by_title,
+               'Genre': pai_sort_by_title
+               }
+
 class PAI:
 
     SIGNATURE = 0x01162002
@@ -303,3 +342,20 @@
         self.file.close()
         self.__init__()
 
+    def sort(self, sort_func, sai):
+        sai_index = 0
+        while 1:
+            sai_index = sai_index + 1
+            try:
+                pai_ptr = sai[sai_index][1]
+            except IndexError:
+                break
+            mdb_ptr_list = [ptr[0] for ptr in self.read_module_at(pai_ptr)[0]]
+            entries = []
+            for mdb_ptr in mdb_ptr_list:
+                data = sort_func(mdb_ptr)
+                entries.append((data, mdb_ptr))
+            entries.sort()
+            self.clear_module_at(pai_ptr)
+            for entry in entries:
+                self.add_entry_to_module_at(pai_ptr, entry[1])

<p><p>1.4       +26 -3     positron/positron/db/WOID.py

Index: WOID.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/db/WOID.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WOID.py	25 May 2003 23:22:34 -0000	1.3
+++ WOID.py	6 Sep 2003 23:02:55 -0000	1.4
@@ -3,6 +3,7 @@
 # db/WOID.py - WOID functions
 #
 # Copyright (C) 2003, Xiph.org Foundation
+# Copyright (C) 2003 Brett Smith <bretts at canonical.org>
 #
 # This file is part of positron.
 #
@@ -19,7 +20,7 @@
 from os import path
 from MDB import MDB
 from SAI import SAI,cmp_sai_record
-from PAI import PAI
+from PAI import PAI,pai_sorters,pai_get_tracknums
 from XIM import XIM
 
 def _mangle_field(field):
@@ -346,14 +347,36 @@
         for record in records:
             self.add_record(record)
 
-    def sort(self):
+    def sort(self, tracklist_filename, root_mdb = None):
         """Sorts the contents of this database and all child databases."""
 
+        if root_mdb is None:
+            root_mdb = self.mdb
+
         cmpfunc = lambda a,b: cmp_sai_record(self.mdb, a, b)
         self.sai.sort(cmpfunc)
+        name_index = self.name.find('\x00')
+        if name_index != -1:
+            name = self.name[:name_index]
+        else:
+            name = self.name
+        if name.endswith('s'):
+            name = name[:-1]
+        if self.pai is not None:
+            base_sort_func = pai_sorters.get(name, None)
+            if name == 'Album':
+                tracknums = pai_get_tracknums(tracklist_filename)
+                sort_func = lambda ptr: base_sort_func(root_mdb, tracknums,
+                                                       ptr)
+            elif base_sort_func is not None:
+                sort_func = lambda ptr: base_sort_func(root_mdb, ptr)
+            else:
+                sort_func = None
+            if sort_func is not None:
+                self.pai.sort(sort_func, self.sai)
 
         for child in self.children:
-            child.sort()
+            child.sort(tracklist_filename, root_mdb)
 
     def close(self):
         self.mdb.close()

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list