settings.py 889 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import json
  2. _file_path = 'settings.json'
  3. default_melody = 'melody.wav'
  4. def _construct_settings_object() -> dict:
  5. return {
  6. 'default_melody': default_melody
  7. }
  8. def _deserialize_settings(dictionary: dict) -> None:
  9. global default_melody
  10. default_melody = dictionary.get('default_melody', 'melody.wav')
  11. def load_settings() -> None:
  12. try:
  13. file = open(_file_path, 'r', encoding='utf-8')
  14. data = file.read()
  15. dictionary = json.loads(data)
  16. _deserialize_settings(dictionary)
  17. file.close()
  18. except Exception:
  19. _deserialize_settings({})
  20. save_settings()
  21. print('Выставлены настройки приложения по умолчанию!')
  22. def save_settings() -> None:
  23. file = open(_file_path, 'w+')
  24. data = json.dumps(_construct_settings_object())
  25. file.write(data)
  26. file.close()