gen_mds.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import minigrid.wrappers
  2. __author__ = "Feng Gu"
  3. __email__ = "contact@fenggu.me"
  4. """
  5. isort:skip_file
  6. """
  7. import inspect
  8. import os
  9. import re
  10. from gymnasium.envs.registration import registry
  11. from tqdm import tqdm
  12. from utils import trim
  13. readme_path = os.path.join(
  14. os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
  15. "README.md",
  16. )
  17. LAYOUT = "env"
  18. pattern = re.compile(r"(?<!^)(?=[A-Z])")
  19. all_envs = list(registry.values())
  20. filtered_envs_by_type = {}
  21. env_names = []
  22. # Obtain filtered list
  23. for env_spec in tqdm(all_envs):
  24. # minigrid.envs:Env
  25. split = env_spec.entry_point.split(".")
  26. # ignore gymnasium.envs.env_type:Env
  27. env_module = split[0]
  28. if env_module != "minigrid":
  29. continue
  30. env_name = split[1]
  31. filtered_envs_by_type[env_name] = env_spec
  32. filtered_envs = {
  33. env[0]: env[1]
  34. for env in sorted(
  35. filtered_envs_by_type.items(),
  36. key=lambda item: item[1].entry_point.split(".")[1],
  37. )
  38. }
  39. for env_name, env_spec in filtered_envs.items():
  40. made = env_spec.make()
  41. docstring = trim(made.unwrapped.__doc__)
  42. pascal_env_name = env_spec.id.split("-")[1]
  43. # remove suffix
  44. p = re.compile(r"([A-Z][a-z]+)*")
  45. name = p.search(pascal_env_name).group()
  46. snake_env_name = pattern.sub("_", name).lower()
  47. env_names.append(snake_env_name)
  48. title_env_name = snake_env_name.replace("_", " ").title()
  49. v_path = os.path.join(
  50. os.path.dirname(os.path.dirname(__file__)),
  51. "environments",
  52. snake_env_name + ".md",
  53. )
  54. front_matter = f"""---
  55. autogenerated:
  56. title: {title_env_name}
  57. ---
  58. """
  59. title = f"# {title_env_name}"
  60. if docstring is None:
  61. docstring = "No information provided"
  62. all_text = f"""{front_matter}
  63. {title}
  64. {docstring}
  65. """
  66. file = open(v_path, "w+", encoding="utf-8")
  67. file.write(all_text)
  68. file.close()
  69. # gen /environments/index.md
  70. index_texts = """---
  71. firstpage:
  72. lastpage:
  73. ---
  74. """
  75. env_index_toctree = """
  76. ```{toctree}
  77. :hidden:
  78. """
  79. sections = []
  80. with open(readme_path) as f:
  81. readme = f.read()
  82. """
  83. sections = [description, publications, installation, basic usage, wrappers, design, included environments&etc]
  84. """
  85. sections = readme.split("<br>")
  86. index_texts += sections[6]
  87. index_texts += env_index_toctree
  88. for env_name in env_names:
  89. index_texts += env_name + "\n"
  90. index_texts += """\n```\n"""
  91. f.close()
  92. output_path = os.path.join(
  93. os.path.dirname(os.path.dirname(__file__)),
  94. "environments",
  95. "index.md",
  96. )
  97. # output index.md
  98. with open(output_path, "w+") as f:
  99. f.write(index_texts)
  100. f.close()
  101. # gen /environments/design.md
  102. design_path = os.path.join(
  103. os.path.dirname(os.path.dirname(__file__)),
  104. "environments",
  105. "design.md",
  106. )
  107. design_texts = """---
  108. layout: "contents"
  109. title: Design
  110. firstpage:
  111. ---\n"""
  112. design_texts += sections[5]
  113. with open(design_path, "w+") as f:
  114. f.write(design_texts)
  115. f.close()
  116. # gen /environments/wrappers.md
  117. wrappers_path = os.path.join(
  118. os.path.dirname(os.path.dirname(__file__)),
  119. "api",
  120. "wrappers.md",
  121. )
  122. wrappers_texts = (
  123. """---
  124. title: Wrappers
  125. lastpage:
  126. ---\n"""
  127. + sections[4]
  128. + "\n"
  129. )
  130. for name, obj in inspect.getmembers(minigrid.wrappers):
  131. if inspect.isclass(obj) and obj.__doc__ is not None:
  132. formatted_doc = " ".join(trim(obj.__doc__).split())
  133. wrappers_texts += f"""## {name}
  134. {formatted_doc}\n\n"""
  135. with open(wrappers_path, "w+") as f:
  136. f.write(wrappers_texts)
  137. f.close()
  138. # gen content/pubs.md
  139. pubs_path = os.path.join(
  140. os.path.dirname(os.path.dirname(__file__)),
  141. "content",
  142. "pubs.md",
  143. )
  144. pubs_texts = (
  145. """---
  146. layout: "contents"
  147. title: Publications
  148. firstpage:
  149. ---\n#List of Publications\n"""
  150. + sections[1]
  151. + "\n"
  152. )
  153. with open(pubs_path, "w+") as f:
  154. f.write(pubs_texts)
  155. f.close()
  156. # gen content/basic_usage.md
  157. pubs_path = os.path.join(
  158. os.path.dirname(os.path.dirname(__file__)),
  159. "content",
  160. "basic_usage.md",
  161. )
  162. pubs_texts = (
  163. """---
  164. layout: "contents"
  165. title: Basic Usage
  166. firstpage:
  167. ---\n"""
  168. + sections[3]
  169. + "\n"
  170. )
  171. with open(pubs_path, "w+") as f:
  172. f.write(pubs_texts)
  173. f.close()