home · contact · privacy
Re-structure whole code into more package-typical file modularity.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 00:43:45 +0000 (01:43 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 00:43:45 +0000 (01:43 +0100)
plomtask/__init__.py [new file with mode: 0644]
plomtask/days.py
plomtask/http.py [new file with mode: 0644]
plomtask/misc.py [new file with mode: 0644]
plomtask/task.py [deleted file]
run.py [new file with mode: 0755]
scripts/pre-commit

diff --git a/plomtask/__init__.py b/plomtask/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
index 8db9f15b7362ede61c70265e4d5eb8a4c2c0c626..071b0b1b27d76b676d02ee4d4530563a5ba547b5 100644 (file)
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
 """Collecting Day and date-related items."""
 from datetime import datetime
 
diff --git a/plomtask/http.py b/plomtask/http.py
new file mode 100644 (file)
index 0000000..3fde34b
--- /dev/null
@@ -0,0 +1,54 @@
+"""plom's task manager"""
+from http.server import BaseHTTPRequestHandler
+from http.server import HTTPServer
+from urllib.parse import urlparse
+from os.path import split as path_split
+from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
+from plomtask.days import Day
+from plomtask.misc import HandledException
+
+TEMPLATES_DIR = 'templates'
+
+
+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):
+        """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):
+        """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 do_GET(self):
+        """Handle any GET request."""
+        try:
+            parsed_url = urlparse(self.path)
+            site = path_split(parsed_url.path)[1]
+            if 'calendar' == site:
+                html = self.do_GET_calendar()
+            else:
+                raise HandledException('Test!')
+            self.send_html(html)
+        except HandledException as error:
+            self.send_msg(error)
+
+    def do_GET_calendar(self):
+        """Show sorted Days."""
+        days = [Day('2024-01-03'), Day('2024-01-01'), Day('2024-01-02')]
+        days.sort()
+        return self.server.jinja.get_template('calendar.html').render(
+                days=days)
diff --git a/plomtask/misc.py b/plomtask/misc.py
new file mode 100644 (file)
index 0000000..1b780e2
--- /dev/null
@@ -0,0 +1,8 @@
+"""
+Whatever fits nowhere else, and/or is too small/trivial
+to merit its own module at this point.
+"""
+
+
+class HandledException(Exception):
+    """To identify Exceptions based on expected (if faulty) user behavior."""
diff --git a/plomtask/task.py b/plomtask/task.py
deleted file mode 100755 (executable)
index 49ca883..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-#!/usr/bin/env python3
-"""plom's task manager"""
-from http.server import BaseHTTPRequestHandler
-from http.server import HTTPServer
-from urllib.parse import urlparse
-from os.path import split as path_split
-from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
-from .days import Day
-
-HTTP_PORT = 8082
-TEMPLATES_DIR = 'templates'
-
-
-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):
-        """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):
-        """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 do_GET(self):
-        """Handle any GET request."""
-        try:
-            parsed_url = urlparse(self.path)
-            site = path_split(parsed_url.path)[1]
-            if 'calendar' == site:
-                html = self.do_GET_calendar()
-            else:
-                raise HandledException('Test!')
-            self.send_html(html)
-        except HandledException as error:
-            self.send_msg(error)
-
-    def do_GET_calendar(self):
-        """Show sorted Days."""
-        days = [Day('2024-01-03'), Day('2024-01-01'), Day('2024-01-02')]
-        days.sort()
-        return self.server.jinja.get_template('calendar.html').render(
-                days=days)
-
-
-def main():
-    """Main loop."""
-    server = TaskServer(('localhost', HTTP_PORT), TaskHandler)
-    print(f'running at port {HTTP_PORT}')
-    try:
-        server.serve_forever()
-    except KeyboardInterrupt:
-        print('aborting due to keyboard interrupt')
-    server.server_close()
-
-
-if __name__ == '__main__':
-    from sys import exit as sys_exit
-    try:
-        main()
-    except HandledException as e:
-        print(f'Aborting due to: {e}')
-        sys_exit(1)
diff --git a/run.py b/run.py
new file mode 100755 (executable)
index 0000000..644fc4a
--- /dev/null
+++ b/run.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+"""Call this to start the application."""
+from sys import exit as sys_exit
+from plomtask.misc import HandledException
+from plomtask.http import TaskHandler, TaskServer
+
+HTTP_PORT = 8082
+
+
+if __name__ == '__main__':
+    try:
+        server = TaskServer(('localhost', HTTP_PORT), TaskHandler)
+        print(f'running at port {HTTP_PORT}')
+        try:
+            server.serve_forever()
+        except KeyboardInterrupt:
+            print('aborting due to keyboard interrupt')
+        server.server_close()
+    except HandledException as e:
+        print(f'Aborting due to: {e}')
+        sys_exit(1)
index 813455cac8cd690224f6b48862b90a56c95bfe1e..cab455311fff30eb454474b3ee840a28222bb0eb 100755 (executable)
@@ -1,10 +1,13 @@
 #!/bin/sh
 set -e
-python3 -m mypy plomtask/*.py
-python3 -m flake8 plomtask/*.py
-python3 -m pylint plomtask/*.py
-python3 -m mypy tests/*.py
-python3 -m flake8 tests/*.py
-python3 -m pylint tests/*.py
+for dir in $(echo '.' 'plomtask' 'tests'); do
+    echo "Running mypy on ${dir}/ …."
+    python3 -m mypy ${dir}/*.py
+    echo "Running flake8 on ${dir}/ …"
+    python3 -m flake8 ${dir}/*.py
+    echo "Running pylint on ${dir}/ …"
+    python3 -m pylint ${dir}/*.py
+done
+echo "Running unittest on tests/."
 python3 -m unittest tests/*.py
 exit 0