| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import yaml
- from pathlib import Path
- from pydantic import BaseModel
- from typing import List, Dict, Union,Optional
- APP_PATH = Path(__file__).parent.parent
- CONFIG_PATH = APP_PATH / "config.yaml"
- class Config(BaseModel):
- sub_url: Optional[str] = None
- start_port: Optional[int] = 9660 # Changed to int
- mimo_exe: Optional[str] = None
- chrome_exe: Optional[str] = None
- redis_exe: Optional[str] = None
- redis_port: Optional[int] = None # Changed to int
- def save(self):
- config_path = get_config_path()
- with open(config_path, "w", encoding="utf-8") as file:
- yaml.dump(self.model_dump(), file)
- return self
-
- def get_config_path():
- return CONFIG_PATH
- def read_config(config_path: Path):
- if not config_path.exists():
- config = Config()
- config.save()
- return config.model_dump() # Return the model as a dictionary
- with open(config_path, "r", encoding="utf-8") as file:
- config_dict = yaml.safe_load(file)
- return config_dict
- config_yaml = read_config(get_config_path())
- config = Config(**config_yaml)
- def main():
- print(config)
- if __name__ == "__main__":
- main()
|