[sword-devel] [PATCH 7/7] Use argparser.

Matěj Cepl mcepl at cepl.eu
Sat Mar 31 11:49:04 MST 2018


---
 versification/av11n.py | 77 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 27 deletions(-)

diff --git a/versification/av11n.py b/versification/av11n.py
index 198eeb4..88f8402 100755
--- a/versification/av11n.py
+++ b/versification/av11n.py
@@ -9,12 +9,15 @@
 # special as for ordering.
 #
 # Invoke simply by calling the program and the file name.
+import argparse
 import io
 import logging
 # in normal state level should be debug.WARNING, debug.INFO and debug.DEBUG
 # give additional information.
-logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
-                    level=logging.WARNING)
+logging.basicConfig(format='%(levelname)s:%(message)s',
+                    level=logging.INFO)
+log = logging.getLogger('versification')
+
 import re
 import sys
 try:
@@ -22,40 +25,54 @@ try:
 except ImportError:
     import xml.etree.ElementTree as ET
 
-VERSEID_RE = re.compile(r'^.+\..+\..+$')
+OSIS_NS = 'http://www.bibletechnologies.net/2003/OSIS/namespace'
+VERSEID_RE = re.compile(r'^(.+\.\d+\.\d+).*$')
 
 # Inform the user that we need the SWORD extension
 try:
     import Sword
 except ImportError:
-    logging.exception(
+    log.exception(
         "You do not have the SWORD library installed. Please install it.")
     sys.exit(1)
 
-# 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]
-    sys.exit(1)
+arg_parser = argparse.ArgumentParser(
+    description='Compare OSIS file with available v11ns.')
+
+arg_parser.add_argument('--verbose', '-v', action='count')
+arg_parser.add_argument('filename', nargs=1)
+
+
+args = arg_parser.parse_args()
+
+if args.verbose:
+    log.setLevel = logging.DEBUG
+
+log.debug('args = %s', args)
 
 # Open the file
-logging.debug('Opening %s' % (sys.argv[1],))
+log.debug('Opening %s' % args.filename[0])
 
-tree = ET.parse(io.open(sys.argv[1], encoding='utf8')).getroot()
+tree = ET.parse(io.open(args.filename[0], encoding='utf8')).getroot()
 # Get the list of versifications
-logging.debug('Fetching a list of v11ns')
+log.debug('Fetching a list of v11ns')
 vmgr = Sword.VersificationMgr.getSystemVersificationMgr()
 av11ns = vmgr.getVersificationSystems()
+log.debug('av11ns = %s', av11ns)
 
 # Get the list of all osisIDs
-logging.debug('Fetching a list of OSIS IDs')
+log.debug('Fetching a list of OSIS IDs')
 ids = set()
-for item in tree.iter():
+for item in tree.iter('{%s}verse' % OSIS_NS):
     if 'osisID' in item.attrib:
-        ids.add(item.attrib['osisID'])
+        ids.add(item.attrib['osisID'].split('!')[0])
+log.debug('ids = len(%d)', len(ids))
 
 # Iterate each versification scheme
 for v11n in av11ns:
-    print('Checking %s' % v11n.c_str())
+    v11n_name = v11n.c_str()
+    print('\nChecking %s:\n%s' %
+          (v11n_name, (len(v11n_name) + 10) * '-'))
     # Construct a list of the IDs in this versification
     key = Sword.VerseKey()
     key.setVersificationSystem(v11n.c_str())
@@ -85,36 +102,42 @@ for v11n in av11ns:
     inNT = False
     # Now iterate the ones we have in this file
     for osisid in ids:
-        logging.debug('Checking key %s', osisid)
+#        log.debug('Checking key %s', osisid)
         if osisid in otkeyList:
             otkeyList.remove(osisid)
         elif osisid in ntkeyList:
             ntkeyList.remove(osisid)
             inNT = True
-        elif VERSEID_RE.match(osisid) and inNT:
-            ntextraKeys.append(osisid)
-        elif VERSEID_RE.match(osisid) and not inNT:
-            otextraKeys.append(osisid)
-        # Ignore it if not VERSEID_RE.match()
+        else:
+            verse_match = VERSEID_RE.match(osisid)
+            if verse_match and inNT:
+                ntextraKeys.append(verse_match.group(1))
+            elif verse_match and not inNT:
+                otextraKeys.append(verse_match.group(1))
+            # Ignore it if not VERSEID_RE.match()
 
     # Now let's see what is left over
     # Sets in Python cannot be ordered
     keyList = list(otkeyList.union(ntkeyList))
     keyList.sort()
     if len(keyList) > 0:
-        logging.info('\tThe following IDs don’t appear in your file:\n%s',
-                     str("\n".join(keyList)))
+        if len(keyList) < 100:
+            log.info('\tThe following IDs don’t appear in your file:\n%s',
+                         str(", ".join(keyList)))
         print ('\tThere are %d OT IDs and %d NT IDs ' +
-               'in v11n which arn’t in your file.') \
+               'in v11n which aren’t in your file.') \
             % (len(otkeyList), len(ntkeyList))
     else:
         print '\tYour file has all the references in this v11n'
 
     # Now let's see if you had extra
     if len(otextraKeys + ntextraKeys) > 0:
-        logging.info(
-            '\tThe following IDs don’t appear in v11n:\n%s',
-            str("\n".join(keyList)))
+        # It doesn't make sense to print out lists longer than 100
+        # they cannot be read anyway
+        if len(keyList) < 100:
+            log.info(
+                '\tThe following IDs don’t appear in v11n:\n%s',
+                str(", ".join(keyList)))
         print ('\tThere are %d OT IDs and %d NT IDs ' +
                'in your file which don’t appear in v11n.') \
             % (len(otextraKeys), len(ntextraKeys))
-- 
2.16.2




More information about the sword-devel mailing list