post_gen_project.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 os
  12. import shutil
  13. import subprocess
  14. def handle_services(use_rest, use_core):
  15. if use_core or use_rest:
  16. # Write "import taipy as tp" at the third line of the import.txt file
  17. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "r") as import_file:
  18. import_lines = import_file.readlines()
  19. import_lines[0] = "import taipy as tp\n" + import_lines[0] + "\n"
  20. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "w") as import_file:
  21. import_file.writelines(import_lines)
  22. # Import the necessary services
  23. if use_core and use_rest:
  24. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "a") as import_file:
  25. import_file.write("from taipy import Core, Rest\n")
  26. elif use_core:
  27. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "a") as import_file:
  28. import_file.write("from taipy import Core\n")
  29. elif use_rest:
  30. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "a") as import_file:
  31. import_file.write("from taipy import Rest\n")
  32. # Start the Rest service
  33. if use_rest:
  34. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "a") as main_file:
  35. main_file.write(" rest = Rest()\n")
  36. if use_core:
  37. # Create and submit the placeholder scenario
  38. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "a") as main_file:
  39. main_file.write(" core = Core()\n")
  40. if not use_rest:
  41. main_file.write(" core.run()\n")
  42. main_file.write(" # #############################################################################\n")
  43. main_file.write(" # PLACEHOLDER: Create and submit your scenario here #\n")
  44. main_file.write(" # #\n")
  45. main_file.write(" # Example: #\n")
  46. main_file.write(" # from configuration import scenario_config #\n")
  47. main_file.write(" # scenario = tp.create_scenario(scenario_config) #\n")
  48. main_file.write(" # scenario.submit() #\n")
  49. main_file.write(" # Comment, remove or replace the previous lines with your own use case #\n")
  50. main_file.write(" # #############################################################################\n")
  51. else:
  52. shutil.rmtree(os.path.join(os.getcwd(), "algorithms"))
  53. shutil.rmtree(os.path.join(os.getcwd(), "configuration"))
  54. def handle_run_service():
  55. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "a+") as main_file:
  56. main_file.seek(0)
  57. main_content = main_file.read()
  58. # Run Rest service along with the GUI service
  59. if "rest = Rest()" in main_content:
  60. main_file.write(' tp.run(gui, rest, title="{{cookiecutter.__application_title}}")\n')
  61. else:
  62. main_file.write(' gui.run(title="{{cookiecutter.__application_title}}")\n')
  63. def handle_single_page_app():
  64. shutil.rmtree(os.path.join(os.getcwd(), "pages"))
  65. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "a") as main_file:
  66. main_file.write("\n")
  67. main_file.write(" gui = Gui(page=page)\n")
  68. handle_run_service()
  69. with open(os.path.join(os.getcwd(), "sections", "page_content.txt"), "a") as page_content_file:
  70. page_content_file.write(
  71. '''
  72. page = """
  73. <center>
  74. <|navbar|lov={[("home", "Homepage")]}|>
  75. </center>
  76. """
  77. '''
  78. )
  79. def handle_multi_page_app(pages):
  80. for page_name in pages:
  81. os.mkdir(os.path.join(os.getcwd(), "pages", page_name))
  82. with open(os.path.join(os.getcwd(), "pages", "page_example", "page_example.md"), "r") as page_md_file:
  83. page_md_content = page_md_file.read()
  84. page_md_content = page_md_content.replace("Page example", page_name.replace("_", " ").title())
  85. with open(os.path.join(os.getcwd(), "pages", page_name, page_name + ".md"), "w") as page_md_file:
  86. page_md_file.write(page_md_content)
  87. with open(os.path.join(os.getcwd(), "pages", "page_example", "page_example.py"), "r") as page_content_file:
  88. page_py_content = page_content_file.read()
  89. page_py_content = page_py_content.replace("page_example", page_name)
  90. with open(os.path.join(os.getcwd(), "pages", page_name, page_name + ".py"), "w") as page_content_file:
  91. page_content_file.write(page_py_content)
  92. with open(os.path.join(os.getcwd(), "pages", "__init__.py"), "a") as page_init_file:
  93. for page_name in pages:
  94. page_init_file.write(f"from .{page_name}.{page_name} import {page_name}\n")
  95. shutil.rmtree(os.path.join(os.getcwd(), "pages", "page_example"))
  96. newline = ",\n "
  97. user_page_dict = newline.join(f'"{page_name}": {page_name}' for page_name in pages)
  98. page_dict = """
  99. pages = {
  100. "/": root_page,
  101. {pages}
  102. }
  103. """
  104. with open(os.path.join(os.getcwd(), "sections", "page_content.txt"), "a") as page_content_file:
  105. page_content_file.write(page_dict.replace("{pages}", user_page_dict))
  106. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "a") as import_file:
  107. import_file.write("from pages import *\n")
  108. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "a") as main_file:
  109. main_file.write("\n")
  110. main_file.write(" gui = Gui(pages=pages)\n")
  111. handle_run_service()
  112. def generate_main_file():
  113. with open(os.path.join(os.getcwd(), "sections", "import.txt"), "r") as import_file:
  114. import_lines = import_file.read()
  115. with open(os.path.join(os.getcwd(), "sections", "page_content.txt"), "r") as page_content_file:
  116. page_content = page_content_file.read()
  117. with open(os.path.join(os.getcwd(), "sections", "main.txt"), "r") as main_file:
  118. main_lines = main_file.read()
  119. with open(os.path.join(os.getcwd(), "{{cookiecutter.__main_file}}.py"), "a") as app_main_file:
  120. app_main_file.write(import_lines)
  121. app_main_file.write("\n")
  122. app_main_file.write(page_content)
  123. app_main_file.write("\n\n")
  124. app_main_file.write(main_lines)
  125. def initialize_as_git_project(project_dir: str) -> str:
  126. if shutil.which("git") is None:
  127. msg = "\nERROR: Git executable not found, skipping git initialisation"
  128. return msg
  129. try:
  130. subprocess.run(
  131. ["git", "init", "."],
  132. cwd=project_dir,
  133. stdout=subprocess.DEVNULL,
  134. stderr=subprocess.DEVNULL,
  135. check=True,
  136. )
  137. msg = f"\nInitialized Git repository in {project_dir}"
  138. except subprocess.CalledProcessError:
  139. msg = f"\nERROR: Failed to initialise Git repository in {project_dir}"
  140. return msg
  141. use_core = "{{ cookiecutter.__core }}".upper()
  142. use_rest = "{{ cookiecutter.__rest }}".upper()
  143. handle_services(use_rest in ["YES", "Y"], use_core in ["YES", "Y"])
  144. pages = "{{ cookiecutter.__pages }}".split(" ")
  145. # Remove empty string from pages list
  146. pages = [page for page in pages if page != ""]
  147. if len(pages) == 0:
  148. handle_single_page_app()
  149. else:
  150. handle_multi_page_app(pages)
  151. generate_main_file()
  152. # Remove the sections folder
  153. shutil.rmtree(os.path.join(os.getcwd(), "sections"))
  154. # Initialize the project as a git repository
  155. git_init_message = ""
  156. if "{{ cookiecutter.__git }}".upper() in ["YES", "Y"]:
  157. git_init_message = initialize_as_git_project(os.getcwd())
  158. else:
  159. os.remove(os.path.join(os.getcwd(), ".gitignore"))
  160. main_file_name = "{{cookiecutter.__main_file}}.py"
  161. print(
  162. f"New Taipy application has been created at {os.path.join(os.getcwd())}"
  163. f"{git_init_message}"
  164. f"\n\nTo start the application, change directory to the newly created folder:"
  165. f"\n\tcd {os.path.join(os.getcwd())}"
  166. f"\nand run the application as follows:"
  167. f"\n\ttaipy run {main_file_name}"
  168. )