1 from http.server import BaseHTTPRequestHandler, HTTPServer
7 def build_page(entries):
16 <form action="/" method="POST">
18 <input type="submit" name="energy ---" value="---" />
19 <input type="submit" name="energy --" value="--" />
20 <input type="submit" name="energy -" value="-" />
21 <input type="submit" name="energy +" value="+" />
22 <input type="submit" name="energy ++" value="++" />
23 <input type="submit" name="energy +++" value="+++" />
25 <input name="comment" type="text" />
27 <input name="backdate" type="number" value="0" min="0" step="5" />
34 <tr><th>datetime</th><th>energy level</th><th>comment</th></tr>
46 db_name = "energy_tracker"
47 self.db_file = db_name + ".json"
48 self.lock_file = db_name+ ".lock"
50 if os.path.exists(self.db_file):
51 with open(self.db_file, "r") as f:
52 self.from_dict(json.load(f))
54 def from_dict(self, d):
62 if os.path.exists(self.lock_file):
63 raise LockFileDetected
64 if os.path.exists(self.db_file):
65 shutil.copy(self.db_file, self.db_file + ".bak")
66 f = open(self.lock_file, "w+")
68 with open(self.db_file, "w") as f:
69 json.dump(self.to_dict(), f)
70 os.remove(self.lock_file)
74 class MyServer(BaseHTTPRequestHandler):
78 from urllib.parse import parse_qs
79 length = int(self.headers['content-length'])
80 postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
82 for val in ('---', '--', '-', '+', '++', '+++'):
83 val_name = 'energy ' + val
84 if val_name.encode() in postvars.keys():
85 time = datetime.datetime.now() - datetime.timedelta(minutes=int(postvars[b'backdate'][0].decode()))
86 db.entries[str(time)[:19]] = [val, postvars[b'comment'][0].decode()]
90 self.send_response(302)
91 self.send_header('Location', '/')
93 except LockFileDetected:
94 self.send_response(400)
96 self.wfile.write(bytes("Sorry, lock file!", "utf-8"))
99 self.send_response(200)
100 self.send_header("Content-type", "text/html")
104 for datetime, data in db.entries.items():
105 entries = "<tr><td>%s</td><td class=\"energy_cell\">%s</td><td>%s</td></tr>\n" % (datetime, data[0], data[1]) + entries
106 page = build_page(entries)
107 self.wfile.write(bytes(page, "utf-8"))
111 hostName = "localhost"
113 if __name__ == "__main__":
114 webServer = HTTPServer((hostName, serverPort), MyServer)
115 print("Server started http://%s:%s" % (hostName, serverPort))
117 webServer.serve_forever()
118 except KeyboardInterrupt:
120 webServer.server_close()
121 print("Server stopped.")