From 6aedb3877c015fbbc6be14cf0eac7b8e6c6534b2 Mon Sep 17 00:00:00 2001 From: John Little Date: Sun, 10 Jan 2016 15:50:41 +0000 Subject: [PATCH 1/7] 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() From 8ac01ad7c03cdc4000cb5f1987b608d1ba8d3247 Mon Sep 17 00:00:00 2001 From: John Little Date: Sun, 10 Jan 2016 16:00:59 +0000 Subject: [PATCH 2/7] changed the path to lowercase --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index a8aee1fb..84762030 100644 --- a/server.py +++ b/server.py @@ -4,7 +4,7 @@ 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") +base_path = os.path.join(os.getcwd(),"esp8266","data") class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): From 3d0753b1f9e007fe3439ce2daa38d2cfca3fc449 Mon Sep 17 00:00:00 2001 From: John Little Date: Sun, 10 Jan 2016 20:52:07 +0000 Subject: [PATCH 3/7] Ability to process tags stored in a json file Case sensitive tags are stored in data/tags.json. For now, this is what I have in mine { "WEB_ADDRESS":"localhost", "PAGE_TITLE":"Testing things..." } --- server.py | 69 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/server.py b/server.py index 84762030..2013314b 100644 --- a/server.py +++ b/server.py @@ -1,7 +1,10 @@ #!/usr/bin/env python -import sys, os import SimpleHTTPServer, SocketServer +import sys +import os +import json +import re #Replace this with a different path if you need to... base_path = os.path.join(os.getcwd(),"esp8266","data") @@ -13,44 +16,52 @@ class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): self.send_header("Content-type", "text/html") self.end_headers() - data = self.process(self.path) + data = self.process_tpl(self.path) self.wfile.write(data) self.wfile.close() return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) - def process(self,fn): + def process_tpl(self,fn): + p = re.compile('\$(.*?)\$') 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) + fn = os.path.join(base_path,fn) + data = open(fn).read() + + fn_json = os.path.join(base_path,"tags.json") + if os.path.exists(fn_json): + json_dic = json.loads(open(fn_json).read()) + else: + json_dic = {} + + tags = p.findall(data) + i = 0 + n_tags = len(tags) + while i < n_tags: + dd = self.process_tag(data,tags[i],json_dic) + if dd != data: + data = dd + tags = p.findall(data) + n = len(tags) else: i = i+1 - return "\n".join(lines) + return data + + def process_tag(self, data, tag, json_dic={}): + print " processing $%s$" % tag + if tag in json_dic.keys(): + data = data.replace("$"+tag+"$",json_dic[tag]) + elif tag.startswith("INCLUDE[") and tag.endswith("]"): + fn = tag[8:-1] + fn = os.path.join(base_path,fn) + d = open(fn).read() + p0 = data.find("$"+tag) + p1 = data.find("]",p0)+2 + data = data[:p0]+d+data[p1:] + + return data if __name__ == '__main__': print "="*60 From 70a7ccb0835676ba4b6b84d78489cfd92c851c34 Mon Sep 17 00:00:00 2001 From: John Little Date: Sun, 10 Jan 2016 20:54:03 +0000 Subject: [PATCH 4/7] Created some sample json tags --- esp8266/data/tags.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 esp8266/data/tags.json diff --git a/esp8266/data/tags.json b/esp8266/data/tags.json new file mode 100644 index 00000000..5aebc12c --- /dev/null +++ b/esp8266/data/tags.json @@ -0,0 +1,4 @@ +{ + "WEB_ADDRESS":"localhost", + "PAGE_TITLE":"Testing things..." +} From 8320040f213f5a2196f28f4e439b65852fd0c70b Mon Sep 17 00:00:00 2001 From: John Little Date: Mon, 11 Jan 2016 12:24:18 +0000 Subject: [PATCH 5/7] moved the server script to tools Updated base_path to reflect the change. --- server.py => tools/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename server.py => tools/server.py (97%) diff --git a/server.py b/tools/server.py similarity index 97% rename from server.py rename to tools/server.py index 2013314b..a4d9abed 100644 --- a/server.py +++ b/tools/server.py @@ -7,7 +7,7 @@ import json import re #Replace this with a different path if you need to... -base_path = os.path.join(os.getcwd(),"esp8266","data") +base_path = os.path.join(os.getcwd(),"..","esp8266","data") class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): From 518fb0f23849a3f812e3023997961432ccd67307 Mon Sep 17 00:00:00 2001 From: John Little Date: Tue, 12 Jan 2016 20:36:56 +0000 Subject: [PATCH 6/7] Added correct handling of the menu links --- tools/server.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tools/server.py b/tools/server.py index a4d9abed..df6021ce 100644 --- a/tools/server.py +++ b/tools/server.py @@ -8,20 +8,44 @@ import re #Replace this with a different path if you need to... base_path = os.path.join(os.getcwd(),"..","esp8266","data") +base_path = os.path.join(os.getcwd(),"data") class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): - if self.path.endswith(".tpl"): + is_tpl, s = self.process_path(self.path) + if is_tpl: self.send_response(301) self.send_header("Content-type", "text/html") self.end_headers() - data = self.process_tpl(self.path) + data = self.process_tpl(s) self.wfile.write(data) self.wfile.close() return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) + def process_path(self,s): + #A template link is all caps and is associated to a lower().tpl file in base_path + if s == "/": + return True,"home.tpl" + + ret = False,"" + s = s.replace("/","") + if s.endswith(".tpl"): + return True,s + + s = s.lower()+".tpl" + + #these do not exactly match, so let's make them! + s = s.replace("configsta","config_sta") + s = s.replace("configap","config_ap") + s = s.replace("configsys","system") + + if os.path.exists(os.path.join(base_path,s)): + ret = True,s + + return ret + def process_tpl(self,fn): p = re.compile('\$(.*?)\$') if fn.startswith("/") or fn.startswith("\\"): @@ -44,7 +68,7 @@ class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): if dd != data: data = dd tags = p.findall(data) - n = len(tags) + n_tags = len(tags) else: i = i+1 return data @@ -72,3 +96,4 @@ if __name__ == '__main__': handler = MyHandler server = SocketServer.TCPServer(("",8080), handler) server.serve_forever() + From b87efcf6fb83cf48879d3c5eafdb24c15ae06b00 Mon Sep 17 00:00:00 2001 From: John Little Date: Tue, 12 Jan 2016 20:39:29 +0000 Subject: [PATCH 7/7] updated base_path --- tools/server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/server.py b/tools/server.py index df6021ce..eeb970ce 100644 --- a/tools/server.py +++ b/tools/server.py @@ -8,7 +8,6 @@ import re #Replace this with a different path if you need to... base_path = os.path.join(os.getcwd(),"..","esp8266","data") -base_path = os.path.join(os.getcwd(),"data") class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self):