Validate JSON when quiting the editor

master
Daniel Berteaud 6 years ago
parent 7ff6f1a504
commit 58ed888d63
  1. 20
      bin/var_editor.py

@ -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()

Loading…
Cancel
Save