From f8a748415cd35e72c184ceae4ae6f132954bb213 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 20 Dec 2018 15:37:36 +0100 Subject: [PATCH] Save vars in yaml format --- bin/var_editor.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/var_editor.py b/bin/var_editor.py index e9110ec..f196071 100755 --- a/bin/var_editor.py +++ b/bin/var_editor.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import os, sys, tempfile, getopt, json +import os, sys, tempfile, getopt, json, yaml from cryptography.fernet import Fernet def main(): @@ -28,12 +28,17 @@ def main(): # Create a fernet object using our key f = Fernet(key) # This temp file will hold the decrypted content while we edit it - tmp = tempfile.NamedTemporaryFile() + tmp = tempfile.NamedTemporaryFile(suffix='.yml') # We open the file which contains the encrypted content, if it exists if os.path.exists(args[0]): crypt = open(args[0], 'rb').read() # And we decrypt it, and write it in the temp file clear = f.decrypt(crypt) + try: + convert = yaml.dump(json.loads(clear)).encode('utf-8') + clear = convert + except: + pass tmp.write(clear) tmp.flush() loop = 1 @@ -44,9 +49,9 @@ def main(): # and save it clear = open(tmp.name, 'rb').read() loop = 0 - # Validate JSON data. Edit agin if not valid + # Validate YAML data. Edit agin if not valid try: - json.loads(clear) + yaml.safe_load(clear) except: loop = 1 wcrypt = open(args[0], 'wb') @@ -55,7 +60,7 @@ def main(): wcrypt.close() def usage(): - print(argv[0] + ' [-k secret] [-K ./secret.txt] variables.json') + print(argv[0] + ' [-k secret] [-K ./secret.txt] variables.txt') if __name__ == '__main__': main()