106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
|
#!/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)
|