[sword-devel] [PATCH 3/7] Switch to logging to make it a little bit more civilized.

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


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

diff --git a/versification/av11n.py b/versification/av11n.py
index 7dabfee..54347a8 100755
--- a/versification/av11n.py
+++ b/versification/av11n.py
@@ -7,10 +7,12 @@
 # in the proper order, although within each testament, it requires nothing
 # special as for ordering.
 #
-# Invoke simply by calling the program and the file name.  If you want
-# more output, change the following line to be True instead of False
-verbose = False
-debug = True
+# Invoke simply by calling the program and the file name.
+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)
 import re
 import sys
 verseid = re.compile(r'^.+\..+\..+$')
@@ -19,7 +21,8 @@ verseid = re.compile(r'^.+\..+\..+$')
 try:
     import Sword
 except ImportError:
-    print "You do not have the SWORD library installed. Please install it."
+    logging.exception(
+        "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
@@ -27,30 +30,29 @@ except ImportError:
 try:
     from pyquery import PyQuery as pq  # noqa
 except ImportError:
-    print "You do not appear to have PyQuery installed. Please install it."
+    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 "Usage: %s <OSISfile>" % (sys.argv[0],)
+    print >>sys.stderr, "Usage: %s <OSISfile>" % sys.argv[0]
+    sys.exit(1)
 
 # Open the file
-if debug:
-    print 'Opening %s' % (sys.argv[1],)
+logging.debug('Opening %s' % (sys.argv[1],))
 d = pq(filename=sys.argv[1])
 # Get the list of versifications
-if debug:
-    print 'Fetching a list of versifications'
+logging.debug('Fetching a list of versifications')
 vmgr = Sword.VersificationMgr.getSystemVersificationMgr()
 av11ns = vmgr.getVersificationSystems()
 
 # Get the list of all osisIDs
-if debug:
-    print 'Fetching a list of OSIS IDs'
+logging.debug('Fetching a list of OSIS IDs')
 ids = d("*[osisID]")
 # Iterate each versification scheme
 for v11n in av11ns:
-    print 'Checking %s' % (v11n.c_str(),)
+    print('Checking %s' % v11n.c_str())
     # Construct a list of the IDs in this versification
     key = Sword.VerseKey()
     key.setVersificationSystem(v11n.c_str())
@@ -98,26 +100,21 @@ for v11n in av11ns:
     keyList = list(otkeyList.union(ntkeyList))
     keyList.sort()
     if len(keyList) > 0:
-        if verbose:
-            print '\tThe following IDs do not appear in your file:'
-            for k in keyList:
-                print k
-        else:
-            print ('\tThere are %d OT IDs and %d NT IDs ' +
-                   'in the versification which are not in your file.') \
-                % (len(otkeyList), len(ntkeyList))
+        logging.info('\tThe following IDs do not appear in your file:\n%s',
+                     str("\n".join(keyList)))
+        print ('\tThere are %d OT IDs and %d NT IDs ' +
+               'in the versification which are not in your file.') \
+            % (len(otkeyList), len(ntkeyList))
     else:
         print '\tYour file has all the references in this versification'
 
     # Now let's see if you had extra
     if len(otextraKeys + ntextraKeys) > 0:
-        if verbose:
-            print '\tThe following IDs do not appear in the versification:'
-            for k in ntextraKeys + otextraKeys:
-                print k
-        else:
-            print ('\tThere are %d OT IDs and %d NT IDs ' +
-                   'in your file which do not appear in the versification.') \
-                % (len(otextraKeys), len(ntextraKeys))
+        logging.info(
+            '\tThe following IDs do not appear in the versification:\n%s',
+            str("\n".join(keyList)))
+        print ('\tThere are %d OT IDs and %d NT IDs ' +
+               'in your file which do not appear in the versification.') \
+            % (len(otextraKeys), len(ntextraKeys))
     else:
         print '\tYour file has no extra references'
-- 
2.16.2




More information about the sword-devel mailing list