From b557c789f4eec704db0e6276390395fac5d8db9e Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sun, 17 Mar 2024 01:43:45 +0100 Subject: [PATCH] Re-structure whole code into more package-typical file modularity. --- plomtask/__init__.py | 0 plomtask/days.py | 1 - plomtask/{task.py => http.py} | 29 ++--------------------------- plomtask/misc.py | 8 ++++++++ run.py | 21 +++++++++++++++++++++ scripts/pre-commit | 15 +++++++++------ 6 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 plomtask/__init__.py rename plomtask/{task.py => http.py} (73%) mode change 100755 => 100644 create mode 100644 plomtask/misc.py create mode 100755 run.py 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/task.py b/plomtask/http.py old mode 100755 new mode 100644 similarity index 73% rename from plomtask/task.py rename to plomtask/http.py index 49ca883..3fde34b --- a/plomtask/task.py +++ b/plomtask/http.py @@ -1,20 +1,15 @@ -#!/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 +from plomtask.days import Day +from plomtask.misc import HandledException -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.""" @@ -57,23 +52,3 @@ class TaskHandler(BaseHTTPRequestHandler): 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/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/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 -- 2.30.2