mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-08-14 15:35:53 +08:00
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..." }
This commit is contained in:
parent
8ac01ad7c0
commit
3d0753b1f9
69
server.py
69
server.py
@ -1,7 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, os
|
|
||||||
import SimpleHTTPServer, SocketServer
|
import SimpleHTTPServer, SocketServer
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
#Replace this with a different path if you need to...
|
#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")
|
||||||
@ -13,44 +16,52 @@ class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|||||||
self.send_header("Content-type", "text/html")
|
self.send_header("Content-type", "text/html")
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
data = self.process(self.path)
|
data = self.process_tpl(self.path)
|
||||||
self.wfile.write(data)
|
self.wfile.write(data)
|
||||||
self.wfile.close()
|
self.wfile.close()
|
||||||
return
|
return
|
||||||
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
||||||
|
|
||||||
def process(self,fn):
|
def process_tpl(self,fn):
|
||||||
|
p = re.compile('\$(.*?)\$')
|
||||||
if fn.startswith("/") or fn.startswith("\\"):
|
if fn.startswith("/") or fn.startswith("\\"):
|
||||||
fn = fn[1:]
|
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()
|
fn = os.path.join(base_path,fn)
|
||||||
lines_inc = lines_inc.split("\n")
|
data = open(fn).read()
|
||||||
if i < n_lines-1:
|
|
||||||
lines1 = lines[i+1:]
|
fn_json = os.path.join(base_path,"tags.json")
|
||||||
else:
|
if os.path.exists(fn_json):
|
||||||
lines1 = []
|
json_dic = json.loads(open(fn_json).read())
|
||||||
lines = lines[:i]+lines_inc+lines1
|
else:
|
||||||
n_lines = len(lines)
|
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:
|
else:
|
||||||
i = i+1
|
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__':
|
if __name__ == '__main__':
|
||||||
print "="*60
|
print "="*60
|
||||||
|
Loading…
x
Reference in New Issue
Block a user