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

Stan Seibert volsung at xiph.org
Sun May 25 15:28:00 PDT 2003



volsung     03/05/25 18:28:00

  Modified:    positron/db SAI.py WOID.py
  Log:
  Several DB updates:
  * Sorting of the database by reordering the SAI entries.  (Seems to work
  since the Neuros displays records in the order they are shown in the SAI
  table.)  This is handy because it can be done in-place.
  * Deleting the null record is not allowed, and now we don't do it.  :)
  * Extended ASCII can't be displayed on the Neuros, so we convert it to
    underscores.  Deals with part of bug #355.

Revision  Changes    Path
1.2       +25 -0     positron/positron/db/SAI.py

Index: SAI.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/db/SAI.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SAI.py	22 May 2003 04:42:52 -0000	1.1
+++ SAI.py	25 May 2003 22:28:00 -0000	1.2
@@ -18,6 +18,19 @@
 import types
 from util import *
 
+def cmp_sai_record(mdb, sai_record_a, sai_record_b):
+    (a,dummy) = mdb.read_record_at(sai_record_a[0])
+    (b,dummy) = mdb.read_record_at(sai_record_b[0])
+    
+    if a == None and b == None:
+        return 0
+    elif a == None:
+        return -1
+    elif b == None:
+        return 1
+    else:
+        return cmp(a["data"][0].lower(), b["data"][0].lower())
+
 class SAI:
     """Read and write a SAI file as if it were a list.
 
@@ -142,6 +155,18 @@
             if self[i][1] > threshold:
                 record[1] += offset
                 self[i] = record
+
+    def sort(self, cmpfunc):
+        """Sort the SAI entries based upon cmpfunc (as used in list.sort())."""
+
+        # Note we gotta do this the slighly long way because we're not
+        # exactly a python list.
+        mylist = [self[i] for i in range(len(self))]
+        
+        mylist.sort(cmpfunc)
+
+        for i in range(len(self)):
+            self[i] = mylist[i]
 
     def clear(self):
         self.file.truncate(SAI.DATA_START+8)

<p><p>1.2       +49 -1     positron/positron/db/WOID.py

Index: WOID.py
===================================================================
RCS file: /usr/local/cvsroot/positron/positron/db/WOID.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WOID.py	22 May 2003 04:42:52 -0000	1.1
+++ WOID.py	25 May 2003 22:28:00 -0000	1.2
@@ -14,13 +14,42 @@
 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
 # or FITNESS FOR A PARTICULAR PURPOSE.  See the license for more details.
 
+from types import *
 from util import *
 from os import path
 from MDB import MDB
-from SAI import SAI
+from SAI import SAI,cmp_sai_record
 from PAI import PAI
 from XIM import XIM
 
+def _mangle_field(field):
+    """Replaces extended ASCII characters in a field with underscores.
+
+    These characters cannot be displayed on the Neuros.
+    Non-text fields are passed-through unchanged."""
+
+    if type(field) is ListType or type(field) is TupleType:
+        return map(_mangle_field, field)  # Recursively handle list elements
+    elif type(field) is StringType or type(field) is UnicodeType:
+        # This is a singlet field
+        new_string = ""
+        for old_char in field:
+            if ord(old_char) >= 0x7F:
+                new_string += "_"
+            else:
+                new_string += old_char
+
+        return new_string
+    else:
+        return field
+
+def _mangle_record(record):
+    """Replaces extended ASCII characters in a record with underscores.
+
+    See _mangle_field()."""
+
+    return map(_mangle_field, record)
+
 class WOID:
 
     def __init__(self):
@@ -128,6 +157,10 @@
         """Returns the SAI index number for the first non-deleted record
         containing data in field number \"check_field\"."""
 
+        # Since records have been mangled, we should transform the data
+        # similarly
+        data = _mangle_field(data)
+
         for i in range(len(self.sai)):
             (mdb_pointer, sai_pointer) = self.sai[i]
 
@@ -170,6 +203,9 @@
         record and the second element is a word pointer to the PAI
         module corresponding to new record."""
 
+        # Eliminate bad characters
+        record = _mangle_record(record)
+
         # Put single objects into lists
         record = map(unflatten_singlet, record)
         record = map(uncollapse_null_list, record)
@@ -242,6 +278,9 @@
         (mdb_pointer, pai_pointer) = self.sai[sai_index]
 
         (record, next) = self.mdb.read_record_at(mdb_pointer)
+        if record == None:
+            return # DON'T DELETE THE NULL RECORD
+        
         self.mdb.delete_record_at(mdb_pointer)
         self.sai.delete(sai_index)
         if self.pai != None:
@@ -302,6 +341,15 @@
             
         for record in records:
             self.add_record(record)
+
+    def sort(self):
+        """Sorts the contents of this database and all child databases."""
+
+        cmpfunc = lambda a,b: cmp_sai_record(self.mdb, a, b)
+        self.sai.sort(cmpfunc)
+
+        for child in self.children:
+            child.sort()
 
     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