diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-02-05 16:39:41 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-02-05 16:39:41 +0800 |
commit | fcc3ce60bcfa57734a51723717fe681ee4bf1327 (patch) | |
tree | c8d883a6cd2b898fed85a7f717be34de242ded25 /jimbrella | |
parent | 14feb6a4378668d054d28a1335eacd2026b621dc (diff) |
Config format switched to .ini
Eliminated toml from dependency list
Diffstat (limited to 'jimbrella')
-rw-r--r-- | jimbrella/admin.py | 8 | ||||
-rw-r--r-- | jimbrella/auth.py | 6 | ||||
-rw-r--r-- | jimbrella/config.py | 26 | ||||
-rw-r--r-- | jimbrella/routine.py | 13 | ||||
-rw-r--r-- | jimbrella/test/jform_data.py | 4 | ||||
-rw-r--r-- | jimbrella/test/test_routine.py | 12 | ||||
-rw-r--r-- | jimbrella/umbrellas.py | 1 | ||||
-rw-r--r-- | jimbrella/web.py | 4 |
8 files changed, 21 insertions, 53 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py index 1385d0a..b150e3d 100644 --- a/jimbrella/admin.py +++ b/jimbrella/admin.py @@ -6,13 +6,13 @@ from .umbrellas import Umbrellas from .admin_log import AdminLog from .users import Users from .exceptions import * -from .config import * +from .config import config from .utils import human_datetime, human_timedelta, CST bp = Blueprint("admin", __name__, url_prefix="/admin") -db = Umbrellas(DATABASE_PATH) -users = Users(DATABASE_PATH) -admin_log = AdminLog(ADMIN_LOG_PATH) +db = Umbrellas(config.get("general", "db_path")) +users = Users(config.get("general", "db_path")) +admin_log = AdminLog(config.get("logging", "admin_log_path")) @bp.before_request diff --git a/jimbrella/auth.py b/jimbrella/auth.py index 39d355b..0b88de2 100644 --- a/jimbrella/auth.py +++ b/jimbrella/auth.py @@ -2,10 +2,10 @@ from flask import Blueprint, request, session, render_template, redirect, url_fo from werkzeug.security import generate_password_hash, check_password_hash from .users import Users from .exceptions import UsernameTakenError -from .config import * +from .config import config bp = Blueprint("auth", __name__, url_prefix="/") -users = Users(DATABASE_PATH) +users = Users(config.get("general", "db_path")) def show_error(action, message): @@ -39,7 +39,7 @@ def auth(action): if not check_password_hash(user["password"], password): return show_error("login", "Incorrect password. Sorry.") elif action == "register": - if not ACCEPT_NEW_USERS: + if not config.getboolean("general", "accept_new_users"): return show_error("register", "Sorry, but user registrations are closed.") try: users.register(username, generate_password_hash(password), "en-US") diff --git a/jimbrella/config.py b/jimbrella/config.py index 81e5d09..0759f50 100644 --- a/jimbrella/config.py +++ b/jimbrella/config.py @@ -1,30 +1,12 @@ +from configparser import ConfigParser import os from pathlib import Path -import toml config_path = ( os.environ["JIMBRELLA_CONFIG"] if "JIMBRELLA_CONFIG" in os.environ - else "/opt/jimbrella/config.toml" + else "/opt/jimbrella/config.ini" ) -f = open(config_path) -config = toml.load(f) -f.close() - -JIMBRELLA_DIR = Path(config["jimbrella_dir"]) - -FLASK_SECRET_KEY = config["flask"]["secret_key"] - -ACCEPT_NEW_USERS = config["user"]["accept_new_users"] - -JFORM_TAKEAWAY_URL = config["jform"]["takeaway_url"] -JFORM_GIVEBACK_URL = config["jform"]["giveback_url"] -JFORM_BOOKMARK_DIR = JIMBRELLA_DIR / Path(config["jform"]["bookmark_dir"]) - -DATABASE_PATH = JIMBRELLA_DIR / Path(config["db"]["db_path"]) - -DUE_HOURS = config["rules"]["due_hours"] - -LOG_PATH = JIMBRELLA_DIR / Path(config["logging"]["log_path"]) -ADMIN_LOG_PATH = JIMBRELLA_DIR / Path(config["logging"]["admin_log_path"]) +config = ConfigParser() +config.read(config_path) diff --git a/jimbrella/routine.py b/jimbrella/routine.py index 2626a76..77d69f2 100644 --- a/jimbrella/routine.py +++ b/jimbrella/routine.py @@ -4,9 +4,8 @@ import logging from .umbrellas import Umbrellas from .jform import JForm from .admin_log import AdminLog -from .config import * from .exceptions import * -from .config import DUE_HOURS +from .config import config from .utils import CST """A set of routine methods, run at an interval (somewhere from ten minutes to one hour), to: @@ -114,7 +113,7 @@ def process_overdue(db: Umbrellas, admin: AdminLog): ) continue - if now - lent_at > timedelta(hours=DUE_HOURS): + if now - lent_at > timedelta(hours=config.getint("general", "due_hours")): db.mark_overdue(umb["id"]) logging.warning( "Umbrella #{id} is now overdue, tenant: {tenant_name} (ID: {tenant_id}, phone: {tenant_phone})".format( @@ -134,9 +133,9 @@ def process_overdue(db: Umbrellas, admin: AdminLog): if __name__ == "__main__": - takeaway = JForm("takeaway", JFORM_TAKEAWAY_URL, JFORM_BOOKMARK_DIR) - giveback = JForm("giveback", JFORM_GIVEBACK_URL, JFORM_BOOKMARK_DIR) - db = Umbrellas(DATABASE_PATH) - admin_log = AdminLog(ADMIN_LOG_PATH) + takeaway = JForm("takeaway", config.get("jform", "takeaway_url"), config.get("jform", "bookmark_dir")) + giveback = JForm("giveback", config.get("jform", "giveback_url"), config.get("jform", "bookmark_dir")) + db = Umbrellas(config.get("general", "db_path")) + admin_log = AdminLog(config.get("logging", "admin_log_path")) sync_jform(takeaway, giveback, db, admin_log) process_overdue(db, admin_log) diff --git a/jimbrella/test/jform_data.py b/jimbrella/test/jform_data.py index 5d235ce..756a9ae 100644 --- a/jimbrella/test/jform_data.py +++ b/jimbrella/test/jform_data.py @@ -1,7 +1,6 @@ import json from datetime import datetime, timedelta from ..umbrellas import Umbrellas -from ..config import * TENANT_NAMES = ["Alice", "Bob", "Carol", "Dave", "Eve", "Frank"] TENANT_IDS = ["01", "02", "03", "04", "05", "06"] @@ -91,9 +90,6 @@ def mock_jform_data() -> tuple: "code": 0, } - db = Umbrellas(DATABASE_PATH) - umbrellas = db.read() - key_numbers = [umb["id"] for umb in umbrellas] now = datetime.now() takeaway_rows = [ mock_answer_sheet(*ALICE, 1, 1, now - timedelta(days=7)), diff --git a/jimbrella/test/test_routine.py b/jimbrella/test/test_routine.py index 8d718be..b49ad28 100644 --- a/jimbrella/test/test_routine.py +++ b/jimbrella/test/test_routine.py @@ -9,15 +9,7 @@ from ..routine import sync_jform, process_overdue from ..umbrellas import Umbrellas from ..jform import JForm from ..admin_log import AdminLog -from ..config import * - -"""Set up logging. -logging.basicConfig( - filename="/tmp/jimbrella.log", - encoding="utf-8", - level=logging.INFO, - format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s", -)""" +from ..config import config """Spin up a simple Flask app to mimic jForm.""" jform = Flask(__name__) @@ -65,7 +57,7 @@ if __name__ == "__main__": mock_jform.start() """Initialize Database and JForm.""" - shutil.copyfile(DATABASE_PATH, TEST_DATABASE_PATH) + shutil.copyfile(config.get("general", "db_path"), TEST_DATABASE_PATH) db = Umbrellas(TEST_DATABASE_PATH) takeaway_jform = JForm("takeaway", "http://localhost:5001/takeaway", "/tmp") giveback_jform = JForm("giveback", "http://localhost:5001/giveback", "/tmp") diff --git a/jimbrella/umbrellas.py b/jimbrella/umbrellas.py index 2525a31..b3a261d 100644 --- a/jimbrella/umbrellas.py +++ b/jimbrella/umbrellas.py @@ -3,7 +3,6 @@ from datetime import datetime, timezone, timedelta from dateutil.parser import isoparse from typing import Union from .utils import human_datetime, human_timedelta, CST -from .config import DUE_HOURS, ADMIN_LOG_PATH from .exceptions import * STATUSES = ["available", "lent", "overdue", "maintenance", "unknown"] diff --git a/jimbrella/web.py b/jimbrella/web.py index 479c22a..95d1020 100644 --- a/jimbrella/web.py +++ b/jimbrella/web.py @@ -2,13 +2,13 @@ import logging from flask import Flask, render_template from .admin import bp as admin_bp from .auth import bp as auth_bp -from .config import * +from .config import config # this logger is shared across modules # logging.basicConfig(filename=LOG_PATH, level=logging.DEBUG) app = Flask("jimbrella") -app.secret_key = FLASK_SECRET_KEY +app.secret_key = config.get("security", "secret_key") @app.route("/") |