Skip to content

Commit

Permalink
pyhtml functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
krayon committed Feb 25, 2022
1 parent 3388632 commit c1ff85c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 164 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ to transfer the *NightyNight* python files directly to the device. eg.
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_server.py web_pythml.py web_nn.css \
index.py index.pyhtml \
web_reboot.py web_reboot.pyhtml \
favicon.ico favicon.16x16.png favicon.32x32.png \

:
```

Expand Down
2 changes: 1 addition & 1 deletion globs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
prod = 'Quadronyx NightyNight (D1 uPython Edition)';
url = 'https://github.com/krayon/nightynight/';
desc = '(Gold)';
ver = '2.0.4';
ver = '2.0.5';

# vim:ts=4:tw=80:sw=4:et:ai:si
11 changes: 5 additions & 6 deletions index.SOURCE.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
<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>
<div class="mainTables">
<table class="mainTables">
<tr>
<td rowspan="6">&nbsp;</td>
<td colspan="2">""" + title + """</td>
<td colspan="2"><?py title ?></td>
<td rowspan="6">&nbsp;</td>
</tr>

<tr>
<td>Device Unique ID:</td>
<td class="ro"><pre>""" + uid + """</pre></td>
<td class="ro"><pre><?py uid ?></pre></td>
</tr>
<tr>
<td>MAC Address:</td>
<td class="ro"><pre>""" + mac + """</pre></td>
<td class="ro"><pre><?py mac ?></pre></td>
</tr>

<tr>
Expand All @@ -40,9 +40,8 @@
</tr>

<tr>
<td class="byline ro" colspan=4><em>""" + ver + """</em></td>
<td class="byline ro" colspan=4><em><?py ver ?></em></td>
</tr>

</table>
</div>
</body>
Expand Down
30 changes: 0 additions & 30 deletions index.SOURCE.py

This file was deleted.

40 changes: 19 additions & 21 deletions index.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import os;
import globs;

# Returns:
# <s:buffer>, <i:next_offset>
def index(size_buf, offset, vardict, body): #{
print("index.index(size_buf:%d, offset:%d, vardict, body)" % (size_buf, offset));
return "", -1;
#}

# Returns:
# <s:buffer>
def __DEFAULT_PAGE(title, ver, uid, mac): #{
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><div class="mainTables"><table class="mainTables"><tr><td rowspan="6">&nbsp;</td><td colspan="2">""" + title + """</td><td rowspan="6">&nbsp;</td></tr><tr><td>Device Unique ID:</td><td class="ro"><pre>""" + uid + """</pre></td></tr><tr><td>MAC Address:</td><td class="ro"><pre>""" + mac + """</pre></td></tr><tr><td colspan=2>&nbsp;</td></tr><tr><td colspan=2><a href="web_reboot.py" >Reboot</a></td></tr><tr><td colspan=2>&nbsp;</td></tr><tr><td class="byline ro" colspan=4><em>""" + ver + """</em></td></tr></table></div></body></html>""";
#}
from web_pyhtml import pyhtml_parser;

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

buf = '';
read = 0;

# Variables
title = globs.prod + " " + globs.desc;
ver = '<a href="' + globs.url + '">' \
+ title + " " + globs.ver + "<br>\n" \
+ "on " + os.uname().machine + " (" + os.uname().sysname + ") " + "<br>\n" \
+ "uPython " + os.uname().version + " (" + os.uname().release + ")" \
+ '</a>';
uid = globs.uid;
mac = globs.mac;

return "HTTP/1.0 200 OK\n" + "Content-Type: text/html\n\n" + __DEFAULT_PAGE(title, ver, globs.uid, globs.mac), -1;
#}
tag, buf, read = pyhtml_parser('index.pyhtml', size_buf, offset, vardict, body);
if (tag): #{
print("Processing py tag:|" + buf + "|");
buf = eval(buf, {
'title': title
,'ver': ver
,'uid': uid
,'mac': mac
});
#}

### # Returns:
### # <s:buffer>, <i:next_offset>
### def endGET(size_buf, offset, vardict, body): #{
### print("index.endGET()");
###
### return "HTTP/1.0 200 OK\nContent-Type: text/html\n\n", -1;
### #}
return buf, read;
#}

# vim:ts=4:tw=80:sw=4:et:ai:si
5 changes: 5 additions & 0 deletions web_nn.SOURCE.css
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ div.mainTables {
font-style: italic;
}

.warning {
background-color:#ff0000;
font-style: italic;
}

.mainTables tr td.byline {
font-size: 8px;
text-align: left;
Expand Down
37 changes: 36 additions & 1 deletion web_reboot.SOURCE.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@
<link rel="stylesheet" type="text/css" href="web_nn.css">
</head>
<body>
<h1>Rebooting in 5 seconds...</h1>
<div class="mainTables">
<table class="mainTables">
<tr>
<td rowspan="6">&nbsp;</td>
<td colspan="2"><?py title ?></td>
<td rowspan="6">&nbsp;</td>
</tr>

<tr>
<td>Device Unique ID:</td>
<td class="ro"><pre><?py uid ?></pre></td>
</tr>
<tr>
<td>MAC Address:</td>
<td class="ro"><pre><?py mac ?></pre></td>
</tr>

<tr>
<td colspan=2>&nbsp;</td>
</tr>

<tr>
<td class="warning" colspan=2>
Rebooting in 5 seconds...
</td>
</tr>

<tr>
<td colspan=2>&nbsp;</td>
</tr>

<tr>
<td class="byline ro" colspan=4><em><?py ver ?></em></td>
</tr>
</table>
</div>
</body>
</html>
40 changes: 0 additions & 40 deletions web_reboot.SOURCE.py

This file was deleted.

85 changes: 21 additions & 64 deletions web_reboot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import os;
import globs;

# Returns:
# <s:buffer>, <i:next_offset>
def web_reboot(size_buf, offset, vardict, body): #{
print("web_reboot.web_reboot(size_buf:%d, offset:%d, vardict, body)" % (size_buf, offset));
return "", -1;
#}
from web_pyhtml import pyhtml_parser;

# Returns:
# <s:buffer>, <i:next_offset>
Expand All @@ -16,66 +11,28 @@ def GET(size_buf, offset, vardict, body): #{
buf = '';
read = 0;

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

try: #{
f = open('web_reboot.pyhtml', 'rb');
except: #}{
print('[ERR ] Template not found');
return "HTTP/1.0 404 Not Found\n\n", -1;
ver = '<a href="' + globs.url + '">' \
+ title + " " + globs.ver + "<br>\n" \
+ "on " + os.uname().machine + " (" + os.uname().sysname + ") " + "<br>\n" \
+ "uPython " + os.uname().version + " (" + os.uname().release + ")" \
+ '</a>';
uid = globs.uid;
mac = globs.mac;

tag, buf, read = pyhtml_parser('web_reboot.pyhtml', size_buf, offset, vardict, body);
if (tag): #{
print("Processing py tag:|" + buf + "|");
buf = eval(buf, {
'title': title
,'ver': ver
,'uid': uid
,'mac': mac
});
#}

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;
return buf, read;
#}

# Returns:
Expand All @@ -90,7 +47,7 @@ def endGET(size_buf, offset, vardict, body): #{

machine.reset();

return "HTTP/1.0 200 OK\nContent-Type: text/html\n\n", -1;
return '', -1;
#}

# vim:ts=4:tw=80:sw=4:et:ai:si

0 comments on commit c1ff85c

Please sign in to comment.