config.py 930 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import util
  3. import json
  4. from website import S3Website
  5. __config_file__ = os.path.join(os.path.expanduser("~"), ".vaporfile")
  6. def load_config(path=None):
  7. if not path:
  8. path = __config_file__
  9. with open(path) as f:
  10. config = json.loads(f.read())
  11. try:
  12. config["websites"]
  13. except KeyError:
  14. config["websites"] = {}
  15. return config
  16. def get_config(path=None):
  17. """Get the config, load it if possible, create it if necessary"""
  18. try:
  19. c = load_config(path)
  20. except IOError:
  21. # No existing config, create a new one.
  22. c = {}
  23. return c
  24. def save_config(
  25. config, path=None):
  26. if not path:
  27. path = __config_file__
  28. # JSON is a better pickle:
  29. with open(path, "w") as f:
  30. f.write(json.dumps(config, sort_keys=True, indent=4))
  31. # Make the file read/write only to the user.
  32. os.chmod(path, 0o0600)