config.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import json, os, pathlib, logging
  2. from protolib import get_app_root
  3. from widgets.file_browser import FileBrowserWidget
  4. logger = logging.getLogger(__name__)
  5. def load_config(file_browser: FileBrowserWidget):
  6. os.makedirs(pathlib.Path(get_app_root(), 'config'), exist_ok=True)
  7. os.makedirs(pathlib.Path(get_app_root(), 'config', 'typing'), exist_ok=True)
  8. path = pathlib.Path(get_app_root(), 'config', 'typing', 'types.json')
  9. if os.path.exists(path):
  10. with open(path) as f:
  11. file_browser.config['typing']['types'] = json.load(f)
  12. f.close()
  13. else:
  14. with open(path, 'w') as f:
  15. json.dump(file_browser.config['typing']['types'], f)
  16. f.close()
  17. path = pathlib.Path(get_app_root(), 'config', 'typing', 'mimetypes.json')
  18. if os.path.exists(path):
  19. with open(path) as f:
  20. file_browser.config['typing']['mimetypes'] = json.load(f)
  21. f.close()
  22. else:
  23. with open(path, 'w') as f:
  24. json.dump(file_browser.config['typing']['mimetypes'], f)
  25. f.close()
  26. logger.info('Loaded config')
  27. def dump_config(file_browser: FileBrowserWidget):
  28. os.makedirs(pathlib.Path(get_app_root(), 'config'), exist_ok=True)
  29. os.makedirs(pathlib.Path(get_app_root(), 'config', 'typing'), exist_ok=True)
  30. path = pathlib.Path(get_app_root(), 'config', 'typing', 'types.json')
  31. with open(path, 'w') as f:
  32. json.dump(file_browser.config['typing']['types'], f)
  33. f.close()
  34. path = pathlib.Path(get_app_root(), 'config', 'typing', 'mimetypes.json')
  35. with open(path, 'w') as f:
  36. json.dump(file_browser.config['typing']['mimetypes'], f)
  37. f.close()
  38. logger.info('Dumped config')