[Pkg-isocodes-devel] [iso-codes] 05/07: Adapt script to create XML files

Tobias Quathamer toddy at moszumanska.debian.org
Fri Mar 4 13:07:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

toddy pushed a commit to branch master
in repository iso-codes.

commit 36e4039497a833fb034d3132268c774b43b43a94
Author: Dr. Tobias Quathamer <toddy at debian.org>
Date:   Fri Mar 4 13:44:19 2016 +0100

    Adapt script to create XML files
---
 bin/xml_from_json.py | 310 ++++++++++++++++++++++++++-------------------------
 common.mk            |   4 +
 2 files changed, 161 insertions(+), 153 deletions(-)

diff --git a/bin/xml_from_json.py b/bin/xml_from_json.py
index 071ea8c..07a1910 100755
--- a/bin/xml_from_json.py
+++ b/bin/xml_from_json.py
@@ -19,6 +19,14 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 import json
+import sys
+
+# Get the current ISO code domain, the path to the JSON data dir, and the XML output file
+if len(sys.argv) != 4:
+    sys.exit("Please provide the domain, the path to the JSON data dir, and the XML output file.")
+domain = sys.argv[1]
+datapath = sys.argv[2]
+xml_file = sys.argv[3]
 
 #
 # Define the headers of the XML files
@@ -384,164 +392,160 @@ Source: <http://www.bsi-global.com/en/Standards-and-Publications/Industry-Sector
 def get_iso_entries(standard):
     """ Return all entries from the given standard
     """
-    with open("data/iso_" + standard + ".json") as input_file:
+    with open(datapath + "/iso_" + standard + ".json") as input_file:
         iso = json.load(input_file)
         return iso[standard]
 
-#
-# Handle ISO 639-2
-#
-with open("iso_639/iso_639.xml", "w") as outfile:
-    outfile.write(headers["639"])
-    outfile.write("\n")
-    outfile.write("<iso_639_entries>\n")
-    for entry in get_iso_entries("639-2"):
-        outfile.write("\t<iso_639_entry\n")
-        if "bibliographic" in entry:
-            outfile.write("\t\tiso_639_2B_code=\"" + entry["bibliographic"] + "\"\n")
-        else:
-            outfile.write("\t\tiso_639_2B_code=\"" + entry["alpha_3"] + "\"\n")
-        outfile.write("\t\tiso_639_2T_code=\"" + entry["alpha_3"] + "\"\n")
-        if "alpha_2" in entry:
-            outfile.write("\t\tiso_639_1_code=\"" + entry["alpha_2"] + "\"\n")
-        outfile.write("\t\tname=\"" + entry["name"] + "\"")
-        if "common_name" in entry:
-            outfile.write("\n\t\tcommon_name=\"" + entry["common_name"] + "\"")
-        outfile.write(" />\n")
-    outfile.write("</iso_639_entries>\n")
-
-#
-# Handle ISO 639-3
-#
-with open("iso_639_3/iso_639_3.xml", "w") as outfile:
-    outfile.write(headers["639-3"])
-    outfile.write("\n")
-    outfile.write("<iso_639_3_entries>\n")
-    for entry in get_iso_entries("639-3"):
-        outfile.write("\t<iso_639_3_entry\n")
-        outfile.write("\t\tid=\"" + entry["alpha_3"] + "\"\n")
-        if "alpha_2" in entry:
-            outfile.write("\t\tpart1_code=\"" + entry["alpha_2"] + "\"\n")
-        if "terminology" in entry:
-            outfile.write("\t\tpart2_code=\"" + entry["terminology"] + "\"\n")
-        # Special case for lcq, which is the only entry with status "Retired"
-        if entry["alpha_3"] == "lcq":
-            outfile.write("\t\tstatus=\"Retired\"\n")
-        else:
-            outfile.write("\t\tstatus=\"Active\"\n")
-        outfile.write("\t\tscope=\"" + entry["scope"] + "\"\n")
-        outfile.write("\t\ttype=\"" + entry["type"] + "\"\n")
-        if "inverted_name" in entry:
-            outfile.write("\t\tinverted_name=\"" + entry["inverted_name"] + "\"\n")
-        outfile.write("\t\treference_name=\"" + entry["name"] + "\"\n")
-        if "common_name" in entry:
-            outfile.write("\t\tcommon_name=\"" + entry["common_name"] + "\"\n")
-        if "inverted_name" in entry:
-            outfile.write("\t\tname=\"" + entry["inverted_name"] + "\"")
-        else:
+# Create the output file
+with open(xml_file, "w") as outfile:
+    #
+    # Handle ISO 639-2
+    #
+    if domain == "iso_639-2":
+        outfile.write(headers["639"])
+        outfile.write("\n")
+        outfile.write("<iso_639_entries>\n")
+        for entry in get_iso_entries("639-2"):
+            outfile.write("\t<iso_639_entry\n")
+            if "bibliographic" in entry:
+                outfile.write("\t\tiso_639_2B_code=\"" + entry["bibliographic"] + "\"\n")
+            else:
+                outfile.write("\t\tiso_639_2B_code=\"" + entry["alpha_3"] + "\"\n")
+            outfile.write("\t\tiso_639_2T_code=\"" + entry["alpha_3"] + "\"\n")
+            if "alpha_2" in entry:
+                outfile.write("\t\tiso_639_1_code=\"" + entry["alpha_2"] + "\"\n")
             outfile.write("\t\tname=\"" + entry["name"] + "\"")
