settings.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from pathlib import Path
  5. from PySide6.QtCore import QObject, Slot, Signal
  6. from module.logger import logger
  7. class Setting(QObject):
  8. @classmethod
  9. def copy_from_template(cls) -> None:
  10. """
  11. 从模板复制一个配置文件
  12. :return:
  13. """
  14. template_path = Path.cwd() / 'module' / 'config' / 'argument' / 'setting-template.json'
  15. if not template_path.exists():
  16. logger.error('template.json not exists')
  17. return
  18. with open(template_path, 'r', encoding='utf-8') as f:
  19. data = f.read()
  20. setting_path = Path.cwd() / 'module' / 'config' / 'argument' / 'setting.json'
  21. with open(setting_path, 'w', encoding='utf-8') as f:
  22. f.write(data)
  23. logger.info('setting.json copy from template.json success')
  24. @Slot(result="QString")
  25. def read(self) -> str:
  26. """
  27. 读取配置文件的数据
  28. :return:
  29. """
  30. setting_path = Path.cwd() / 'module' / 'config' / 'argument' / 'setting.json'
  31. if not setting_path.exists():
  32. logger.error('setting.json not exists')
  33. self.copy_from_template()
  34. with open(setting_path, 'r', encoding='utf-8') as f:
  35. return f.read()
  36. @Slot(str)
  37. def update(self, data: str) -> None:
  38. """
  39. 更新配置文件的数据
  40. :return:
  41. """
  42. setting_path = Path.cwd() / 'module' / 'config' / 'argument' / 'setting.json'
  43. if not setting_path.exists():
  44. logger.error('setting.json not exists')
  45. self.copy_from_template()
  46. with open(setting_path, 'w', encoding='utf-8') as f:
  47. f.write(data)
  48. logger.info('setting.json update success')