diff --git a/tools/python_utils/__pycache__/app.cpython-312.pyc b/tools/python_utils/__pycache__/app.cpython-312.pyc index 72dd69d..c62195c 100644 Binary files a/tools/python_utils/__pycache__/app.cpython-312.pyc and b/tools/python_utils/__pycache__/app.cpython-312.pyc differ diff --git a/tools/python_utils/__pycache__/label.cpython-312.pyc b/tools/python_utils/__pycache__/label.cpython-312.pyc new file mode 100644 index 0000000..384b77f Binary files /dev/null and b/tools/python_utils/__pycache__/label.cpython-312.pyc differ diff --git a/tools/python_utils/app.py b/tools/python_utils/app.py index 28e3174..2378d5f 100644 --- a/tools/python_utils/app.py +++ b/tools/python_utils/app.py @@ -2,6 +2,8 @@ from flask import Flask, send_from_directory, abort, request import json import requests import os +import logging +import label app = Flask(__name__) def pretty_print_POST(req): @@ -39,13 +41,21 @@ def download_pre(): except FileNotFoundError: abort(404, description="File not found") -@app.route('/print', methods=['POST']) -def print_label(): - url = "http://10.200.4.12:8000/label" - data = request.get_json() - label = requests.post(url,data=json.dumps(data)).content - print(label) - return "{Success}" + +@app.route("/label", methods=["POST"]) +def build(): + typst_bytes = request.get_data() + typst_string = typst_bytes.decode("utf-8") + + try: + j = json.loads(typst_string) + except json.JSONDecodeError as e: + print(e) + return "Failed" + + pdf = label.compile_pdf(j) + return "Success" + if __name__ == '__main__': app.run(debug=True) diff --git a/tools/python_utils/files/logo_stack.png b/tools/python_utils/files/logo_stack.png new file mode 100644 index 0000000..4328b5b Binary files /dev/null and b/tools/python_utils/files/logo_stack.png differ diff --git a/tools/python_utils/files/main.typ b/tools/python_utils/files/main.typ new file mode 100644 index 0000000..2557085 --- /dev/null +++ b/tools/python_utils/files/main.typ @@ -0,0 +1,69 @@ +#import "@preview/tiaoma:0.3.0" +#set page(width: 89mm, height: 36mm, + margin: ( + top: 2mm, + bottom: 2mm, + left: 3mm, + right: 3mm, + )) +#set text(font: "DejaVu Sans Mono") +#let label(computer) = block[ + +#table( + columns: (2cm,1fr,auto), + align: (center,left,center), + stroke: 0pt, + inset: 0mm, +// gutter: 0mm, + [#image("logo_stack.png",width: 60%)], +[ + #set text(11pt) + angestoepselt e.V. \ + Passwort: csw \ + #set text(20pt) + #computer.id + #set text(10pt) +],[ + #tiaoma.barcode( + computer.id, + "QRCode", + height: 20mm, + ) + ]/*, + table.cell(colspan: 3)[ + #set text(8pt) + #block(height: 0.5mm) + #computer.distribution/#computer.version/#computer.cpu/#computer.memory/#computer.disk/ID:#computer.id + ] */ + ) + + #set text(10pt) + #computer.distribution/#computer.version/#computer.cpu/#computer.memory/#computer.disk/ID:#computer.id +] + +#let charger(computer) = block[ + #table( + columns: (2cm,1fr,auto), + align: (center,left,center), + stroke: 0pt, + inset: 1mm, + [#image("logo_stack.png",width: 60%)], + [ + #set text(8pt) + Ladegerät:\ + #set text(20pt) + #computer.id + #set text(8pt) + ],[ + #tiaoma.barcode( + computer.id, + "QRCode", + height: 20mm, + ) + ] + ) +] + +#let computer = json(bytes(sys.inputs.computer)) +#label(computer) +#charger(computer) diff --git a/tools/python_utils/label.py b/tools/python_utils/label.py new file mode 100644 index 0000000..85977de --- /dev/null +++ b/tools/python_utils/label.py @@ -0,0 +1,14 @@ +import typst +import json + + + +def compile_pdf(data: dict): + data = {"computer": json.dumps(data)} + try: + res = typst.compile(input="files/main.typ", sys_inputs=data) + except RuntimeError as e: + print(e) + print("Error compiling") + return False + print(res) diff --git a/tools/python_utils/python.sh b/tools/python_utils/python.sh index cfb9450..719cebd 100644 --- a/tools/python_utils/python.sh +++ b/tools/python_utils/python.sh @@ -13,6 +13,6 @@ print_data() { EOF } -curl -X POST http://localhost:5000/print \ +curl -X POST http://localhost:5000/label \ -H "Content-Type: application/json" \ -d "$(print_data)" diff --git a/tools/python_utils/shell.nix b/tools/python_utils/shell.nix index c2e9605..e9978c5 100644 --- a/tools/python_utils/shell.nix +++ b/tools/python_utils/shell.nix @@ -6,6 +6,8 @@ pkgs.mkShell { (pkgs.python3.withPackages (ps: with ps; [ flask requests + typst + pip ])) ];