-        outfile.write(" />\n")
-    outfile.write("</iso_639_3_entries>\n")
-
-#
-# Handle ISO 639-5
-#
-with open("iso_639_5/iso_639_5.xml", "w") as outfile:
-    outfile.write(headers["639-5"])
-    outfile.write("\n")
-    outfile.write("<iso_639_5_entries>\n")
-    for entry in get_iso_entries("639-5"):
-        outfile.write("\t<iso_639_5_entry\n")
-        outfile.write("\t\tid=\"" + entry["alpha_3"] + "\"\n")
-        outfile.write("\t\tname=\"" + entry["name"] + "\"")
-        outfile.write(" />\n")
-    outfile.write("</iso_639_5_entries>\n")
-
-#
-# Handle ISO 3166
-#
-with open("iso_3166/iso_3166.xml", "w") as outfile:
-    outfile.write(headers["3166"])
-    outfile.write("\n")
-    outfile.write("<iso_3166_entries>\n")
-    for entry in get_iso_entries("3166-1"):
-        outfile.write("\t<iso_3166_entry\n")
-        outfile.write("\t\talpha_2_code=\"" + entry["alpha_2"] + "\"\n")
-        outfile.write("\t\talpha_3_code=\"" + entry["alpha_3"] + "\"\n")
-        outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
-        if "common_name" in entry:
-            outfile.write("\t\tcommon_name=\"" + entry["common_name"] + "\"\n")
-        outfile.write("\t\tname=\"" + entry["name"] + "\"")
-        if "official_name" in entry:
-            outfile.write("\n\t\tofficial_name=\"" + entry["official_name"] + "\"")
-        outfile.write(" />\n")
-    for entry in get_iso_entries("3166-3"):
-        outfile.write("\t<iso_3166_3_entry\n")
-        outfile.write("\t\talpha_4_code=\"" + entry["alpha_4"] + "\"\n")
-        outfile.write("\t\talpha_3_code=\"" + entry["alpha_3"] + "\"\n")
-        if "numeric" in entry:
+            if "common_name" in entry:
+                outfile.write("\n\t\tcommon_name=\"" + entry["common_name"] + "\"")
+            outfile.write(" />\n")
+        outfile.write("</iso_639_entries>\n")
+    #
+    # Handle ISO 639-3
+    #
+    elif domain == "iso_639-3":
+        outfile.write(headers["639-3"])
+        outfile.write("\n")
+        outfile.write("<iso_639_3_entries>\n")
+        for entry in get_iso_entries("639-3"):
+            outfile.write("\t<iso_639_3_entry\n")
+            outfile.write("\t\tid=\"" + entry["alpha_3"] + "\"\n")
+            if "alpha_2" in entry:
+                outfile.write("\t\tpart1_code=\"" + entry["alpha_2"] + "\"\n")
+            if "terminology" in entry:
+                outfile.write("\t\tpart2_code=\"" + entry["terminology"] + "\"\n")
+            # Special case for lcq, which is the only entry with status "Retired"
+            if entry["alpha_3"] == "lcq":
+                outfile.write("\t\tstatus=\"Retired\"\n")
+            else:
+                outfile.write("\t\tstatus=\"Active\"\n")
+            outfile.write("\t\tscope=\"" + entry["scope"] + "\"\n")
+            outfile.write("\t\ttype=\"" + entry["type"] + "\"\n")
+            if "inverted_name" in entry:
+                outfile.write("\t\tinverted_name=\"" + entry["inverted_name"] + "\"\n")
+            outfile.write("\t\treference_name=\"" + entry["name"] + "\"\n")
+            if "common_name" in entry:
+                outfile.write("\t\tcommon_name=\"" + entry["common_name"] + "\"\n")
+            if "inverted_name" in entry:
+                outfile.write("\t\tname=\"" + entry["inverted_name"] + "\"")
+            else:
+                outfile.write("\t\tname=\"" + entry["name"] + "\"")
+            outfile.write(" />\n")
+        outfile.write("</iso_639_3_entries>\n")
+    #
+    # Handle ISO 639-5
+    #
+    elif domain == "iso_639-5":
+        outfile.write(headers["639-5"])
+        outfile.write("\n")
+        outfile.write("<iso_639_5_entries>\n")
+        for entry in get_iso_entries("639-5"):
+            outfile.write("\t<iso_639_5_entry\n")
+            outfile.write("\t\tid=\"" + entry["alpha_3"] + "\"\n")
+            outfile.write("\t\tname=\"" + entry["name"] + "\"")
+            outfile.write(" />\n")
+        outfile.write("</iso_639_5_entries>\n")
+    #
+    # Handle ISO 3166
+    #
+    elif domain == "iso_3166-1":
+        outfile.write(headers["3166"])
+        outfile.write("\n")
+        outfile.write("<iso_3166_entries>\n")
+        for entry in get_iso_entries("3166-1"):
+            outfile.write("\t<iso_3166_entry\n")
+            outfile.write("\t\talpha_2_code=\"" + entry["alpha_2"] + "\"\n")
+            outfile.write("\t\talpha_3_code=\"" + entry["alpha_3"] + "\"\n")
             outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
