_config_blocker.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import functools
  12. from ...logger._taipy_logger import _TaipyLogger
  13. from ..exceptions.exceptions import ConfigurationUpdateBlocked
  14. class _ConfigBlocker:
  15. """Configuration blocker singleton."""
  16. __logger = _TaipyLogger._get_logger()
  17. __block_config_update = False
  18. @classmethod
  19. def _block(cls):
  20. if not cls.__block_config_update:
  21. cls.__logger.info("Blocking configuration update.")
  22. cls.__block_config_update = True
  23. @classmethod
  24. def _unblock(cls):
  25. if cls.__block_config_update:
  26. cls.__logger.info("Unblocking configuration update.")
  27. cls.__block_config_update = False
  28. @classmethod
  29. def _check(cls):
  30. def inner(f):
  31. @functools.wraps(f)
  32. def _check_if_is_blocking(*args, **kwargs):
  33. if cls.__block_config_update:
  34. error_message = (
  35. "The Core service should be stopped by running core.stop() before"
  36. " modifying the Configuration. For more information, please refer to:"
  37. " https://docs.taipy.io/en/latest/manuals/running_services/#running-core."
  38. )
  39. cls.__logger.error(f"ConfigurationUpdateBlocked: {error_message}")
  40. raise ConfigurationUpdateBlocked(error_message)
  41. return f(*args, **kwargs)
  42. return _check_if_is_blocking
  43. return inner