config.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Copy from https://github.com/LmeSzinc/AzurLaneAutoScript
  3. """
  4. from typing import Optional, Union
  5. from filelock import FileLock
  6. from deploy.config import DeployConfig as _DeployConfig
  7. from deploy.utils import *
  8. def poor_yaml_read_with_lock(file):
  9. if not os.path.exists(file):
  10. return {}
  11. with FileLock(f"{file}.lock"):
  12. return poor_yaml_read(file)
  13. def poor_yaml_write_with_lock(data, file, template_file=DEPLOY_TEMPLATE):
  14. folder = os.path.dirname(file)
  15. if not os.path.exists(folder):
  16. os.mkdir(folder)
  17. with FileLock(f"{file}.lock"):
  18. with FileLock(f"{DEPLOY_TEMPLATE}.lock"):
  19. return poor_yaml_write(data, file, template_file)
  20. class DeployConfig(_DeployConfig):
  21. def show_config(self):
  22. pass
  23. def read(self):
  24. """
  25. Read and update deploy config, copy `self.configs` to properties.
  26. """
  27. self.config = poor_yaml_read_with_lock(DEPLOY_TEMPLATE)
  28. self.config.update(poor_yaml_read_with_lock(self.file))
  29. for key, value in self.config.items():
  30. if hasattr(self, key):
  31. super().__setattr__(key, value)
  32. def write(self):
  33. """
  34. Write `self.config` into deploy config.
  35. """
  36. poor_yaml_write_with_lock(self.config, self.file)
  37. def __setattr__(self, key: str, value):
  38. """
  39. Catch __setattr__, copy to `self.config`, write deploy config.
  40. """
  41. super().__setattr__(key, value)
  42. if key[0].isupper() and key in self.config:
  43. self.config[key] = value
  44. self.write()