_core_cli.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. from typing import Dict
  12. from taipy._cli._base_cli._abstract_cli import _AbstractCLI
  13. from taipy._cli._base_cli._taipy_parser import _TaipyParser
  14. from .config import CoreSection
  15. class _CoreCLI(_AbstractCLI):
  16. """Command-line interface for Taipy Core application."""
  17. __MODE_ARGS: Dict[str, Dict] = {
  18. "--development": {
  19. "action": "store_true",
  20. "dest": "taipy_development",
  21. "help": """
  22. When execute Taipy application in `development` mode, all entities from the previous development version
  23. will be deleted before running new Taipy application.
  24. """,
  25. },
  26. "--experiment": {
  27. "dest": "taipy_experiment",
  28. "nargs": "?",
  29. "const": "",
  30. "metavar": "VERSION",
  31. "help": """
  32. When execute Taipy application in `experiment` mode, the current Taipy application is saved to a new
  33. version. If version name already exists, check for compatibility with current Python Config and run the
  34. application. Without being specified, the version number will be a random string.
  35. """,
  36. },
  37. "--production": {
  38. "dest": "taipy_production",
  39. "nargs": "?",
  40. "const": "",
  41. "metavar": "VERSION",
  42. "help": """
  43. When execute in `production` mode, the current version is used in production. All production versions
  44. should have the same configuration and share all entities. Without being specified, the latest version
  45. is used.
  46. """,
  47. },
  48. }
  49. __FORCE_ARGS: Dict[str, Dict] = {
  50. "--force": {
  51. "dest": "taipy_force",
  52. "action": "store_true",
  53. "help": """
  54. Force override the configuration of the version if existed and run the application. Default to False.
  55. """,
  56. },
  57. "--no-force": {
  58. "dest": "no_taipy_force",
  59. "action": "store_true",
  60. "help": "Stop the application if any Config conflict exists.",
  61. },
  62. }
  63. @classmethod
  64. def create_parser(cls):
  65. core_parser = _TaipyParser._add_groupparser("Taipy Core", "Optional arguments for Taipy Core service")
  66. mode_group = core_parser.add_mutually_exclusive_group()
  67. for mode_arg, mode_arg_dict in cls.__MODE_ARGS.items():
  68. mode_group.add_argument(mode_arg, cls.__add_taipy_prefix(mode_arg), **mode_arg_dict)
  69. force_group = core_parser.add_mutually_exclusive_group()
  70. for force_arg, force_arg_dict in cls.__FORCE_ARGS.items():
  71. force_group.add_argument(cls.__add_taipy_prefix(force_arg), **force_arg_dict)
  72. @classmethod
  73. def create_run_parser(cls):
  74. run_parser = _TaipyParser._add_subparser("run", help="Run a Taipy application.")
  75. mode_group = run_parser.add_mutually_exclusive_group()
  76. for mode_arg, mode_arg_dict in cls.__MODE_ARGS.items():
  77. mode_group.add_argument(mode_arg, **mode_arg_dict)
  78. force_group = run_parser.add_mutually_exclusive_group()
  79. for force_arg, force_arg_dict in cls.__FORCE_ARGS.items():
  80. force_group.add_argument(force_arg, **force_arg_dict)
  81. @classmethod
  82. def handle_command(cls):
  83. args, _ = _TaipyParser._parser.parse_known_args()
  84. as_dict = {}
  85. if args.taipy_development:
  86. as_dict[CoreSection._MODE_KEY] = CoreSection._DEVELOPMENT_MODE
  87. elif args.taipy_experiment is not None:
  88. as_dict[CoreSection._MODE_KEY] = CoreSection._EXPERIMENT_MODE
  89. as_dict[CoreSection._VERSION_NUMBER_KEY] = args.taipy_experiment
  90. elif args.taipy_production is not None:
  91. as_dict[CoreSection._MODE_KEY] = CoreSection._PRODUCTION_MODE
  92. as_dict[CoreSection._VERSION_NUMBER_KEY] = args.taipy_production
  93. if args.taipy_force:
  94. as_dict[CoreSection._FORCE_KEY] = True
  95. elif args.no_taipy_force:
  96. as_dict[CoreSection._FORCE_KEY] = False
  97. return as_dict
  98. @classmethod
  99. def __add_taipy_prefix(cls, key: str):
  100. if key.startswith("--no-"):
  101. return f"{key[:5]}taipy-{key[5:]}"
  102. return f"{key[:2]}taipy-{key[2:]}"