Created
April 29, 2018 06:59
-
-
Save NGenetzky/7aa478b3592e37ddcc753994fc013ab5 to your computer and use it in GitHub Desktop.
Dumps environment varaibles to a JSON file using TinyDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
'''Dumps environment varaibles to a JSON file using TinyDB. | |
''' | |
import os | |
from tinydb import TinyDB | |
from tinydb import Query | |
DEFAULT_DB = './db.json' | |
def get_db(path=DEFAULT_DB): | |
return TinyDB(path, sort_keys=True, indent=4, separators=(',', ': ')) | |
class EnvObj(object): | |
def __init__(self, name, value): | |
self._name = name | |
self._value = value | |
def toDict(self): | |
return { | |
'name': self._name, | |
'value': self._value, | |
} | |
def main(args=None): | |
db = get_db() | |
table = db.table('env') | |
for env in os.environ: | |
e = EnvObj(env, os.environ[env]) | |
table.upsert(e.toDict(), Query()['name'] == env) | |
return 0 | |
if __name__ == "__main__": | |
import sys | |
sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment