-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigi_reader.py
More file actions
93 lines (70 loc) · 2.61 KB
/
Copy pathdigi_reader.py
File metadata and controls
93 lines (70 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#To Run:
# type input_file.txt | python digi_reader.py >> output.csv
import sys
from provider.digikey import *
from provider.mouser import *
if __name__ == "__main__":
for line in sys.stdin:
sys.stderr.write('\n\n-------------------------------------\n')
# don't read commented lines
if line.strip()[0] == '#':
continue
d = None
# parse barcode 1786439000000040970571
sys.stderr.write('info: code read: {}'.format(line))
if len(line) == 23: # digikey bag code
prod_id = line[0:7]
qnt = line[8:16]
#If the code ends in a zero (Remove it due to search engine problems)
if line[6] == "0":
newList = list(prod_id)
del(newList[6])
prod_id = "".join(newList)
sys.stderr.write('info: String end popped: {}\n'.format(prod_id))
#If the code starts with a zero (Remove it due to search engine problems)
if line[0] == "0":
newList = list(prod_id)
del(newList[0])
prod_id = "".join(newList)
sys.stderr.write('info: String front popped: {}\n'.format(prod_id))
d = barcode2data(prod_id)
elif '-ND' in line: # digikey part/number
d = partNumber2Data(line.strip())
elif '-' in line: # mouser code
d = mouser2data(line.strip())
else:
sys.stderr.write('info: cannot parse code, no valid format : {}\n'.format(line))
if d is not None:
print "%s, %s, %s, %s, %s" % (d['provider'], line.replace("\n", ""), d['provider_pn'], d['manufacturer_pn'], d['description'])
#Takes in String of Barcode and returns Data
def barcodeScan(line):
sys.stderr.write('\n\n-------------------------------------\n')
data = None
sys.stderr.write('info: code read: {}\n'.format(line))
if len(line) >= 22: # digikey bag code
prod_id = line[0:7]
qnt = line[8:16]
#If the code ends in a zero (Remove it due to search engine problems)
if line[6] == "0":
newList = list(prod_id)
del(newList[6])
prod_id = "".join(newList)
sys.stderr.write('info: String end popped: {}\n'.format(prod_id))
#If the code starts with a zero (Remove it due to search engine problems)
if line[0] == "0":
newList = list(prod_id)
del(newList[0])
prod_id = "".join(newList)
sys.stderr.write('info: String front popped: {}\n'.format(prod_id))
data = barcode2data(prod_id)
elif '-ND' in line: # digikey part/number
data = partNumber2Data(line.strip())
elif '-' in line: # mouser code
d = mouser2data(line.strip())
else:
sys.stderr.write('info: cannot parse code, no valid format : {}\n'.format(line))
if data is not None:
print "%s, %s, %s, %s" % (d['provider'], d['provider_pn'], d['manufacturer_pn'], d['description'])
return data