123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #!/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
- MICE_TOILET_TEMPLATE = 'http://parts.shimatta.net/api/v1/parts/storages/075acbaf-ed60-47b9-9bd5-98bfbc3a79ea/'
- def char_range(c1, c2):
- """Generates the characters from `c1` to `c2`, exclusive."""
- for c in range(ord(c1), ord(c2)):
- yield chr(c)
- 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 = query_res.json()
- 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"]}')
- labels = []
- if storage['template'] == MICE_TOILET_TEMPLATE:
- row = input('Select which row shall be printed [1..3]:')
- if row not in char_range('1', '4'):
- print('Error - invalid row')
- return None
- row = int(row)
- side = input('Select which side shall be printed [t,b]:')
- if side not in ['t', 'b']:
- print('Error - invalid side')
- return None
- # get children
- query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
- if query_res.status_code != 200:
- print('Request for children unsuccessful')
- sys.exit(-3)
- storage_json = query_res.json()
- if storage_json['count'] != 24:
- print('Storage json does not contain correct amount of storage entries')
- sys.exit(-1)
- # pagination
- storage_list = storage_json['results']
- while storage_json['next']:
- query_res = requests.get(storage_json['next'], headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
- storage_json = query_res.json()
- storage_list.extend(storage_json['results'])
- storage_dict = {storage['name']: storage for storage in storage_list}
- if side == 'b':
- for column in char_range('a','i'):
- label = shimatta_label.storage_label.StorageLabelSmdMiceToiletBottomQR()
- field_uuid = storage_dict[f'{column}{row}']['id']
- label.put_content(f'[stor_uuid]{field_uuid}')
- labels.append(label)
- else:
- label = shimatta_label.storage_label.StorageLabel()
- label.put_content(f'[stor_uuid]{uuid}', storage['full_path'], storage['verbose_name'])
- labels.append(label)
- return labels
- 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}')
- labels = []
- label = shimatta_label.component_label.ComponentLabelSmall()
- label.put_content(f'[comp_uuid]{uuid}', component['name'], manufacturer, pkg)
- labels.append(label)
- return labels
- 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' :
- labels = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
- elif matches.group('prefix') == 'comp_uuid':
- labels = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
- else:
- print('Invalid QR code!')
- continue
- if labels:
- ql_wrapper.print_labels(labels)
|