From fc05f221c4fbbf4be31c0c1b2c9293eacb9ba680 Mon Sep 17 00:00:00 2001
From: Thomas Zastrow <thomas.zastrow@mpcdf.mpg.de>
Date: Tue, 2 Aug 2022 15:42:04 +0200
Subject: [PATCH] Implementing template functionality

---
 mmd_tools/mmdCreate.py | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/mmd_tools/mmdCreate.py b/mmd_tools/mmdCreate.py
index f785e49..cafc21b 100644
--- a/mmd_tools/mmdCreate.py
+++ b/mmd_tools/mmdCreate.py
@@ -3,23 +3,27 @@
 from datetime import date
 import uuid
 import argparse
+import json
 
 import mmdFormats
 
-
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
     parser.add_argument("-o", help="specify output file")
     parser.add_argument("-format", help="specify metadata format")
+    parser.add_argument("-template", help="A JSON file with some pre-filled key value pairs")
     args = parser.parse_args()
 
     outfile = "metadata.mmd"
+
+    # The outfile which will store the metadata:
     if args.o:
         print("Outputfile: ", args.o)
         outfile = args.o
     else:
         print("Outputfile: metadata.mmd")
 
+    # Was there a metadataformat specified. If not, use MmdSimple:
     if args.format:
         print("Metadata format: ", args.format)
     else:
@@ -29,9 +33,19 @@ if __name__ == "__main__":
         metadata =  mmdFormats.DublinCore()
     else:
         metadata = mmdFormats.MmdSimple()
-    
+
+    # Was there a template with JSON encoded key value pairs specified:
+    if args.template:
+        infile = open(args.template)
+        kvps = json.load(infile)
+        for entry in metadata.data:
+            if entry.name in kvps:
+                entry.value = kvps[entry.name]
+         
+    # A UUID as id:
     metadata.id = str(uuid.uuid4())
 
+    # Lets get the metadata from the user:
     for entry in metadata.data:
         if entry.vocabulary:
             record = input(entry.tip + " [" + entry.value + " possible values: " + str(entry.vocabulary) +"]: ")
@@ -41,6 +55,7 @@ if __name__ == "__main__":
         if record != "":
             entry.value = record
             
+    # Writing back the metadata:
     outputfile =  open(outfile, 'w')
     outputfile.write(metadata.toJson())
     outputfile.close()
\ No newline at end of file
-- 
GitLab