-        if "withdrawal_date" in entry:
-            outfile.write("\t\tdate_withdrawn=\"" + entry["withdrawal_date"] + "\"\n")
-        outfile.write("\t\tnames=\"" + entry["name"] + "\"")
-        if "comment" in entry:
-            outfile.write("\n\t\tcomment=\"" + entry["comment"] + "\"")
-        outfile.write(" />\n")
-    outfile.write("</iso_3166_entries>\n")
-
-#
-# Handle ISO 3166-2
-#
-with open("iso_3166_2/iso_3166_2.xml", "w") as outfile:
-    outfile.write(headers["3166-2"])
-    outfile.write("\n")
-    outfile.write("<iso_3166_2_entries>\n")
-    for country in get_iso_entries("3166-2"):
-        outfile.write("<iso_3166_country code=\"" + country["code"] + "\">\n")
-        for subset in country["subsets"]:
-            outfile.write("<iso_3166_subset type=\"" + subset["type"] + "\">\n")
-            for item in subset["items"]:
-                outfile.write("\t<iso_3166_2_entry\n")
-                outfile.write("\t\tcode=\"" + item["code"] + "\"\tname=\"" + item["name"] + "\"")
-                if "parent" in item:
-                    outfile.write("\tparent=\"" + item["parent"] + "\"")
-                outfile.write(" />\n")
-            outfile.write("</iso_3166_subset>\n")
-        outfile.write("</iso_3166_country>\n")
-    outfile.write("</iso_3166_2_entries>\n")
-
-#
-# Handle ISO 15924
-#
-with open("iso_15924/iso_15924.xml", "w") as outfile:
-    outfile.write(headers["15924"])
-    outfile.write("\n")
-    outfile.write("<iso_15924_entries>\n")
-    for entry in get_iso_entries("15924"):
-        outfile.write("\t<iso_15924_entry\n")
-        outfile.write("\t\talpha_4_code=\"" + entry["alpha_4"] + "\"\n")
-        outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
-        outfile.write("\t\tname=\"" + entry["name"] + "\"")
-        outfile.write(" />\n")
-    outfile.write("</iso_15924_entries>\n")
-
-#
-# Handle ISO 4217
-#
-with open("iso_4217/iso_4217.xml", "w") as outfile:
-    outfile.write(headers["4217"])
-    outfile.write("\n")
-    outfile.write("<iso_4217_entries>\n")
-    for entry in get_iso_entries("4217"):
-        outfile.write("\t<iso_4217_entry\n")
-        outfile.write("\t\tletter_code=\"" + entry["alpha_3"] + "\"\n")
-        if "numeric" in entry:
+            if "common_name" in entry:
+                outfile.write("\t\tcommon_name=\"" + entry["common_name"] + "\"\n")
+            outfile.write("\t\tname=\"" + entry["name"] + "\"")
+            if "official_name" in entry:
+                outfile.write("\n\t\tofficial_name=\"" + entry["official_name"] + "\"")
+            outfile.write(" />\n")
+        for entry in get_iso_entries("3166-3"):
+            outfile.write("\t<iso_3166_3_entry\n")
+            outfile.write("\t\talpha_4_code=\"" + entry["alpha_4"] + "\"\n")
+            outfile.write("\t\talpha_3_code=\"" + entry["alpha_3"] + "\"\n")
+            if "numeric" in entry:
+                outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
+            if "withdrawal_date" in entry:
+                outfile.write("\t\tdate_withdrawn=\"" + entry["withdrawal_date"] + "\"\n")
+            outfile.write("\t\tnames=\"" + entry["name"] + "\"")
+            if "comment" in entry:
+                outfile.write("\n\t\tcomment=\"" + entry["comment"] + "\"")
+            outfile.write(" />\n")
+        outfile.write("</iso_3166_entries>\n")
+    #
+    # Handle ISO 3166-2
+    #
+    elif domain == "iso_3166-2":
+        outfile.write(headers["3166-2"])
+        outfile.write("\n")
+        outfile.write("<iso_3166_2_entries>\n")
+        for country in get_iso_entries("3166-2"):
+            outfile.write("<iso_3166_country code=\"" + country["code"] + "\">\n")
+            for subset in country["subsets"]:
+                outfile.write("<iso_3166_subset type=\"" + subset["type"] + "\">\n")
+                for item in subset["items"]:
+                    outfile.write("\t<iso_3166_2_entry\n")
+                    outfile.write("\t\tcode=\"" + item["code"] + "\"\tname=\"" + item["name"] + "\"")
+                    if "parent" in item:
+                        outfile.write("\tparent=\"" + item["parent"] + "\"")
+                    outfile.write(" />\n")
+                outfile.write("</iso_3166_subset>\n")
+            outfile.write("</iso_3166_country>\n")
+        outfile.write("</iso_3166_2_entries>\n")
+    #
+    # Handle ISO 15924
+    #
+    elif domain == "iso_15924":
+        outfile.write(headers["15924"])
+        outfile.write("\n")
+        outfile.write("<iso_15924_entries>\n")
+        for entry in get_iso_entries("15924"):
+            outfile.write("\t<iso_15924_entry\n")
+            outfile.write("\t\talpha_4_code=\"" + entry["alpha_4"] + "\"\n")
             outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
