Skip to content

Commit

Permalink
web_reboot eval for pyhtml file
Browse files Browse the repository at this point in the history
  • Loading branch information
krayon committed Feb 24, 2022
1 parent f41ef1c commit 3388632
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pyboard.py --device /dev/ttyUSB0 -f cp \
config.json app.py globs.py ui.py utils.py \
debug.py config.py main.py boot.py \
web_server.py index.py web_nn.css \
web_reboot.py \
web_reboot.py web_reboot.pyhtml \
:
```

Expand Down
2 changes: 1 addition & 1 deletion web_reboot.SOURCE.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon.16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.32x32.png">
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>""" + title + """</title>
<title><?py title ?></title>
<link rel="stylesheet" type="text/css" href="web_nn.css">
</head>
<body>
Expand Down
66 changes: 61 additions & 5 deletions web_reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,74 @@ def web_reboot(size_buf, offset, vardict, body): #{
return "", -1;
#}

def __DEFAULT_PAGE(title): #{
return """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>""" + title + """</title><link rel="stylesheet" type="text/css" href="web_nn.css"></head><body><h1>Rebooting in 5 seconds...</h1></body></html>""";
#}

# Returns:
# <s:buffer>, <i:next_offset>
def GET(size_buf, offset, vardict, body): #{
print("web_reboot.GET(size_buf:%d, offset:%d, vardict, body)" % (size_buf, offset));

buf = '';
read = 0;

title = globs.prod + " " + globs.desc;

return "HTTP/1.0 200 OK\nContent-Type: text/html\n\n" + __DEFAULT_PAGE(title), -1;
try: #{
f = open('web_reboot.pyhtml', 'rb');
except: #}{
print('[ERR ] Template not found');
return "HTTP/1.0 404 Not Found\n\n", -1;
#}

output = '';
if (offset <= 0): #{
output = "HTTP/1.0 200 OK\nContent-Type: text/html\n\n";
else: #}{
f.seek(offset);
#}

# TODO: Handle if we only get half a encoded tag better ("<?py ... ?>")
# For now, we skip the tag :facepalm:

code_s = 99;
code_e = -1;
while (code_s >= 0 and code_e < 0): #{
offset = offset + read;
buf = f.read(size_buf - len(output));
buf = buf.decode();
read = len(buf);
if (read == 0): #{
# We're done
f.close(); f = None;
return output, -1;
#}
code_s = buf.find('<?py');
code_e = buf.find('?>');
#}

# If there's no "<?py", just put it straight through
if (code_s < 0): #{
f.close(); f = None;
return output + buf, offset + read;
#}

# If there's a "<?py" later in the string, output everything up to that
if (code_s > 0): #{
f.close(); f = None;
print('Breaking at py tag');
return output + buf[:code_s], offset + code_s;
#}

# Starting with code tag
#offset = offset + code_e;
buf = buf[4:code_e].strip();
code_s = None;
#code_e = None;
read = 0;

print("Processing py tag:|" + buf + "|");
buf = eval(buf, {'title': title});

f.close(); f = None;
return buf, offset + code_e + 2;
#}

# Returns:
Expand Down
1 change: 1 addition & 0 deletions web_reboot.pyhtml

0 comments on commit 3388632

Please sign in to comment.