Generate-Assets.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/python3
  2. # 使用此脚本将data/html下的代码转换成写死的Controller
  3. import os
  4. import sys
  5. import datetime
  6. if __name__ == "__main__":
  7. if not os.path.exists("../controller/auto-generated"):
  8. os.mkdir("../controller/auto-generated")
  9. if len(sys.argv) != 2:
  10. print("usage: Generate-Assets <AssetFilePath>")
  11. exit(-1)
  12. pass
  13. assetPath = sys.argv[1]
  14. with open("html/" + assetPath, "r", encoding="utf-8") as f:
  15. fileTexts = []
  16. for it in f.readlines():
  17. lineText = it.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "").replace("\'", "\\'").replace("\"", "\\\"")
  18. lineText = " string(\"" + lineText + "\")"
  19. fileTexts.append(lineText)
  20. fileText = " + \n".join(fileTexts)
  21. if len(fileTexts) == 0:
  22. fileText = "\"\""
  23. writeToFilePath = "../controller/auto-generated/" + assetPath.replace("/", "-") + ".cpp"
  24. print("Writing to " + writeToFilePath)
  25. with open(writeToFilePath, "w+", encoding="utf-8") as w:
  26. w.write("//\n")
  27. w.write("// Created by xcbosa on " + str(datetime.date.today()) + "\n")
  28. w.write("//\n")
  29. w.write("\n")
  30. w.write("#include \"../../processor/processor.h\"\n")
  31. w.write("#include \"../../utils/utils.h\"\n")
  32. w.write("#include \"../../webuiconf.h\"\n")
  33. w.write("\n")
  34. w.write("using namespace std;\n")
  35. w.write("using namespace xc::processor;\n")
  36. w.write("using namespace xc::utils;\n")
  37. w.write("using namespace xc::processor::templates;\n")
  38. w.write("\n")
  39. w.write("namespace xc::controller {\n")
  40. w.write("\n")
  41. w.write(" static string ControllerPath = \"/" + assetPath + "\";\n\n")
  42. w.write(" static ResponseData *controllerResponse(RequestData request) {\n")
  43. w.write(" return new TextResponseData(200,\n")
  44. w.write(fileText + "\n")
  45. w.write(" , mimeTypeOfFile(ControllerPath));\n")
  46. w.write(" }\n\n")
  47. w.write(" ContentGeneratorDefineS(request.getURLPath() == ControllerPath, controllerResponse(request))\n")
  48. w.write("\n")
  49. w.write("}\n")