From: Christian Heller Date: Sun, 17 Mar 2024 00:43:45 +0000 (+0100) Subject: Re-structure whole code into more package-typical file modularity. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/ledger2?a=commitdiff_plain;h=b557c789f4eec704db0e6276390395fac5d8db9e;p=plomtask Re-structure whole code into more package-typical file modularity. --- diff --git a/plomtask/__init__.py b/plomtask/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plomtask/days.py b/plomtask/days.py index 8db9f15..071b0b1 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -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 index 0000000..3fde34b --- /dev/null +++ b/plomtask/http.py @@ -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 index 0000000..1b780e2 --- /dev/null +++ b/plomtask/misc.py @@ -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 index 49ca883..0000000 --- a/plomtask/task.py +++ /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 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) diff --git a/scripts/pre-commit b/scripts/pre-commit index 813455c..cab4553 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -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