[sword-svn] r523 - trunk/versification

greg.hellings at crosswire.org greg.hellings at crosswire.org
Wed Apr 4 19:29:41 MST 2018


Author: greg.hellings
Date: 2018-04-04 19:29:41 -0700 (Wed, 04 Apr 2018)
New Revision: 523

Modified:
   trunk/versification/av11n.py
Log:
Don?\226?\128?\153t fool with PyQuery and use standard XML libraries.

Modified: trunk/versification/av11n.py
===================================================================
--- trunk/versification/av11n.py	2018-04-05 02:29:34 UTC (rev 522)
+++ trunk/versification/av11n.py	2018-04-05 02:29:41 UTC (rev 523)
@@ -9,6 +9,7 @@
 # special as for ordering.
 #
 # Invoke simply by calling the program and the file name.
+import io
 import logging
 # in normal state level should be debug.WARNING, debug.INFO and debug.DEBUG
 # give additional information.
@@ -16,8 +17,13 @@
                     level=logging.WARNING)
 import re
 import sys
-verseid = re.compile(r'^.+\..+\..+$')
+try:
+    import lxml.etree as ET
+except ImportError:
+    import xml.etree.ElementTree as ET
 
+VERSEID_RE = re.compile(r'^.+\..+\..+$')
+
 # Inform the user that we need the SWORD extension
 try:
     import Sword
@@ -26,15 +32,6 @@
         "You do not have the SWORD library installed. Please install it.")
     sys.exit(1)
 
-# Inform the user that we need pyquery, as it makes parsing XML files
-# that much easier
-try:
-    from pyquery import PyQuery as pq  # noqa
-except ImportError:
-    logging.exception(
-        "You do not appear to have PyQuery installed. Please install it.")
-    sys.exit(2)
-
 # Without the name of a file, we cannot proceed any further
 if len(sys.argv) < 2 or sys.argv[1] == '--help':
     print >>sys.stderr, "Usage: %s <OSISfile>" % sys.argv[0]
@@ -42,7 +39,8 @@
 
 # Open the file
 logging.debug('Opening %s' % (sys.argv[1],))
-d = pq(filename=sys.argv[1])
+
+tree = ET.parse(io.open(sys.argv[1], encoding='utf8')).getroot()
 # Get the list of versifications
 logging.debug('Fetching a list of v11ns')
 vmgr = Sword.VersificationMgr.getSystemVersificationMgr()
@@ -50,7 +48,11 @@
 
 # Get the list of all osisIDs
 logging.debug('Fetching a list of OSIS IDs')
-ids = d("*[osisID]")
+ids = set()
+for item in tree.iter():
+    if 'osisID' in item.attrib:
+        ids.add(item.attrib['osisID'])
+
 # Iterate each versification scheme
 for v11n in av11ns:
     print('Checking %s' % v11n.c_str())
@@ -82,20 +84,18 @@
 
     inNT = False
     # Now iterate the ones we have in this file
-    for e in ids:
-        logging.debug('e = %s', e)
-        osisid = e.attrib.get('osisID')
-        #print 'Checking key %s' % (osisid,)
+    for osisid in ids:
+        logging.debug('Checking key %s', osisid)
         if osisid in otkeyList:
             otkeyList.remove(osisid)
         elif osisid in ntkeyList:
             ntkeyList.remove(osisid)
             inNT = True
-        elif verseid.match(osisid) and inNT:
+        elif VERSEID_RE.match(osisid) and inNT:
             ntextraKeys.append(osisid)
-        elif verseid.match(osisid) and not inNT:
+        elif VERSEID_RE.match(osisid) and not inNT:
             otextraKeys.append(osisid)
-        # Ignore it if not verseid.match()
+        # Ignore it if not VERSEID_RE.match()
 
     # Now let's see what is left over
     # Sets in Python cannot be ordered




More information about the sword-cvs mailing list