From 09b14d437330be43575bcccef425319de381c589 Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Mon, 5 Jul 2021 17:13:43 +0200 Subject: [PATCH] Migrate configuration to dynaconf and include default configuration in deb package --- .gitignore | 4 +++- debian/documento-printserver.install | 3 ++- default.config.json | 8 -------- documento_printserver/config.py | 6 ++++++ documento_printserver/print_server.py | 18 +++++++++--------- poetry.lock | 23 ++++++++++++++++++++++- pyproject.toml | 1 + settings.toml | 11 +++++++++++ tox.ini | 3 ++- 9 files changed, 56 insertions(+), 21 deletions(-) delete mode 100644 default.config.json create mode 100644 documento_printserver/config.py create mode 100644 settings.toml diff --git a/.gitignore b/.gitignore index ae58a2c..1b93aef 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,6 @@ debian/documento-printserver.*.debhelper debian/files barcode-tmp.pdf -config.json \ No newline at end of file +config.json +# Ignore dynaconf secret files +.secrets.* diff --git a/debian/documento-printserver.install b/debian/documento-printserver.install index cccfb64..00a8afe 100644 --- a/debian/documento-printserver.install +++ b/debian/documento-printserver.install @@ -1 +1,2 @@ -99-escpos.rules etc/udev/rules.d \ No newline at end of file +99-escpos.rules etc/udev/rules.d +settings.toml etc/documento/printserver \ No newline at end of file diff --git a/default.config.json b/default.config.json deleted file mode 100644 index 681330b..0000000 --- a/default.config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "vendor": "0416", - "product": "5011", - "url": "http://127.0.0.1:8000", - "username": "admin", - "password": "admin", - "barcode_printer": "QL-500" -} \ No newline at end of file diff --git a/documento_printserver/config.py b/documento_printserver/config.py new file mode 100644 index 0000000..86108aa --- /dev/null +++ b/documento_printserver/config.py @@ -0,0 +1,6 @@ +from dynaconf import Dynaconf + +settings = Dynaconf( + envvar_prefix="DOCUMENTO_PRINTSERVER", + settings_files=["/etc/documento/printserver/settings.toml"], +) diff --git a/documento_printserver/print_server.py b/documento_printserver/print_server.py index 8f11816..fe001c5 100644 --- a/documento_printserver/print_server.py +++ b/documento_printserver/print_server.py @@ -1,4 +1,3 @@ -import json import subprocess # noqa import time @@ -7,13 +6,11 @@ from barcode import Code128 from barcode.writer import ImageWriter from escpos.printer import Usb -# Load config -with open("config.json") as f: - config = json.load(f) +from .config import settings # Read vendor and product IDs from config and setup printer -raw_vendor = config.get("vendor") -raw_product = config.get("product") +raw_vendor = settings.get("printer.vendor") +raw_product = settings.get("printer.product") vendor = int(f"0x{raw_vendor}", 16) product = int(f"0x{raw_product}", 16) print(f"Printer: Vendor: {raw_vendor} ({vendor}); Product: {raw_product} ({product})") @@ -69,7 +66,7 @@ def print_info(document, categories=None): printer.control("LF") -base_url = config.get("url") +base_url = settings.get("server.url") login_url = base_url + "/api/auth/login/" jobs_url = base_url + "/api/print_jobs/" categories_url = base_url + "/api/categories/" @@ -80,7 +77,10 @@ def print_server(): # Get auth token r = requests.post( login_url, - json={"username": config.get("username"), "password": config.get("password"),}, + json={ + "username": settings.get("server.username"), + "password": settings.get("server.password"), + }, ) token = r.json()["token"] headers = {"Authorization": f"Token {token}"} @@ -112,7 +112,7 @@ def print_server(): # Send barcode label to printer subprocess.Popen( # noqa - ["lp", "-d", config.get("barcode_printer"), "barcode-tmp.pdf"], + ["lp", "-d", settings.get("barcode_printer.name"), "barcode-tmp.pdf"], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL, ) # noqa diff --git a/poetry.lock b/poetry.lock index 6f899a5..5a7ce4e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -183,6 +183,23 @@ toml = "*" [package.extras] pipenv = ["pipenv"] +[[package]] +name = "dynaconf" +version = "3.1.4" +description = "The dynamic configurator for your Python Project" +category = "main" +optional = false +python-versions = "*" + +[package.extras] +all = ["redis", "ruamel.yaml", "configobj", "hvac"] +configobj = ["configobj"] +ini = ["configobj"] +redis = ["redis"] +toml = ["toml"] +vault = ["hvac"] +yaml = ["ruamel.yaml"] + [[package]] name = "filelock" version = "3.0.12" @@ -1040,7 +1057,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes [metadata] lock-version = "1.1" python-versions = "^3.6" -content-hash = "f9b9d14acb1db63d5f985fb7967df739432cbc19255eecd5df6158ce037b8920" +content-hash = "870e1ea1a73db425d7e3b392900f102b052aa8a6e08e5d9c07967d6d5b216f03" [metadata.files] alabaster = [ @@ -1161,6 +1178,10 @@ dparse = [ {file = "dparse-0.5.1-py3-none-any.whl", hash = "sha256:e953a25e44ebb60a5c6efc2add4420c177f1d8404509da88da9729202f306994"}, {file = "dparse-0.5.1.tar.gz", hash = "sha256:a1b5f169102e1c894f9a7d5ccf6f9402a836a5d24be80a986c7ce9eaed78f367"}, ] +dynaconf = [ + {file = "dynaconf-3.1.4-py2.py3-none-any.whl", hash = "sha256:e6f383b84150b70fc439c8b2757581a38a58d07962aa14517292dcce1a77e160"}, + {file = "dynaconf-3.1.4.tar.gz", hash = "sha256:b2f472d83052f809c5925565b8a2ba76a103d5dc1dbb9748b693ed67212781b9"}, +] filelock = [ {file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"}, {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"}, diff --git a/pyproject.toml b/pyproject.toml index 5b20391..f2278de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ python = "^3.6" python-escpos = "^3.0a8" python-barcode = "^0.13.1" requests = "^2.25.1" +dynaconf = "^3.1.4" [tool.poetry.dev-dependencies] sphinx = "^3.5" diff --git a/settings.toml b/settings.toml new file mode 100644 index 0000000..a6d14be --- /dev/null +++ b/settings.toml @@ -0,0 +1,11 @@ +[printer] +vendor = "0416" +product = "5011" + +[barcode_printer] +name = "QL-500" + +[server] +url = "http://127.0.0.1:8000" +username = "admin" +password = "admin" diff --git a/tox.ini b/tox.ini index 4f12426..24bf09a 100644 --- a/tox.ini +++ b/tox.ini @@ -37,7 +37,7 @@ commands = [flake8] max_line_length = 100 -exclude = tests,.tox,.venv +exclude = tests,.tox,.venv,debian ignore = BLK100,E203,E231,W503,D100,D101,D102,D103,D104,D105,D106,D107,RST215,RST214,F821,F841,S106,T100,T101,DJ05 [isort] @@ -65,3 +65,4 @@ omit = */tests/* .tox/* .venv/* + debian/* -- GitLab