generate_config_docs.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import re
  2. import tomli
  3. class DocGenerator:
  4. def __init__(self, filename):
  5. self.doc = []
  6. self.current_section = ""
  7. self.current_comment = []
  8. self.current_field_name = ""
  9. self.current_field_value = []
  10. self.current_field_unset = False
  11. self.filename = filename
  12. def write(self):
  13. with open("../CONFIG.md", "w") as text_file:
  14. text_file.write("# PgCat Configurations \n")
  15. for entry in self.doc:
  16. if entry["name"] == "__section__":
  17. text_file.write("## `" + entry["section"] + "` Section" + "\n")
  18. text_file.write("\n")
  19. continue
  20. text_file.write("### " + entry["name"]+ "\n")
  21. text_file.write("```"+ "\n")
  22. text_file.write("path: " + entry["fqdn"]+ "\n")
  23. text_file.write("default: " + entry["defaults"].strip()+ "\n")
  24. if entry["example"] is not None:
  25. text_file.write("example: " + entry["example"].strip()+ "\n")
  26. text_file.write("```"+ "\n")
  27. text_file.write("\n")
  28. text_file.write(entry["comment"]+ "\n")
  29. text_file.write("\n")
  30. def save_entry(self):
  31. if len(self.current_field_name) == 0:
  32. return
  33. if len(self.current_comment) == 0:
  34. return
  35. self.current_section = self.current_section.replace("sharded_db", "<pool_name>")
  36. self.current_section = self.current_section.replace("simple_db", "<pool_name>")
  37. self.current_section = self.current_section.replace("users.0", "users.<user_index>")
  38. self.current_section = self.current_section.replace("users.1", "users.<user_index>")
  39. self.current_section = self.current_section.replace("shards.0", "shards.<shard_index>")
  40. self.current_section = self.current_section.replace("shards.1", "shards.<shard_index>")
  41. self.doc.append(
  42. {
  43. "name": self.current_field_name,
  44. "fqdn": self.current_section + "." + self.current_field_name,
  45. "section": self.current_section,
  46. "comment": "\n".join(self.current_comment),
  47. "defaults": self.current_field_value if not self.current_field_unset else "<UNSET>",
  48. "example": self.current_field_value if self.current_field_unset else None
  49. }
  50. )
  51. self.current_comment = []
  52. self.current_field_name = ""
  53. self.current_field_value = []
  54. def parse(self):
  55. with open("../pgcat.toml", "r") as f:
  56. for line in f.readlines():
  57. line = line.strip()
  58. if len(line) == 0:
  59. self.save_entry()
  60. if line.startswith("["):
  61. self.current_section = line[1:-1]
  62. self.current_field_name = "__section__"
  63. self.current_field_unset = False
  64. self.save_entry()
  65. elif line.startswith("#"):
  66. results = re.search("^#\s*([A-Za-z0-9_]+)\s*=(.+)$", line)
  67. if results is not None:
  68. self.current_field_name = results.group(1)
  69. self.current_field_value = results.group(2)
  70. self.current_field_unset = True
  71. self.save_entry()
  72. else:
  73. self.current_comment.append(line[1:].strip())
  74. else:
  75. results = re.search("^\s*([A-Za-z0-9_]+)\s*=(.+)$", line)
  76. if results is None:
  77. continue
  78. self.current_field_name = results.group(1)
  79. self.current_field_value = results.group(2)
  80. self.current_field_unset = False
  81. self.save_entry()
  82. self.save_entry()
  83. return self
  84. DocGenerator("../pgcat.toml").parse().write()