diff --git a/.gitignore b/.gitignore index ae58a2ce678c309501040e73a784a1c1e181e88f..1b93aefb9a21bbccb2e7a7d275f0a309727e2136 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 cccfb64abb3fc94e3495d8024e06ef185448c090..00a8afe377cc72c94d43bc913f0de38580a089ab 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 681330b88ba1fe3686cce33360f811ae050a7af1..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..86108aa718ef673de16626a2a53398b85fa50b21 --- /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 8f11816769fcceed591f0dd428d39d2b4cf734b0..fe001c51f41d38f38229a61d12ea7d2ac169490d 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 6f899a5554696238b06a978129427a68432bec98..5a7ce4eb0b7a8e7912fdd13b5bf7c64b92cd9548 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 5b2039107eeff73e418ee268e5e53cffa1a198b5..f2278de0bca1f0e1efd7869c4da2961e50294dc7 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 0000000000000000000000000000000000000000..a6d14be602e2d81e938ff8b3169c8576d2e39530 --- /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 4f124261629cf5fe7acd006113766c555459afe9..24bf09a70548f0eb87b3ff9d476610e0c4d55ac6 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/*