changeset 8:ce07bb2a247b

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 May 2021 02:08:30 +0300
parents 4b4299b62f7f
children 2a8c65d22f86
files lxmldump.py
diffstat 1 files changed, 33 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lxmldump.py	Tue May 11 01:26:57 2021 +0300
+++ b/lxmldump.py	Tue May 11 02:08:30 2021 +0300
@@ -21,6 +21,9 @@
 ### Default settings
 ###
 pkk_cfg = {
+    "verbosity": 3,
+
+    "annotate": False,
     "dump": False,
     "normalize": False,
 
@@ -60,6 +63,10 @@
     pkk_print(("    " * indent) + smsg)
 
 
+def pkk_verbosity(lvl):
+    return pkk_cfg["verbosity"] >= lvl
+
+
 ## Fatal error handler
 def pkk_fatal(smsg):
     print(u"ERROR: "+ smsg)
@@ -77,7 +84,7 @@
     stmp = ""
     for pnode in lnode.iter():
         if isinstance(pnode.text, str):
-            if isinstance(pnode.tag, str) and pnode.tag in pkk_str_fmap:
+            if pkk_cfg["annotate"] and isinstance(pnode.tag, str) and pnode.tag in pkk_str_fmap:
                 stmp += pkk_str_fmap[pnode.tag][0] + pnode.text + pkk_str_fmap[pnode.tag][1]
             else:
                 stmp += pnode.text
@@ -127,14 +134,15 @@
 
     for wnode in dnode.findall("./ExampleBlock/ExampleCtn"):
         sstr = pkk_get_text(wnode.find("./Example"))
-        ltmp = []
-        for qnode in wnode.findall("./FreeTopic[@type='levikki']/GeographicalUsage"):
-            ltmp.append("{} [{}]".format(pkk_get_text(qnode), qnode.attrib["class"]))
+        lstr = ""
 
-        if len(ltmp) > 0:
-            lstr = " ({})".format(", ".join(ltmp))
-        else:
-            lstr = ""
+        if pkk_verbosity(1):
+            ltmp = []
+            for qnode in wnode.findall("./FreeTopic[@type='levikki']/GeographicalUsage"):
+                ltmp.append("{} [{}]".format(pkk_get_text(qnode), qnode.attrib["class"]))
+
+            if len(ltmp) > 0:
+                lstr = " ({})".format(", ".join(ltmp))
 
         pkk_printi(indent + 1, "{} \"{}\"{}\n".format("exmp", sstr, lstr))
 
@@ -147,7 +155,7 @@
 
         index = 1
         for wnode in dnode.findall("./SenseGrp"):
-            pkk_printi(indent + 2, "sense #{}\n".format(index))
+            pkk_printi(indent + 1, "sense #{}\n".format(index))
             pkk_output_sense(indent + 2, wnode)
             index += 1
 
@@ -182,8 +190,13 @@
             pkk_cfg["dump"] = True
         elif arg == "normalize" or arg == "n":
             pkk_cfg["normalize"] = True
+        elif arg == "annotate" or arg == "a":
+            pkk_cfg["annotate"] = True
         elif arg == "p":
             pkk_cfg["debug"] = True
+        elif arg == "verbosity" or arg == "v":
+            needs_param = True
+            pkk_cfg["verbosity"] = param
         else:
             pkk_fatal(u"Invalid option argument '{0}'.".format(oarg))
 
@@ -208,10 +221,21 @@
     print(u"       --help              Show this help")
     print(u"  -d,  --dump              Dump mode")
     print(u"  -n,  --normalize         Output NFC normalized Unicode")
+    print(u"  -a,  --annotate          Annotate strings")
+    print(u"  -v,  --verbosity <n>     Set verbosity level (0 - 3)")
     print(u"")
     sys.exit(0)
 
 
+### Validate settings
+try:
+    pkk_cfg["verbosity"] = int(pkk_cfg["verbosity"])
+except Exception as e:
+    pkk_fatal(u"Verbosity level is not a valid integer.")
+if pkk_cfg["verbosity"] < 0 or pkk_cfg["verbosity"] > 3:
+    pkk_fatal(u"Invalid verbosity level value {0}.".format(pkk_cfg["verbosity"]))
+
+
 ### Handle each input file
 for filename in pkk_filenames:
     # Parse XML file into element tree