From e563f01c7236b75f30f1fc7c838480f648f7a99a Mon Sep 17 00:00:00 2001 From: frederic Date: Thu, 1 Feb 2024 23:56:34 +0100 Subject: [PATCH] updated python script to use printservant instead of local lpr --- Dockerfile | 9 ++--- labelmaker/{__main__.py => main.py} | 56 ++++++----------------------- labelmaker/templates/index.html | 14 -------- 3 files changed, 16 insertions(+), 63 deletions(-) rename labelmaker/{__main__.py => main.py} (67%) diff --git a/Dockerfile b/Dockerfile index 9b077c2..c0b4736 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,11 +36,12 @@ RUN apt-get update && apt-get upgrade -y \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && apt-get clean -y && rm -rf /var/lib/apt/lists/* -WORKDIR /labelmaker -COPY ./poetry.lock ./pyproject.toml /labelmaker/ +RUN git clone https://code.hackerspace.pl/etorameth/labelmaker +WORKDIR /labelmaker RUN poetry install --no-interaction --no-ansi --sync -COPY . /labelmaker +RUN useradd web +USER web -CMD poetry run python labelmaker/__main__.py +CMD poetry run python labelmaker/main.py diff --git a/labelmaker/__main__.py b/labelmaker/main.py similarity index 67% rename from labelmaker/__main__.py rename to labelmaker/main.py index bc8611a..13fd32a 100644 --- a/labelmaker/__main__.py +++ b/labelmaker/main.py @@ -6,14 +6,15 @@ import time import cairocffi as cairo import flask +import img2pdf import pangocffi as pango import pangocairocffi as pangocairo +import requests class App(flask.Flask): def __init__(self, *args, **kwargs): super(App, self).__init__(*args, **kwargs) self.last = 0 - self.health = (0, False, "unknown") app = App(__name__) @@ -73,68 +74,33 @@ class Renderer(object): def index(): return flask.render_template('index.html') -def healthcheck(): - last_checked, last_status, last_details = app.health - if time.time() - last_checked < 1: - return last_status, last_details - output = subprocess.check_output(['lpstat', '-p', '-d']).decode() - mark = False - for line in output.split('\n'): - line = line.strip() - if line.startswith('printer DYMO_LabelWriter450'): - if 'is idle.' in line: - return True, 'Idle' - mark = True - continue - if mark: - if line.startswith('Ready to print'): - app.health = (time.time(), True, line) - return True, line - else: - app.health = (time.time(), False, line) - return False, line - mark = False - return False, "PRINTER_NOT_CONNECTED" - -@app.route('/health') -def health(): - ok, details = healthcheck() - return json.dumps({'ok': ok, 'details': details}) - @app.route('/api/preview//') def stuff_preview(size): text = flask.request.args.get('text') html = flask.request.args.get('html') == '1' - r = Renderer() - r.render_text(text, 'Sans {}'.format(size), 0, -1, html) + renderer = Renderer() + renderer.render_text(text, 'Sans {}'.format(size), 0, -1, html) - preview = r.surface.write_to_png() + preview = renderer.surface.write_to_png() return flask.Response(preview, mimetype='image/png') DELAY = 5 @app.route('/api/print//', methods=['POST']) def stuff_print(size): - if not healthcheck()[0]: - return 'Printer is down.' last = app.last print(last, time.time() - last) if time.time() - last < DELAY: return 'Please wait {} more seconds before next print.'.format(int(DELAY - (time.time() - last))) text = flask.request.args.get('text') html = flask.request.args.get('html') == '1' - r = Renderer() - r.render_text(text, 'Sans {}'.format(size), 0, -1, html) - path = '/tmp/hslabel' - f = open(path, 'wb') + renderer = Renderer() + renderer.render_text(text, 'Sans {}'.format(size), 0, -1, html) - r.surface.write_to_png(f) - f.flush() - f.close() - time.sleep(1) - ex = 'lpr -o PageSize=w118h252 -P DYMO_LabelWriter450 {}'.format(path) - os.system(ex) + data = img2pdf.convert(renderer.surface.write_to_png()) + payload = {'printer': 'dymo_labelwriter450', 'copies': 1, 'body': data} + r = requests.post(os.environ.get('PRINTSERVANT_HOST') + '/print', params=payload) + print('Printing job response', r.text) - f.close() app.last = time.time() return 'ok' diff --git a/labelmaker/templates/index.html b/labelmaker/templates/index.html index e8326fd..f2b9a71 100644 --- a/labelmaker/templates/index.html +++ b/labelmaker/templates/index.html @@ -50,7 +50,6 @@

Box 'o Stuff Label For SAMLA boxes with common equipment

@@ -132,19 +131,6 @@ $ curl -d "" http://label.waw.hackerspace.pl/api/print/60/?text=foobar&html= print(); }); generatePreview(); - var updateStatus = function () { - $.getJSON("/health", function (data) { - if (data.ok) { - $("#systemstatus").attr("class", "label label-success"); - $("#systemstatus").html(data.details); - } else { - $("#systemstatus").attr("class", "label label-danger"); - $("#systemstatus").html(data.details); - } - }); - }; - updateStatus(); - setInterval(updateStatus, 1000); });