-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenID.py
79 lines (73 loc) · 3.12 KB
/
genID.py
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
# A script to log and generate Vendor IDs for C.H.I.P. DIP add-on boards
# Script by Peter Nyboer [email protected]
# TO DO:
# Add a 'delete' or 'delete last' ID function
import sys
import random
import json
import re
def makeid(email,vendorname,preferredID=None):
prefID = ''
if preferredID:
prefID = preferredID.lower()
else:
prefID = hex(random.randrange(0,2147483647))
# read json of existing IDs
jsonfile = open('DIP_vendors.json')
jsondata = jsonfile.read()
print jsondata
vendorDB = json.loads(jsondata)
# parse existing IDs into list to use to check uniqueness:
currentIDs = list()
for key, data in vendorDB.iteritems():
for vid,vinfo in data.iteritems():
print 'VID: '+str(vid)
currentIDs.append(vid)
# make sure JSON is ok
if not preflight(currentIDs):
print 'The JSON file does not have contain all unique vendor IDs. Please Fix DIP_vendors.json'
quit()
else:
# if the desired ID matches an existing ID, generate a new one
if prefID in currentIDs:
print ' :( Your chosen Vendor ID '+prefID+' is already taken.'
choice = raw_input(' > Type "e" to exit or "g" to generate a new ID: ')
if choice == 'g':
while prefID in currentIDs:
prefID = hex(random.randrange(0,2147483647))
print ' + Your new vendor ID is: '+prefID
else:
print 'quit'
quit()
# add info to JSON file
newvendor = { prefID: {"vendor": vendorname,"contact": email} }
vendorDB['vendorIDs'].update(newvendor)
with open('DIP_vendors.json','w') as writejson:
json.dump(vendorDB,writejson)
print ' :) vendorDB updated'
# simple regex check of email format.
def valid_email(email):
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email)
if match:
return match
else:
print ' ! email not valid format. Call script with 3 arguments (last is optional):\n $ python genID.py [email protected] "DIP Makers Inc." 0x00000001'
quit()
# test out the vendor IDs to make sure they are unique. Unfortunately, json.loads() will filter out exactly matched keys, so we can't do much about that.
def preflight(x):
print '-- preflight --'
seen = set()
# http://stackoverflow.com/questions/5278122/checking-if-all-elements-in-a-list-are-unique
test = not any( i.lower() in seen or seen.add(i.lower() ) for i in x)
print '>> preflight result '+str(test)
return test
# TO DO - insert preflight on JSON to make sure it is OK
# test arguments sys.argv to make sure all is good, then call makeid.
if len(sys.argv) < 3:
print ' ! Call genID.py with at least email and vendorname arguments.\n Optional 3rd argument is desired ID, e.g.:\n $ python genID.py [email protected] "DIP Makers Inc." 0x00000001'
elif len(sys.argv) == 3:
if valid_email(sys.argv[1]):
makeid(sys.argv[1],sys.argv[2])
elif len(sys.argv) == 4:
if valid_email(sys.argv[1]):
makeid(sys.argv[1],sys.argv[2],sys.argv[3])