setting.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. Copy from https://github.com/LmeSzinc/AzurLaneAutoScript
  3. """
  4. import multiprocessing
  5. import threading
  6. from multiprocessing.managers import SyncManager
  7. from typing import TYPE_CHECKING, Callable, Generic, TypeVar
  8. if TYPE_CHECKING:
  9. from module.config.config_updater import ConfigUpdater
  10. from module.server.config import DeployConfig
  11. T = TypeVar("T")
  12. class cached_class_property(Generic[T]):
  13. """
  14. Code from https://github.com/dssg/dickens
  15. Add typing support
  16. Descriptor decorator implementing a class-level, read-only
  17. property, which caches its results on the class(es) on which it
  18. operates.
  19. Inheritance is supported, insofar as the descriptor is never hidden
  20. by its cache; rather, it stores values under its access name with
  21. added underscores. For example, when wrapping getters named
  22. "choices", "choices_" or "_choices", each class's result is stored
  23. on the class at "_choices_"; decoration of a getter named
  24. "_choices_" would raise an exception.
  25. """
  26. class AliasConflict(ValueError):
  27. pass
  28. def __init__(self, func: Callable[..., T]):
  29. self.__func__ = func
  30. self.__cache_name__ = '_{}_'.format(func.__name__.strip('_'))
  31. if self.__cache_name__ == func.__name__:
  32. raise self.AliasConflict(self.__cache_name__)
  33. def __get__(self, instance, cls=None) -> T:
  34. if cls is None:
  35. cls = type(instance)
  36. try:
  37. return vars(cls)[self.__cache_name__]
  38. except KeyError:
  39. result = self.__func__(cls)
  40. setattr(cls, self.__cache_name__, result)
  41. return result
  42. class State:
  43. """
  44. Shared settings
  45. """
  46. _init = False
  47. _clearup = False
  48. restart_event: threading.Event = None
  49. manager: SyncManager = None
  50. @classmethod
  51. def init(cls):
  52. cls.manager = multiprocessing.Manager()
  53. cls._init = True
  54. @classmethod
  55. def clearup(cls):
  56. cls.manager.shutdown()
  57. cls._clearup = True
  58. @cached_class_property
  59. def deploy_config(self) -> "DeployConfig":
  60. """
  61. Returns:
  62. DeployConfig:
  63. """
  64. from module.server.config import DeployConfig
  65. return DeployConfig()
  66. @cached_class_property
  67. def config_updater(self) -> "ConfigUpdater":
  68. """
  69. Returns:
  70. ConfigUpdater:
  71. """
  72. from module.config.config_updater import ConfigUpdater
  73. return ConfigUpdater()