[sword-svn] r398 - trunk/modules/python

chrislit at crosswire.org chrislit at crosswire.org
Sun Aug 26 15:40:55 MST 2012


Author: chrislit
Date: 2012-08-26 15:40:55 -0700 (Sun, 26 Aug 2012)
New Revision: 398

Modified:
   trunk/modules/python/usfm2osis.py
Log:
added unsorted & random sort options


Modified: trunk/modules/python/usfm2osis.py
===================================================================
--- trunk/modules/python/usfm2osis.py	2012-08-26 22:25:30 UTC (rev 397)
+++ trunk/modules/python/usfm2osis.py	2012-08-26 22:40:55 UTC (rev 398)
@@ -50,7 +50,6 @@
 # 1.x SWORD module output?, requiring SWORD bindings
 
 ### TODO for next milestone:
-# file sorting options (natural/alphabetic/canonical/none)
 # expand sub-verses with ! in osisIDs
 # Python3 compatability
 # document functions (docstrings)
@@ -90,6 +89,7 @@
 import sys, codecs, re
 from encodings.aliases import aliases
 import multiprocessing, Queue
+import random
 
 date = date.replace('$', '').strip()[6:16]
 rev = rev.replace('$', '').strip()[5:]
@@ -325,6 +325,12 @@
 def keyusfm(filename):
     return usfmNumericOrder.index(filename2osis[filename])
 
+def keysupplied(filename):
+    if not hasattr(keysupplied, "counter"):
+       keysupplied.counter = 0  # it doesn't exist yet, so initialize it
+    keysupplied.counter += 1
+    return keysupplied.counter
+
 def convertToOsis(sFile):
     global encoding
     global relaxedConformance
@@ -1207,7 +1213,8 @@
     print('  -h, --help       print this usage information')
     print('  -o FILENAME      output filename (default is: <osisWork>.osis.xml)')
     print('  -r               enable relaxed markup processing (for non-standard USFM)')
-    print('  -s mode          set book sorting mode: natural (default), alpha, canonical, usfm, none')
+    print('  -s mode          set book sorting mode: natural (default), alpha, canonical,')
+    print('                     usfm, random, none')
     print('  -v               verbose feedback')
     print('  -x               disable XML validation')
     print('')
@@ -1322,15 +1329,17 @@
             elif sys.argv[i].startswith('u'):
                 sortKey = keyusfm
                 print('Sorting book files by USFM book number.')
+            elif sys.argv[i].startswith('random'): # for testing only
+                sortKey = lambda filename: int(random.random()*256)
+                print('Sorting book files randomly.')
             else:
-                sortKey = None # TODO: write appropriate helpers
-                print('Leaving book files unsorted.')
+                sortKey = keysupplied
+                print('Leaving book files unsorted, in the order in which they were supplied.')
             inputFilesIdx += 2 # increment 2, reflecting 2 args for -s
         else:
             sortKey = keynat
             print('Sorting book files naturally.')
 
-
         usfmDocList = sys.argv[inputFilesIdx:]
 
         for filename in usfmDocList:




More information about the sword-cvs mailing list