generate-template-param-doc-pages.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. For each parameter which can be found in the config file,
  3. create a markdown file with a templated content if it does not exist yet.
  4. The files are grouped in sub folders representing the config sections.
  5. """
  6. import os
  7. import configparser
  8. import urllib.request
  9. configFileUrl = "https://raw.githubusercontent.com/jomjol/AI-on-the-edge-device/rolling/sd-card/config/config.ini"
  10. parameterDocsFolder = "parameter-pages"
  11. parameterTemplateFile = "./templates/parameter.md"
  12. expertParameterListFile = "./expert-params.txt"
  13. hiddenInUiParameterListFile = "./hidden-in-ui.txt"
  14. # Fetch default config file from URL
  15. print("Fetching %r..." % configFileUrl)
  16. with urllib.request.urlopen(configFileUrl) as response:
  17. content = response.read().decode("utf-8")
  18. lines = str(content).split("\n")
  19. for l in range(len(lines)):
  20. lines[l] = lines[l].strip() + "\n"
  21. if lines[l][0] == ";":
  22. lines[l] = lines[l][1:] # Remove comment
  23. content = "".join(lines)
  24. # Fetch list of expert parameters
  25. with open(expertParameterListFile) as f:
  26. expertParameters = f.read().splitlines()
  27. # Fetch list of parameters not available through the UI
  28. with open(hiddenInUiParameterListFile) as f:
  29. hiddenInUiParameters = f.read().splitlines()
  30. config = configparser.ConfigParser(allow_no_value=True)
  31. config.optionxform = str # Make it case-insensitive
  32. config.read_string(content)
  33. #shutil.rmtree(parameterDocsFolder)
  34. if not os.path.exists(parameterDocsFolder):
  35. os.mkdir(parameterDocsFolder)
  36. with open(parameterTemplateFile, 'r') as parameterTemplateFileHandle:
  37. parameterTemplate = parameterTemplateFileHandle.read()
  38. print("For each section/parameter, check if there is already a documentation page in the folder %r..." % (os.getcwd() + "/" + parameterDocsFolder))
  39. for section in config:
  40. if section != "DEFAULT":
  41. #print(section)
  42. subFolder = parameterDocsFolder + "/" + section
  43. if not os.path.exists(subFolder):
  44. os.mkdir(subFolder)
  45. for parameter in config[section]:
  46. if not " " in parameter: # Ignore parameters with whitespaces in them (special format, not part of editable config)
  47. value = config[section][parameter]
  48. #print(" %s = %s" % (parameter, value))
  49. if "main." in parameter:
  50. parameter = parameter.replace("main.", "NUMBER.")
  51. """
  52. For each config line, create a markdown file
  53. """
  54. parameterDocFile = subFolder + '/' + parameter + ".md"
  55. if not os.path.exists(parameterDocFile): # File does not exist yet, generate template
  56. print("%r does not exit yet, generating a templated file for it" % (os.getcwd() + "/" + parameterDocFile))
  57. with open(parameterDocFile, 'w') as paramFileHandle:
  58. content = parameterTemplate
  59. content = content.replace("$NAME", parameter)
  60. content = content.replace("$DEFAULT", value)
  61. if parameter in expertParameters:
  62. content = content.replace("$EXPERT_PARAMETER", "!!! Warning\n This is an **Expert Parameter**! Only change it if you understand what it does!") # Note: Needs a 4 whitespace Intent!
  63. else:
  64. content = content.replace("$EXPERT_PARAMETER", "")
  65. if parameter in hiddenInUiParameters:
  66. content = content.replace("$HIDDEN_IN_UI", "!!! Note\n This parameter is not accessible through the Web Interface Configuration Page!") # Note: Needs a 4 whitespace Intent!
  67. else:
  68. content = content.replace("$HIDDEN_IN_UI", "")
  69. paramFileHandle.write(content)