home · contact · privacy
Improve type hints sufficiently for mypy not to complain, and for Day.__eq__ check...
authorChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 23:56:26 +0000 (00:56 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 23:56:26 +0000 (00:56 +0100)
days.py
task.py

diff --git a/days.py b/days.py
index 42fc4c15280f616f0ee637503a4c98319e40d2c6..8db9f15b7362ede61c70265e4d5eb8a4c2c0c626 100644 (file)
--- a/days.py
+++ b/days.py
@@ -8,7 +8,7 @@ DATE_FORMAT = '%Y-%m-%d'
 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)
 
@@ -17,8 +17,8 @@ class Day:
         """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
diff --git a/task.py b/task.py
index 4774c8c51597c934b73be4462b2a1d2688ab0b03..963143463bfcbba7125415ed1aa025f19adbb9a4 100755 (executable)
--- a/task.py
+++ b/task.py
@@ -15,16 +15,25 @@ class HandledException(Exception):
     """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)
@@ -52,8 +61,7 @@ class TaskHandler(BaseHTTPRequestHandler):
 
 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()