shimatta_kenkyusho_print_label.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/python
  2. import shimatta_label.storage_label
  3. import shimatta_label.component_label
  4. import shimatta_label.brother_ql_wrapper as ql_wrapper
  5. import sys
  6. import requests
  7. import urllib.parse
  8. import getpass
  9. import json
  10. import re
  11. def handle_storage_qr_code(base_url, token, uuid):
  12. storage_query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/storages/')
  13. query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'id':uuid})
  14. if query_res.status_code != 200:
  15. print('Request unsuccessful')
  16. sys.exit(-3)
  17. print('Storage found: ')
  18. storage_json = json.loads(query_res.content)
  19. if storage_json['count'] != 1:
  20. print('Storage json does not contain correct amount of storage entries')
  21. sys.exit(-1)
  22. storage = storage_json['results'][0]
  23. print(f'ID: {storage["id"]}')
  24. print(f'Path: {storage["full_path"]}')
  25. label = shimatta_label.storage_label.StorageLabel()
  26. label.put_content(f'[stor_uuid]{uuid}', storage['full_path'])
  27. return label
  28. def handle_component_qr_code(base_url, token, uuid):
  29. query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/components/')
  30. query_res = requests.get(query_url, headers={'Authorization': f'Token {token}'}, params={'id': uuid})
  31. if query_res.status_code != 200:
  32. print('Request unsuccessful')
  33. sys.exit(-3)
  34. component = json.loads(query_res.content)
  35. if component['count'] != 1:
  36. sys.exit(-1)
  37. component = component['results'][0]
  38. pkg = ''
  39. name = component['name']
  40. if component.get('package_data') is not None:
  41. pkg = component['package_data']['name']
  42. manufacturer = component.get('ro_manufacturer_name')
  43. if manufacturer is None:
  44. manufacturer = ''
  45. print(f'Component found: Name: {name}, Package: {pkg}, Manufacturer: {manufacturer}')
  46. label = shimatta_label.component_label.ComponentLabel()
  47. label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg)
  48. return label
  49. printer_model = 'QL-800'
  50. printer_connection = 'usb://0x04f9:0x209b'
  51. if len(sys.argv) < 2:
  52. print(f'Usage: {sys.argv[0]} <shimatta kenkyusho url>')
  53. sys.exit(-1)
  54. qr_re = re.compile(r'^\[(?P<prefix>[a-zA-Z_]+)\](?P<uuid>[a-fA-F0-9\-]+)')
  55. kenkyusho_url = sys.argv[1]
  56. print(f"Shimatta Kenkyusho instance: {kenkyusho_url}")
  57. login_url = urllib.parse.urljoin(kenkyusho_url, 'api/v1/token-auth/')
  58. print(f'Token auth url: {login_url}')
  59. username = input('Username: ')
  60. password = getpass.getpass()
  61. ans = requests.post(login_url, data= {'username': username, 'password': password})
  62. if ans.status_code != 200:
  63. print(f'Got status code {ans.status_code}')
  64. sys.exit(-1)
  65. login_token = json.loads(ans.content)['token']
  66. while True:
  67. qr = input('Please scan Storage or Component QR: ')
  68. matches = qr_re.match(qr)
  69. if matches.group('prefix') == 'stor_uuid' :
  70. label = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
  71. elif matches.group('prefix') == 'comp_uuid':
  72. label = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
  73. else:
  74. print('Invalid QR code!')
  75. continue
  76. printer = ql_wrapper.BrotherQlPrinter(model=printer_model, printer_connection=printer_connection, label_format='29x90')
  77. printer.print_image(label.get_pillow_image(), rotation=90, cut=True)
  78. cont = input('Printed! Continue? [Y/n] ')
  79. if cont.lower() == 'n':
  80. sys.exit(0)