ESP8266 SHT21 sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert-static.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. # convert-static.py
  3. #
  4. # This program reads the static html and js files and converts them into
  5. # minified strings defined in a C header file.
  6. #
  7. # ----------------------------------------------------------------------------
  8. # "THE BEER-WARE LICENSE" (Revision 42):
  9. # <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file. As long as
  10. # you retain this notice you can do whatever you want with this stuff. If we
  11. # meet some day, and you think this stuff is worth it, you can buy us a beer
  12. # in return. Thomas Buck & Christian Högerle
  13. # ----------------------------------------------------------------------------
  14. import binascii
  15. def fileToString(filename, js = False):
  16. f = open(filename, "r")
  17. sf = ""
  18. for s in f.readlines():
  19. st = s
  20. if js == True:
  21. ss = s.split("//", 1)
  22. if ss[0][-1:] == ':':
  23. st = ss[0] + '//' + ss[1]
  24. else:
  25. st = ss[0]
  26. sf += st
  27. return sf
  28. def minify(text, js = False):
  29. text = text.replace("\r", "")
  30. text = text.replace("\t", "")
  31. text = text.replace(" ", " ")
  32. if js == True:
  33. text = text.replace("\\", "\\\\")
  34. text = text.replace("\n", "\\n")
  35. else:
  36. text = text.replace("\n", " ")
  37. text = text.replace(" ", " ")
  38. text = text.replace(" ", " ")
  39. text = text.replace(" ", " ")
  40. text = text.replace(" ", " ")
  41. text = text.replace(" ", " ")
  42. text = text.replace("> <", "><")
  43. if js == True:
  44. text = text.replace("\\n ", "\\n")
  45. text = text.replace("\\n\\n", "\\n")
  46. if (text[-1:] == ' '):
  47. text = text[:-1]
  48. if (text[-2:] == '\\n'):
  49. text = text[:-2]
  50. if (text[0] == ' '):
  51. text = text[1:]
  52. if (text[0] == '\\') and (text[1] == 'n'):
  53. text = text[2:]
  54. return text
  55. def getBinaryFile(filename, id):
  56. f = open(filename, "rb")
  57. s = "const static unsigned char " + id + "[] PROGMEM = {\n";
  58. i = 0
  59. c = 0
  60. while True:
  61. d = f.read(1)
  62. if not d:
  63. break
  64. if i == 0:
  65. s += " "
  66. s += "0x" + binascii.hexlify(d).decode("utf-8") + ", "
  67. i += 1
  68. c += 1
  69. if i >= 8:
  70. i = 0
  71. s += "\n"
  72. if i == 0:
  73. s = s[:-3]
  74. else:
  75. s = s[:-2]
  76. s += "\n"
  77. s += "};"
  78. s = "const static unsigned int " + id + "Size = " + str(c) + ";\n" + "const static char faviconMimeType[] PROGMEM = \"image/x-icon\";\n" + s;
  79. return s
  80. def getAsDefine(name, text):
  81. return "#define " + name + " \"" + text.replace("\"", "\\\"") + "\""
  82. print("Preparing static.h output file...")
  83. f = open("static.h", "w")
  84. f.write("// !!DO NOT EDIT, AUTO-GENERATED FILE!!\n")
  85. f.write("// Use convert-static.py to recreate this.\n")
  86. f.write("\n")
  87. f.write("#ifndef __STATIC_H__\n")
  88. f.write("#define __STATIC_H__\n")
  89. f.write("\n")
  90. print("Processing template.html...")
  91. template = fileToString("template.html")
  92. template = minify(template)
  93. templates = template.split(" /* %% INSERT_CLIENT_LIST_HERE %% */ ")
  94. f.write(getAsDefine("HTML_BEGIN", templates[0]) + "\n")
  95. f.write(getAsDefine("HTML_END", templates[1]) + "\n")
  96. print("Processing client-script.js...")
  97. js = fileToString("client-script.js", True)
  98. js = minify(js, True)
  99. f.write(getAsDefine("JS_FILE", js) + "\n")
  100. print("Processing client-style.css...")
  101. css = fileToString("client-style.css", True)
  102. css = minify(css)
  103. f.write(getAsDefine("CSS_FILE", css) + "\n")
  104. f.write("\n")
  105. print("Processing favicon.ico...")
  106. f.write(getBinaryFile("favicon.ico", "favicon") + "\n")
  107. print("Done!")
  108. f.write("\n")
  109. f.write("#endif // __STATIC_H__\n")
  110. f.write("\n")
  111. f.close()