Validate JSON when quiting the editor

This commit is contained in:
Daniel Berteaud 2018-12-16 10:12:04 +01:00
parent 7ff6f1a504
commit 58ed888d63
1 changed files with 14 additions and 6 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import os, sys, tempfile,getopt
import os, sys, tempfile, getopt, json
from cryptography.fernet import Fernet
def main():
@ -36,11 +36,19 @@ def main():
clear = f.decrypt(crypt)
tmp.write(clear)
tmp.flush()
# Now, lets open our favorite editor to edit the file
os.system(os.getenv('EDITOR', 'vim') + ' ' + tmp.name)
# We closed the editor, we just have to open the cleartext file, encrypt its content
# and save it
clear = open(tmp.name, 'rb').read()
loop = 1
while loop == 1:
# Now, lets open our favorite editor to edit the file
os.system(os.getenv('EDITOR', 'vim') + ' ' + tmp.name)
# We closed the editor, we just have to open the cleartext file, encrypt its content
# and save it
clear = open(tmp.name, 'rb').read()
loop = 0
# Validate JSON data. Edit agin if not valid
try:
json.loads(clear)
except:
loop = 1
wcrypt = open(args[0], 'wb')
wcrypt.write(f.encrypt(clear))
wcrypt.flush()