Add additional scripts

This commit is contained in:
Mario Hüttel 2022-01-10 17:31:39 +01:00
parent 6f2caf0d49
commit 78fe0c9799
4 changed files with 171 additions and 2 deletions

10
.vscode/launch.json vendored
View File

@ -4,7 +4,14 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Kenkyusho Storage Query",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/shimatta_kenkyusho_print_storage.py",
"console": "integratedTerminal",
"args": ["https://lab.shimatta.de/"]
},
{
"name": "Python: Example Print",
"type": "python",
@ -22,5 +29,4 @@
"args": []
}
]
}

33
print_storage_matrix_code.py Executable file
View File

@ -0,0 +1,33 @@
#!/bin/python
import shimatta_label.label_image as li
import shimatta_label.brother_ql_wrapper as ql_wrapper
import sys
import treepoem
class DotmatrixLabel(li.Label):
pixels_x = 120
pixels_y = 106
def put_matrix_code(self, qr):
code = treepoem.generate_barcode('datamatrix', qr).convert('1')
box = (int(self.pixels_x/2 -code.size[0]/2), int(self.pixels_y/2 - code.size[1]/2))
self.img.paste(code, box=box)
printer_model = 'QL-800'
printer_connection = 'usb://0x04f9:0x209b'
if len(sys.argv) >= 2:
qr_data = sys.argv[1]
else:
qr_data = input('Scan QR Code: ')
while qr_data:
label = DotmatrixLabel()
label.put_matrix_code(qr_data)
#label.save('/tmp/foo.png')
printer = ql_wrapper.BrotherQlPrinter(model=printer_model, printer_connection=printer_connection, label_format='12')
printer.print_image(label.get_pillow_image(), cut=True, rotation=90)
qr_data = input('Scan QR Code: ')

105
shimatta_kenkyusho_print_label.py Executable file
View File

@ -0,0 +1,105 @@
#!/bin/python
import shimatta_label.storage_label
import shimatta_label.component_label
import shimatta_label.brother_ql_wrapper as ql_wrapper
import sys
import requests
import urllib.parse
import getpass
import json
import re
def handle_storage_qr_code(base_url, token, uuid):
storage_query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/storages/')
query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'id':uuid})
if query_res.status_code != 200:
print('Request unsuccessful')
sys.exit(-3)
print('Storage found: ')
storage_json = json.loads(query_res.content)
if storage_json['count'] != 1:
print('Storage json does not contain correct amount of storage entries')
sys.exit(-1)
storage = storage_json['results'][0]
print(f'ID: {storage["id"]}')
print(f'Path: {storage["full_path"]}')
label = shimatta_label.storage_label.StorageLabel()
label.put_content(f'[stor_uuid]{uuid}', storage['full_path'])
return label
def handle_component_qr_code(base_url, token, uuid):
query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/components/')
query_res = requests.get(query_url, headers={'Authorization': f'Token {token}'}, params={'id': uuid})
if query_res.status_code != 200:
print('Request unsuccessful')
sys.exit(-3)
component = json.loads(query_res.content)
if component['count'] != 1:
sys.exit(-1)
component = component['results'][0]
pkg = ''
name = component['name']
if component.get('package_data') is not None:
pkg = component['package_data']['name']
manufacturer = component.get('ro_manufacturer_name')
if manufacturer is None:
manufacturer = ''
print(f'Component found: Name: {name}, Package: {pkg}, Manufacturer: {manufacturer}')
label = shimatta_label.component_label.ComponentLabel()
label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg)
return label
printer_model = 'QL-800'
printer_connection = 'usb://0x04f9:0x209b'
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} <shimatta kenkyusho url>')
sys.exit(-1)
qr_re = re.compile(r'^\[(?P<prefix>[a-zA-Z_]+)\](?P<uuid>[a-fA-F0-9\-]+)')
kenkyusho_url = sys.argv[1]
print(f"Shimatta Kenkyusho instance: {kenkyusho_url}")
login_url = urllib.parse.urljoin(kenkyusho_url, 'api/v1/token-auth/')
print(f'Token auth url: {login_url}')
username = input('Username: ')
password = getpass.getpass()
ans = requests.post(login_url, data= {'username': username, 'password': password})
if ans.status_code != 200:
print(f'Got status code {ans.status_code}')
sys.exit(-1)
login_token = json.loads(ans.content)['token']
while True:
qr = input('Please scan Storage or Component QR: ')
matches = qr_re.match(qr)
if matches.group('prefix') == 'stor_uuid' :
label = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
elif matches.group('prefix') == 'comp_uuid':
label = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
else:
print('Invalid QR code!')
continue
printer = ql_wrapper.BrotherQlPrinter(model=printer_model, printer_connection=printer_connection, label_format='29x90')
printer.print_image(label.get_pillow_image(), rotation=90, cut=True)
cont = input('Printed! Continue? [Y/n] ')
if cont.lower() == 'n':
sys.exit(0)

View File

@ -0,0 +1,25 @@
from .label_image import Label
import qrcode
class ComponentLabel(Label):
pixels_x = 991
pixels_y = 306
def __init__(self):
super().__init__()
def put_content(self, qr_data, component_name, manufacturer, package):
self.draw_text(component_name, 320, self.pixels_y/2-45, size=38, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
self.draw_text(manufacturer, 320, self.pixels_y/2, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
self.draw_text(package, 320, self.pixels_y/2+40, size=32, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=8,
border=0,
)
qr.add_data(qr_data)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
qr_y_size = qr_image.size[1]
self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))