partsdb

electronic parts inventory
git clone https://git.e1e0.net/partsdb.git
Log | Files | Refs

commit 29a101cae3fd36806b8c8080639fd343cb2c6e32
parent 7e8fccd17ced4084d228b1873263c81d2ea2466f
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 14 Mar 2021 12:32:42 +0100

avoid interactive add if wanted

Diffstat:
MmyChips.py | 46+++++++++++++++++++++++++++++++---------------
1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/myChips.py b/myChips.py @@ -15,7 +15,7 @@ octo = OctopartClient(os.getenv('OCTOPART_TOKEN', None)) db = PartsDB() -def add_part(mpn): +def add_part(mpn, quantity, category, storage, part_type): result = octo.get_part(mpn)['data']['search']['results'] if result is None: print(f"Can't find results for {sys.argv[1]} on Octopart") @@ -30,7 +30,8 @@ def add_part(mpn): pick = int(input("Which one seems better ? ")) p = result[pick]['part'] - quantity = int(input("How many of them ? ")) + if quantity is None: + quantity = int(input("How many of them ? ")) # if this exists we increment stock spart = db.get_part_by_mpn(p['mpn']) @@ -39,22 +40,31 @@ def add_part(mpn): db.new_part_history_event(spart['id'], quantity, "new buy") return + # only ask for categories if we do not have one already # list categories to choose from - for c in db.get_categories(): - print(f"{c[0]}) {c[1]}") - category = int(input("In which category do you want it in ? ")) + if category is None: + for c in db.get_categories(): + print(f"{c['id']}) {c['name']}") + category = int(input("In which category do you want it in ? ")) + # only ask for storage if we do not have one already # list storages to choose from - for s in db.get_storages(): - print(f"{s[0]}) {s[1]}") - storage = int(input("Where will you store it ? ")) - - smd = input("Is this an SMD part (y/n, default yes) ? ") + if storage is None: + for s in db.get_storages(): + print(f"{s['id']}) {s['name']}") + storage = int(input("Where will you store it ? ")) + + # only ask for part type if we do not have one already + if part_type == 'none': + smd = input("Is this an SMD part (y/n, default yes) ? ") + if smd == 'n' or smd == 'N': + part_type = 'th' + else: + part_type = 'smd' footprint = None datasheet = None image = None - part_type = 'smd' if 'specs' in p: for s in p['specs']: @@ -80,9 +90,6 @@ def add_part(mpn): req = urllib.request.Request(image, headers=headers) image = urllib.request.urlopen(req).read() - if smd == 'n' or smd == 'N': - part_type = 'through-hole' - part = [ p['mpn'], p['mpn'], @@ -158,6 +165,14 @@ if __name__ == '__main__': # add ap_add = asp.add_parser("add", help="Add new part from Octopart") ap_add.add_argument("mpn", help="Manufacturer part number") + ap_add.add_argument("-q", dest='quantity', + help="Quantity of new items", type=int) + ap_add.add_argument("-c", dest='category', + help="Which categoryId it belongs to", type=int) + ap_add.add_argument("-s", dest='storage', + help="Which storageId it belongs to", type=int) + ap_add.add_argument("-t", dest='type', choices=['smd', 'th'], + default='none', help="Trhough-hole of smd ?") # cat ap_cat = asp.add_parser("cat", help="List categories") # list @@ -194,7 +209,8 @@ if __name__ == '__main__': sys.exit(0) if args.command == 'add': - add_part(args.mpn) + add_part(args.mpn, args.quantity, args.category, args.storage, + args.type) elif args.command == 'list': list_parts(args.category, args.short) elif args.command == 'search':