-        outfile.write("\t\tcurrency_name=\"" + entry["name"] + "\"")
-        outfile.write(" />\n")
-    # Insert the obsolete historic entries, which are no
-    # longer included in the JSON data files.
-    outfile.write("""	<historic_iso_4217_entry
+            outfile.write("\t\tname=\"" + entry["name"] + "\"")
+            outfile.write(" />\n")
+        outfile.write("</iso_15924_entries>\n")
+    #
+    # Handle ISO 4217
+    #
+    elif domain == "iso_4217":
+        outfile.write(headers["4217"])
+        outfile.write("\n")
+        outfile.write("<iso_4217_entries>\n")
+        for entry in get_iso_entries("4217"):
+            outfile.write("\t<iso_4217_entry\n")
+            outfile.write("\t\tletter_code=\"" + entry["alpha_3"] + "\"\n")
+            if "numeric" in entry:
+                outfile.write("\t\tnumeric_code=\"" + entry["numeric"] + "\"\n")
+            outfile.write("\t\tcurrency_name=\"" + entry["name"] + "\"")
+            outfile.write(" />\n")
+        # Insert the obsolete historic entries, which are no
+        # longer included in the JSON data files.
+        outfile.write("""	<historic_iso_4217_entry
 		letter_code="ADP"
 		numeric_code="020"
 		currency_name="Andorran Peseta"
@@ -1019,4 +1023,4 @@ with open("iso_4217/iso_4217.xml", "w") as outfile:
 		currency_name="Zaire"
 		date_withdrawn="1994-02" />
 """)
-    outfile.write("</iso_4217_entries>\n")
+        outfile.write("</iso_4217_entries>\n")
diff --git a/common.mk b/common.mk
index e08f4ef..a5b55b2 100644
--- a/common.mk
+++ b/common.mk
@@ -23,6 +23,10 @@ MOSTLYCLEANFILES = \
 $(DOMAIN).pot: $(top_srcdir)/data/$(DOMAIN).json
 	python3 $(top_srcdir)/bin/pot_from_json.py $(DOMAIN) $(top_srcdir)/data
 
+# Generic target to create deprecated .xml files from JSON data files
+$(DOMAIN).xml: $(top_srcdir)/data/$(DOMAIN).json
+	python3 $(top_srcdir)/bin/xml_from_json.py $(DOMAIN) $(top_srcdir)/data $@
+
 # Used in the domain subdirectories for checking that
 # all .po files contain UTF-8 data
 check-local:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-isocodes/iso-codes.git



More information about the Pkg-isocodes-devel mailing list