class Day:
"""Individual days defined by their dates."""
- def __init__(self, date:str):
+ def __init__(self, date: str):
self.date = date
self.datetime = datetime.strptime(date, DATE_FORMAT)
"""Return what weekday matches self.date."""
return self.datetime.strftime('%A')
- def __eq__(self, other:object):
+ def __eq__(self, other: object):
return isinstance(other, self.__class__) and self.date == other.date
- def __lt__(self, other:Day):
+ def __lt__(self, other):
return self.date < other.date
"""To identify Exceptions based on expected (if faulty) user behavior."""
+class TaskServer(HTTPServer):
+ """Variant of HTTPServer that knows .jinja as Jinja Environment."""
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(self, *args, **kwargs)
+ self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
+
+
class TaskHandler(BaseHTTPRequestHandler):
"""Handles single HTTP request."""
+ server: TaskServer
- def send_html(self, html:str, code:int=200):
+ def send_html(self, html: str, code: int = 200):
"""Send HTML as proper HTTP response."""
self.send_response(code)
self.end_headers()
self.wfile.write(bytes(html, 'utf-8'))
- def send_msg(self, msg:str, code:int=400):
+ def send_msg(self, msg: str, code: int = 400):
"""Send message in HTML formatting as HTTP response."""
html = self.server.jinja.get_template('msg.html').render(msg=msg)
self.send_html(html, code)
def main():
"""Main loop."""
- server = HTTPServer(('localhost', HTTP_PORT), TaskHandler)
- server.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
+ server = TaskServer(('localhost', HTTP_PORT), TaskHandler)
print(f'running at port {HTTP_PORT}')
try:
server.serve_forever()