config.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import yaml
  2. from pathlib import Path
  3. from pydantic import BaseModel
  4. from typing import List, Dict, Union,Optional
  5. APP_PATH = Path(__file__).parent.parent
  6. CONFIG_PATH = APP_PATH / "config.yaml"
  7. class Config(BaseModel):
  8. sub_url: Optional[str] = None
  9. start_port: Optional[int] = 9660 # Changed to int
  10. mimo_exe: Optional[str] = None
  11. chrome_exe: Optional[str] = None
  12. redis_exe: Optional[str] = None
  13. redis_port: Optional[int] = None # Changed to int
  14. def save(self):
  15. config_path = get_config_path()
  16. with open(config_path, "w", encoding="utf-8") as file:
  17. yaml.dump(self.model_dump(), file)
  18. return self
  19. def get_config_path():
  20. return CONFIG_PATH
  21. def read_config(config_path: Path):
  22. if not config_path.exists():
  23. config = Config()
  24. config.save()
  25. return config.model_dump() # Return the model as a dictionary
  26. with open(config_path, "r", encoding="utf-8") as file:
  27. config_dict = yaml.safe_load(file)
  28. return config_dict
  29. config_yaml = read_config(get_config_path())
  30. config = Config(**config_yaml)
  31. def main():
  32. print(config)
  33. if __name__ == "__main__":
  34. main()