From 6aedb3877c015fbbc6be14cf0eac7b8e6c6534b2 Mon Sep 17 00:00:00 2001 From: John Little Date: Sun, 10 Jan 2016 15:50:41 +0000 Subject: [PATCH] Create server.py Very simple server to help working with the .tpl files stored in ESP8266/data Serves files on port 8080 For now, the server only understands $INCLUDE[filename.inc]$ type statements (with filename.inc in the same folder as the .tpl file) --- server.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 00000000..a8aee1fb --- /dev/null +++ b/server.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +import sys, os +import SimpleHTTPServer, SocketServer + +#Replace this with a different path if you need to... +base_path = os.path.join(os.getcwd(),"ESP8266","data") + +class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): + def do_GET(self): + if self.path.endswith(".tpl"): + self.send_response(301) + self.send_header("Content-type", "text/html") + self.end_headers() + + data = self.process(self.path) + self.wfile.write(data) + self.wfile.close() + return + SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + + def process(self,fn): + if fn.startswith("/") or fn.startswith("\\"): + fn = fn[1:] + fn = os.path.join(base_path,os.path.normpath(fn)) + fpath,_ = os.path.split(fn) + lines = open(fn).read() + lines = lines.split("\n") + n_lines = len(lines) + i = 0 + while i < n_lines: + line = lines[i].strip() + if line.startswith("$INCLUDE["): + p0 = line.find("[")+1 + p1 = line.find("]") + if p0 < 0 or p0 == len(line) or p1 < 0: + continue + fn_inc = os.path.join(fpath,line[p0:p1]) + if not os.path.exists(fn_inc): + i = i+1 + continue + + lines_inc = open(fn_inc).read() + lines_inc = lines_inc.split("\n") + if i < n_lines-1: + lines1 = lines[i+1:] + else: + lines1 = [] + lines = lines[:i]+lines_inc+lines1 + n_lines = len(lines) + else: + i = i+1 + return "\n".join(lines) + +if __name__ == '__main__': + print "="*60 + print "Serving files from:" + print base_path + os.chdir(base_path) + print "="*60 + handler = MyHandler + server = SocketServer.TCPServer(("",8080), handler) + server.serve_forever()