config_model.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 pydantic import BaseModel, ValidationError, Field
  6. from module.config.utils import *
  7. from module.logger import logger
  8. # 导入配置的Python文件
  9. from tasks.Script.config import Script
  10. from tasks.Restart.config import Restart
  11. class ConfigModel(BaseModel):
  12. config_name: str = "oas"
  13. script: Script = Field(default_factory=Script)
  14. restart: Restart = Field(default_factory=Restart)
  15. # @validator('script')
  16. # def script_validator(cls, v):
  17. # if v is None:
  18. # return Script()
  19. def __init__(self, config_name: str) -> None:
  20. """
  21. :param config_name:
  22. """
  23. data = self.read_json(config_name)
  24. data["config_name"] = config_name
  25. super().__init__(**data)
  26. def __setattr__(self, key, value):
  27. """
  28. 只要修改属性就会触发这个函数 自动保存
  29. :param key:
  30. :param value:
  31. :return:
  32. """
  33. super().__setattr__(key, value)
  34. logger.info("auto save config")
  35. self.save()
  36. @staticmethod
  37. def read_json(config_name: str) -> dict:
  38. """
  39. 读文件 没有额外操作
  40. :param config_name: 不带后缀
  41. :return:
  42. """
  43. filepath = Path.cwd() / "config" / f"{config_name}.json"
  44. return read_file(filepath)
  45. @staticmethod
  46. def write_json(config_name: str, data) -> None:
  47. """
  48. :param config_name: 不带后缀
  49. :param data: 字典而不是字符串
  50. :return:
  51. """
  52. filepath = Path.cwd() / "config" / f"{config_name}.json"
  53. write_file(filepath, data)
  54. def gui_args(self, task: str) -> str:
  55. """
  56. 返回提供给gui显示的参数
  57. :param task: 输入的是任务的名称英文 如'Script' 或者是'script'都是可以的
  58. :return: 返回的是pydantic给我们结构化的输出的信息, 如果不能获取就返回空的str
  59. """
  60. task = convert_to_underscore(task)
  61. task_gui = getattr(self, task, None)
  62. if task_gui is None:
  63. logger.error(f'{task} is no inexistence')
  64. return ''
  65. return task_gui.schema_json()
  66. def gui_task(self, task: str) -> str:
  67. """
  68. 返回提供给gui显示的参数
  69. :param task:
  70. :return:
  71. """
  72. task_name = convert_to_underscore(task)
  73. task = getattr(self, task_name, None)
  74. if task is None:
  75. logger.error(f'{task_name} is no inexistence')
  76. return ''
  77. return task.json()
  78. def save(self) -> None:
  79. """
  80. :return:
  81. """
  82. self.write_json(self.config_name, self.dict())
  83. # @root_validator
  84. # def on_on_property_change(cls, values):
  85. # """
  86. # 当属性改变时保存
  87. # :param values:
  88. # :return:
  89. # """
  90. # logger.info(f'property change auto save')
  91. # cls.save()
  92. if __name__ == "__main__":
  93. try:
  94. c = ConfigModel("oas1")
  95. except ValidationError as e:
  96. print(e)
  97. c = ConfigModel()
  98. # c.write_json('oas1', c.dict())
  99. # c.write_json('oas1', c.schema())
  100. c.script.device.serial = "5555"