Project Manager for Sublime Text 3 . mirror of https://github.com/budRich/ProjectManager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

  1. import sublime
  2. import os
  3. class JsonFile:
  4. def __init__(self, fpath, encoding='utf-8'):
  5. self.encoding = encoding
  6. self.fpath = fpath
  7. def load(self, default=[]):
  8. self.fdir = os.path.dirname(self.fpath)
  9. if not os.path.isdir(self.fdir):
  10. os.makedirs(self.fdir)
  11. if os.path.exists(self.fpath):
  12. with open(self.fpath, mode='r', encoding=self.encoding) as f:
  13. content = f.read()
  14. try:
  15. data = sublime.decode_value(content)
  16. except Exception:
  17. sublime.message_dialog('%s is bad!' % self.fpath)
  18. raise
  19. if not data:
  20. data = default
  21. else:
  22. with open(self.fpath, mode='w', encoding=self.encoding, newline='\n') as f:
  23. data = default
  24. f.write(sublime.encode_value(data, True))
  25. return data
  26. def save(self, data, indent=4):
  27. self.fdir = os.path.dirname(self.fpath)
  28. if not os.path.isdir(self.fdir):
  29. os.makedirs(self.fdir)
  30. with open(self.fpath, mode='w', encoding=self.encoding, newline='\n') as f:
  31. f.write(sublime.encode_value(data, True))
  32. def remove(self):
  33. if os.path.exists(self.fpath):
  34. os.remove(self.fpath)