|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import SimpleHTTPServer, SocketServer |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import json |
| 7 | +import re |
| 8 | + |
| 9 | +#Replace this with a different path if you need to... |
| 10 | +base_path = os.path.join(os.getcwd(),"..","esp8266","data") |
| 11 | + |
| 12 | +class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
| 13 | + def do_GET(self): |
| 14 | + is_tpl, s = self.process_path(self.path) |
| 15 | + if is_tpl: |
| 16 | + self.send_response(301) |
| 17 | + self.send_header("Content-type", "text/html") |
| 18 | + self.end_headers() |
| 19 | + |
| 20 | + data = self.process_tpl(s) |
| 21 | + self.wfile.write(data) |
| 22 | + self.wfile.close() |
| 23 | + return |
| 24 | + SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) |
| 25 | + |
| 26 | + def process_path(self,s): |
| 27 | + #A template link is all caps and is associated to a lower().tpl file in base_path |
| 28 | + if s == "/": |
| 29 | + return True,"home.tpl" |
| 30 | + |
| 31 | + ret = False,"" |
| 32 | + s = s.replace("/","") |
| 33 | + if s.endswith(".tpl"): |
| 34 | + return True,s |
| 35 | + |
| 36 | + s = s.lower()+".tpl" |
| 37 | + |
| 38 | + #these do not exactly match, so let's make them! |
| 39 | + s = s.replace("configsta","config_sta") |
| 40 | + s = s.replace("configap","config_ap") |
| 41 | + s = s.replace("configsys","system") |
| 42 | + |
| 43 | + if os.path.exists(os.path.join(base_path,s)): |
| 44 | + ret = True,s |
| 45 | + |
| 46 | + return ret |
| 47 | + |
| 48 | + def process_tpl(self,fn): |
| 49 | + p = re.compile('\$(.*?)\$') |
| 50 | + if fn.startswith("/") or fn.startswith("\\"): |
| 51 | + fn = fn[1:] |
| 52 | + |
| 53 | + fn = os.path.join(base_path,fn) |
| 54 | + data = open(fn).read() |
| 55 | + |
| 56 | + fn_json = os.path.join(base_path,"tags.json") |
| 57 | + if os.path.exists(fn_json): |
| 58 | + json_dic = json.loads(open(fn_json).read()) |
| 59 | + else: |
| 60 | + json_dic = {} |
| 61 | + |
| 62 | + tags = p.findall(data) |
| 63 | + i = 0 |
| 64 | + n_tags = len(tags) |
| 65 | + while i < n_tags: |
| 66 | + dd = self.process_tag(data,tags[i],json_dic) |
| 67 | + if dd != data: |
| 68 | + data = dd |
| 69 | + tags = p.findall(data) |
| 70 | + n_tags = len(tags) |
| 71 | + else: |
| 72 | + i = i+1 |
| 73 | + return data |
| 74 | + |
| 75 | + def process_tag(self, data, tag, json_dic={}): |
| 76 | + print " processing $%s$" % tag |
| 77 | + if tag in json_dic.keys(): |
| 78 | + data = data.replace("$"+tag+"$",json_dic[tag]) |
| 79 | + elif tag.startswith("INCLUDE[") and tag.endswith("]"): |
| 80 | + fn = tag[8:-1] |
| 81 | + fn = os.path.join(base_path,fn) |
| 82 | + d = open(fn).read() |
| 83 | + p0 = data.find("$"+tag) |
| 84 | + p1 = data.find("]",p0)+2 |
| 85 | + data = data[:p0]+d+data[p1:] |
| 86 | + |
| 87 | + return data |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + print "="*60 |
| 91 | + print "Serving files from:" |
| 92 | + print base_path |
| 93 | + os.chdir(base_path) |
| 94 | + print "="*60 |
| 95 | + handler = MyHandler |
| 96 | + server = SocketServer.TCPServer(("",8080), handler) |
| 97 | + server.serve_forever() |
| 98 | + |
0 commit comments