partsdb

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

commit cffded8bbd2890aa31caf2b13bbafd59586f51b3
parent dd886476448cedbdb13b2c20e5f7a4f6b9aadb9e
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat, 30 Oct 2021 19:22:26 +0200

remove -s on parts list and add -f for format

It can be one of 'full' (default), 'short' which replaces '-s' or
'json'.

Also, bump version.

Diffstat:
Mpartsdb/helpers.py | 128++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mpartsdb/partsdb.py | 7++++---
Msetup.py | 2+-
3 files changed, 77 insertions(+), 60 deletions(-)

diff --git a/partsdb/helpers.py b/partsdb/helpers.py @@ -2,67 +2,83 @@ # -*- coding: utf-8 -*- # vim:fenc=utf-8 +import json import subprocess import tempfile import time -def print_parts_list(parts, short=False): - if short: - header = (f"| {'ID':4} | " - f"{'Category':8} | " - f"{'PN':10} | " - f"{'Manufacturer':16} | " - f"{'Description':25} |" - ) - for i, p in enumerate(parts): - if i % 25 == 0: - print("-"*79) - print(header) - print("-"*79) - print(f"| {p['id']:<4} | " - f"{p['cname'][0:7]:8} | " - f"{_sanitize_value(p['pn'])[0:9]:10} | " - f"{_sanitize_value(p['manufacturer'])[0:15]:16} | " - f"{_sanitize_value(p['description'])[0:24]:25} |" - ) - else: - # max takes an iterable and key is a function where iterables are - # passed and comparison performed, so we look at the longest desc to - # get the lenght of the field. We also check if the header is longer, - # the result is whatever is longer - l_pn = len(max(parts, key=lambda k: len(k['pn']))['pn']) - l_cat = len(max(parts, - key=lambda k: len(k['cname']))['cname']) - l_man = len(max(parts, - key=lambda k: len(k['manufacturer']))['manufacturer']) - l_desc = len(max(parts, - key=lambda k: len(k['description']))['description']) - l_cat = l_cat if l_cat > len('Category') else len('Category') - l_man = l_man if l_man > len('Manufacturer') else len('Manufacturer') - l_desc = l_desc if l_desc > len('Description') else len('Description') - - header = (f"| {'ID':5} | {'PN':{l_pn}} | " - f"{'Category':{l_cat}} | " - f"{'Manufacturer':{l_man}} | " - f"{'Description':{l_desc}} | " - f"{'Type':4} | " - f"{'Footp':6} | " - f"{'Qty':4} |" - ) - for i, p in enumerate(parts): - if i % 25 == 0: - print("-"*len(header)) - print(header) - print("-"*len(header)) - print(f"| {p['id']:<5} | {p['pn']:{l_pn}} | " - f"{p['cname']:{l_cat}} | " - f"{_sanitize_value(p['manufacturer']):{l_man}} | " - f"{_sanitize_value(p['description']):{l_desc}} | " - f"{_sanitize_value(p['part_type'])[0:3]:4} | " - f"{_sanitize_value(p['footprint'])[0:5]:6} | " - f"{p['quantity']:4} |" - ) +def print_parts_list(parts, format='full'): + if format == 'short': + _list_ascii_short(parts) + if format == 'full': + _list_ascii(parts) + if format == 'json': + _list_json(parts) + + +def _list_ascii(parts): + # max takes an iterable and key is a function where iterables are + # passed and comparison performed, so we look at the longest desc to + # get the lenght of the field. We also check if the header is longer, + # the result is whatever is longer + l_pn = len(max(parts, key=lambda k: len(k['pn']))['pn']) + l_cat = len(max(parts, + key=lambda k: len(k['cname']))['cname']) + l_man = len(max(parts, + key=lambda k: len(k['manufacturer']))['manufacturer']) + l_desc = len(max(parts, + key=lambda k: len(k['description']))['description']) + l_cat = l_cat if l_cat > len('Category') else len('Category') + l_man = l_man if l_man > len('Manufacturer') else len('Manufacturer') + l_desc = l_desc if l_desc > len('Description') else len('Description') + + header = (f"| {'ID':5} | {'PN':{l_pn}} | " + f"{'Category':{l_cat}} | " + f"{'Manufacturer':{l_man}} | " + f"{'Description':{l_desc}} | " + f"{'Type':4} | " + f"{'Footp':6} | " + f"{'Qty':4} |" + ) + for i, p in enumerate(parts): + if i % 25 == 0: + print("-"*len(header)) + print(header) + print("-"*len(header)) + print(f"| {p['id']:<5} | {p['pn']:{l_pn}} | " + f"{p['cname']:{l_cat}} | " + f"{_sanitize_value(p['manufacturer']):{l_man}} | " + f"{_sanitize_value(p['description']):{l_desc}} | " + f"{_sanitize_value(p['part_type'])[0:3]:4} | " + f"{_sanitize_value(p['footprint'])[0:5]:6} | " + f"{p['quantity']:4} |" + ) + + +def _list_ascii_short(parts): + header = (f"| {'ID':4} | " + f"{'Category':8} | " + f"{'PN':10} | " + f"{'Manufacturer':16} | " + f"{'Description':25} |" + ) + for i, p in enumerate(parts): + if i % 25 == 0: + print("-"*79) + print(header) + print("-"*79) + print(f"| {p['id']:<4} | " + f"{p['cname'][0:7]:8} | " + f"{_sanitize_value(p['pn'])[0:9]:10} | " + f"{_sanitize_value(p['manufacturer'])[0:15]:16} | " + f"{_sanitize_value(p['description'])[0:24]:25} |" + ) + + +def _list_json(parts): + p = [dict(zip([key for key in part.keys()], part)) for part in parts] + print(json.dumps({'parts': p})) def print_part(p, history): diff --git a/partsdb/partsdb.py b/partsdb/partsdb.py @@ -229,8 +229,9 @@ def main(): ap_list = asp.add_parser("list", help="List all parts from a category (or all)") ap_list.add_argument("category", help="Category Name") - ap_list.add_argument("-s", dest='short', - action='store_true', help="Short output") + ap_list.add_argument("-f", dest='format', + choices=['full', 'short', 'json'], default='full', + help="Short output") # search ap_search = asp.add_parser("search", help="Search for parts") ap_search.add_argument("search_term", help="Term to search for") @@ -263,7 +264,7 @@ def main(): add_part(args.mpn, args.quantity, args.category, args.storage, args.type) elif args.command == 'list': - list_parts(args.category, args.short) + list_parts(args.category, args.format) elif args.command == 'search': search_part(args.search_term) elif args.command == 'get': diff --git a/setup.py b/setup.py @@ -7,7 +7,7 @@ long_description = (here / 'README.md').read_text(encoding='utf-8') setup( name='partsdb', - version='1.2.5', + version='1.3.0', description='Electronic parts database', long_description=long_description, long_description_content_type='text/markdown',