shimatta_kenkyusho_print_label.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. MICE_TOILET_TEMPLATE = 'http://parts.shimatta.net/api/v1/parts/storages/075acbaf-ed60-47b9-9bd5-98bfbc3a79ea/'
  12. def char_range(c1, c2):
  13. """Generates the characters from `c1` to `c2`, exclusive."""
  14. for c in range(ord(c1), ord(c2)):
  15. yield chr(c)
  16. def handle_storage_qr_code(base_url, token, uuid):
  17. storage_query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/storages/')
  18. query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'id':uuid})
  19. if query_res.status_code != 200:
  20. print('Request unsuccessful')
  21. sys.exit(-3)
  22. print('Storage found: ')
  23. storage_json = query_res.json()
  24. if storage_json['count'] != 1:
  25. print('Storage json does not contain correct amount of storage entries')
  26. sys.exit(-1)
  27. storage = storage_json['results'][0]
  28. print(f'ID: {storage["id"]}')
  29. print(f'Path: {storage["full_path"]}')
  30. labels = []
  31. if storage['template'] == MICE_TOILET_TEMPLATE:
  32. row = input('Select which row shall be printed [1..3]:')
  33. if row not in char_range('1', '4'):
  34. print('Error - invalid row')
  35. return None
  36. row = int(row)
  37. side = input('Select which side shall be printed [t,b]:')
  38. if side not in ['t', 'b']:
  39. print('Error - invalid side')
  40. return None
  41. # get children
  42. query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
  43. if query_res.status_code != 200:
  44. print('Request for children unsuccessful')
  45. sys.exit(-3)
  46. storage_json = query_res.json()
  47. if storage_json['count'] != 24:
  48. print('Storage json does not contain correct amount of storage entries')
  49. sys.exit(-1)
  50. # pagination
  51. storage_list = storage_json['results']
  52. while storage_json['next']:
  53. query_res = requests.get(storage_json['next'], headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
  54. storage_json = query_res.json()
  55. storage_list.extend(storage_json['results'])
  56. storage_dict = {storage['name']: storage for storage in storage_list}
  57. if side == 'b':
  58. for column in char_range('a','i'):
  59. label = shimatta_label.storage_label.StorageLabelSmdMiceToiletBottomQR()
  60. field_uuid = storage_dict[f'{column}{row}']['id']
  61. label.put_content(f'[stor_uuid]{field_uuid}')
  62. labels.append(label)
  63. else:
  64. label = shimatta_label.storage_label.StorageLabel()
  65. label.put_content(f'[stor_uuid]{uuid}', storage['full_path'], storage['verbose_name'])
  66. labels.append(label)
  67. return labels
  68. def handle_component_qr_code(base_url, token, uuid):
  69. query_url = urllib.parse.urljoin(base_url, 'api/v1/parts/components/')
  70. query_res = requests.get(query_url, headers={'Authorization': f'Token {token}'}, params={'id': uuid})
  71. if query_res.status_code != 200:
  72. print('Request unsuccessful')
  73. sys.exit(-3)
  74. component = json.loads(query_res.content)
  75. if component['count'] != 1:
  76. sys.exit(-1)
  77. component = component['results'][0]
  78. pkg = ''
  79. name = component['name']
  80. if component.get('package_data') is not None:
  81. pkg = component['package_data']['name']
  82. manufacturer = component.get('ro_manufacturer_name')
  83. if manufacturer is None:
  84. manufacturer = ''
  85. print(f'Component found: Name: {name}, Package: {pkg}, Manufacturer: {manufacturer}')
  86. labels = []
  87. label = shimatta_label.component_label.ComponentLabelSmall()
  88. label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg)
  89. labels.append(label)
  90. return labels
  91. if len(sys.argv) < 2:
  92. print(f'Usage: {sys.argv[0]} <shimatta kenkyusho url>')
  93. sys.exit(-1)
  94. qr_re = re.compile(r'^\[(?P<prefix>[a-zA-Z_]+)\](?P<uuid>[a-fA-F0-9\-]+)')
  95. kenkyusho_url = sys.argv[1]
  96. print(f"Shimatta Kenkyusho instance: {kenkyusho_url}")
  97. login_url = urllib.parse.urljoin(kenkyusho_url, 'api/v1/token-auth/')
  98. print(f'Token auth url: {login_url}')
  99. username = input('Username: ')
  100. password = getpass.getpass()
  101. ans = requests.post(login_url, data= {'username': username, 'password': password})
  102. if ans.status_code != 200:
  103. print(f'Got status code {ans.status_code}')
  104. sys.exit(-1)
  105. login_token = json.loads(ans.content)['token']
  106. while True:
  107. qr = input('Please scan Storage or Component QR: ')
  108. matches = qr_re.match(qr)
  109. if matches.group('prefix') == 'stor_uuid' :
  110. labels = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
  111. elif matches.group('prefix') == 'comp_uuid':
  112. labels = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
  113. else:
  114. print('Invalid QR code!')
  115. continue
  116. if labels:
  117. ql_wrapper.print_labels(labels)