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

Loading…
Cancel
Save