generate_pyi.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 json
  12. import os
  13. import typing as t
  14. # ############################################################
  15. # Generate Python interface definition files
  16. # ############################################################
  17. from taipy.gui.config import Config
  18. # ############################################################
  19. # Generate gui pyi file (gui/gui.pyi)
  20. # ############################################################
  21. gui_py_file = "./taipy/gui/gui.py"
  22. gui_pyi_file = gui_py_file + "i"
  23. os.system(f"pipenv run stubgen {gui_py_file} --no-import --parse-only --export-less -o ./")
  24. gui_config = "".join(
  25. f", {k}: {v.__name__} = ..."
  26. if "<class" in str(v)
  27. else f", {k}: {str(v).replace('typing', 't').replace('taipy.gui.config.', '')} = ..."
  28. for k, v in Config.__annotations__.items()
  29. )
  30. replaced_content = ""
  31. with open(gui_pyi_file, "r") as file:
  32. for line in file:
  33. if "def run(" in line:
  34. replace_str = line[line.index(", run_server") : (line.index("**kwargs") + len("**kwargs"))]
  35. # ", run_server: bool = ..., run_in_thread: bool = ..., async_mode: str = ..., **kwargs"
  36. line = line.replace(replace_str, gui_config)
  37. replaced_content = replaced_content + line
  38. with open(gui_pyi_file, "w") as write_file:
  39. write_file.write(replaced_content)
  40. # ############################################################
  41. # Generate Page Builder pyi file (gui/builder/__init__.pyi)
  42. # ############################################################
  43. builder_py_file = "./taipy/gui/builder/__init__.py"
  44. builder_pyi_file = builder_py_file + "i"
  45. with open("./taipy/gui/viselements.json", "r") as file:
  46. viselements = json.load(file)
  47. with open("./tools/gui/builder/block.txt", "r") as file:
  48. block_template = file.read()
  49. with open("./tools/gui/builder/control.txt", "r") as file:
  50. control_template = file.read()
  51. os.system(f"pipenv run stubgen {builder_py_file} --no-import --parse-only --export-less -o ./")
  52. with open(builder_pyi_file, "a") as file:
  53. file.write("from ._element import _Element, _Block\n")
  54. def get_properties(element, viselements) -> t.List[t.Dict[str, t.Any]]:
  55. properties = element["properties"]
  56. if "inherits" not in element:
  57. return properties
  58. for inherit in element["inherits"]:
  59. inherit_element = next((e for e in viselements["undocumented"] if e[0] == inherit), None)
  60. if inherit_element is None:
  61. inherit_element = next((e for e in viselements["blocks"] if e[0] == inherit), None)
  62. if inherit_element is None:
  63. inherit_element = next((e for e in viselements["controls"] if e[0] == inherit), None)
  64. if inherit_element is None:
  65. raise RuntimeError(f"Can't find element with name {inherit}")
  66. properties += get_properties(inherit_element[1], viselements)
  67. return properties
  68. def build_doc(element: t.Dict[str, t.Any]):
  69. if "doc" not in element:
  70. return ""
  71. doc = str(element["doc"]).replace("\n", f'\n{16*" "}')
  72. return f"{element['name']} ({element['type']}): {doc} {'(default: '+element['default_value'] + ')' if 'default_value' in element else ''}" # noqa: E501
  73. for control_element in viselements["controls"]:
  74. name = control_element[0]
  75. property_list = []
  76. property_names = []
  77. for property in get_properties(control_element[1], viselements):
  78. if property["name"] not in property_names and "[" not in property["name"]:
  79. property_list.append(property)
  80. property_names.append(property["name"])
  81. properties = ", ".join([f"{p} = ..." for p in property_names])
  82. doc_arguments = f"\n{12*' '}".join([build_doc(p) for p in property_list])
  83. # append properties to __init__.pyi
  84. with open(builder_pyi_file, "a") as file:
  85. file.write(
  86. control_template.replace("{{name}}", name)
  87. .replace("{{properties}}", properties)
  88. .replace("{{doc_arguments}}", doc_arguments)
  89. )
  90. for block_element in viselements["blocks"]:
  91. name = block_element[0]
  92. property_list = []
  93. property_names = []
  94. for property in get_properties(block_element[1], viselements):
  95. if property["name"] not in property_names and "[" not in property["name"]:
  96. property_list.append(property)
  97. property_names.append(property["name"])
  98. properties = ", ".join([f"{p} = ..." for p in property_names])
  99. doc_arguments = f"{8*' '}".join([build_doc(p) for p in property_list])
  100. # append properties to __init__.pyi
  101. with open(builder_pyi_file, "a") as file:
  102. file.write(
  103. block_template.replace("{{name}}", name)
  104. .replace("{{properties}}", properties)
  105. .replace("{{doc_arguments}}", doc_arguments)
  106. )
  107. os.system(f"pipenv run isort {gui_pyi_file}")
  108. os.system(f"pipenv run black {gui_pyi_file}")
  109. os.system(f"pipenv run isort {builder_pyi_file}")
  110. os.system(f"pipenv run black {builder_pyi_file}")