_create_cli.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 pathlib
  12. import sys
  13. from typing import Dict, Optional
  14. from cookiecutter.exceptions import OutputDirExistsException
  15. from cookiecutter.main import cookiecutter
  16. import taipy
  17. from ._base_cli._abstract_cli import _AbstractCLI
  18. from ._base_cli._taipy_parser import _TaipyParser
  19. class _CreateCLI(_AbstractCLI):
  20. _template_map: Dict[str, str] = {}
  21. _COMMAND_NAME = "create"
  22. _ARGUMENTS = ["--application"]
  23. @classmethod
  24. def generate_template_map(cls, template_path: Optional[pathlib.Path] = None):
  25. if not template_path:
  26. template_path = pathlib.Path(taipy.__file__).parent.resolve() / "templates"
  27. # Update the template map with the new templates but do not override the existing ones
  28. cls._template_map.update(
  29. {
  30. str(x.name): str(x)
  31. for x in template_path.iterdir()
  32. if x.is_dir() and not x.name.startswith("_") and x.name not in cls._template_map
  33. }
  34. )
  35. @classmethod
  36. def create_parser(cls):
  37. create_parser = _TaipyParser._add_subparser(
  38. cls._COMMAND_NAME,
  39. help="Create a new Taipy application using pre-defined templates.",
  40. )
  41. create_parser.add_argument(
  42. "--application",
  43. choices=list(cls._template_map.keys()),
  44. default="default",
  45. help="The template used to create the new Taipy application.",
  46. )
  47. @classmethod
  48. def handle_command(cls):
  49. args = cls._parse_arguments()
  50. if not args:
  51. return
  52. try:
  53. cookiecutter(cls._template_map[args.application])
  54. except OutputDirExistsException as err:
  55. error_msg = f"{str(err)}. Please remove the existing directory or provide a new folder name."
  56. print(error_msg) # noqa: T201
  57. sys.exit(1)
  58. sys.exit(0)