partsdb

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

helpers.py (4989B)


      1 #!/usr/bin/env python3
      2 # -*- coding: utf-8 -*-
      3 # vim:fenc=utf-8
      4 
      5 import subprocess
      6 import tempfile
      7 import time
      8 
      9 
     10 def print_parts_list(parts, short=False):
     11     if short:
     12         header = (f"| {'ID':4} | "
     13                   f"{'Category':8} | "
     14                   f"{'PN':10} | "
     15                   f"{'Manufacturer':16} | "
     16                   f"{'Description':25} |"
     17                   )
     18         for i, p in enumerate(parts):
     19             if i % 25 == 0:
     20                 print("-"*79)
     21                 print(header)
     22                 print("-"*79)
     23             print(f"| {p['id']:<4} | "
     24                   f"{p['cname'][0:7]:8} | "
     25                   f"{_sanitize_value(p['pn'])[0:9]:10} | "
     26                   f"{_sanitize_value(p['manufacturer'])[0:15]:16} | "
     27                   f"{_sanitize_value(p['description'])[0:24]:25} |"
     28                   )
     29     else:
     30         # max takes an iterable and key is a function where iterables are
     31         # passed and comparison performed, so we look at the longest desc to
     32         # get the lenght of the field.  We also check if the header is longer,
     33         # the result is whatever is longer
     34         l_pn = len(max(parts, key=lambda k: len(k['pn']))['pn'])
     35         l_cat = len(max(parts,
     36                         key=lambda k: len(k['cname']))['cname'])
     37         l_man = len(max(parts,
     38                         key=lambda k: len(k['manufacturer']))['manufacturer'])
     39         l_desc = len(max(parts,
     40                          key=lambda k: len(k['description']))['description'])
     41         l_cat = l_cat if l_cat > len('Category') else len('Category')
     42         l_man = l_man if l_man > len('Manufacturer') else len('Manufacturer')
     43         l_desc = l_desc if l_desc > len('Description') else len('Description')
     44 
     45         header = (f"| {'ID':5} | {'PN':{l_pn}} | "
     46                   f"{'Category':{l_cat}} | "
     47                   f"{'Manufacturer':{l_man}} | "
     48                   f"{'Description':{l_desc}} | "
     49                   f"{'Type':4} | "
     50                   f"{'Footp':6} | "
     51                   f"{'Qty':4} |"
     52                   )
     53         for i, p in enumerate(parts):
     54             if i % 25 == 0:
     55                 print("-"*len(header))
     56                 print(header)
     57                 print("-"*len(header))
     58             print(f"| {p['id']:<5} | {p['pn']:{l_pn}} | "
     59                   f"{p['cname']:{l_cat}} | "
     60                   f"{_sanitize_value(p['manufacturer']):{l_man}} | "
     61                   f"{_sanitize_value(p['description']):{l_desc}} | "
     62                   f"{_sanitize_value(p['part_type'])[0:3]:4} | "
     63                   f"{_sanitize_value(p['footprint'])[0:5]:6} | "
     64                   f"{p['quantity']:4} |"
     65                   )
     66 
     67 
     68 def print_part(p, history):
     69     print(f"PN: {p['pn']}\tManufacturer: {p['manufacturer']}")
     70     print(f"Category: {p['cat']}\tType: {p['part_type']}"
     71           f"\tFootprint: {p['footprint']}")
     72     print(f"Storage: {p['storage']}")
     73     print(f"Created: {p['insert_date']}"
     74           f"\tUpdated: {p['update_date']}\n")
     75     print(f"Description:\n{p['description']}\n")
     76     print(f"Specs:\n{p['specs']}")
     77 
     78     if p['datasheet'] is not None:
     79         print("This part has a datasheet available.")
     80     if p['image'] is not None:
     81         print("This part has an image available.")
     82 
     83     print(f"\nQuantity: {p['quantity']}")
     84     # here should go the historical data.
     85     if history:
     86         print("History:")
     87         for h in history:
     88             print(f"{h['insert_date']} | "
     89                   f"{h['movement']:4} | "
     90                   f"{h['mcomment']}")
     91 
     92 
     93 def _sanitize_value(value):
     94     if value is None:
     95         return "-"
     96     return value
     97 
     98 
     99 def open_file(content, extension):
    100     with tempfile.NamedTemporaryFile(suffix=extension) as f:
    101         f.write(content)
    102         subprocess.Popen(['xdg-open', f.name], start_new_session=True)
    103         time.sleep(2)
    104 
    105 
    106 def html_main_index(dest_folder, categories, env):
    107     tpl = env.get_template('index.html')
    108     with open(f'{dest_folder}/index.html', 'w') as f:
    109         f.write(tpl.render(cat=categories))
    110 
    111 
    112 def html_category_index(dest_folder, category, parts, env):
    113     tpl = env.get_template('cat.html')
    114     with open(f"{dest_folder}/cat_list_{category['id']}.html", 'w') as f:
    115         f.write(tpl.render(category=category, parts=parts))
    116 
    117 
    118 def html_part(dest_folder, part, part_history, env):
    119     tpl = env.get_template('part.html')
    120     with open(f"{dest_folder}/part_{part['id']}.html", 'w') as f:
    121         f.write(tpl.render(part=part, history=part_history))
    122 
    123 
    124 def html_css(dest_folder, env):
    125     tpl = env.get_template('style.css')
    126     with open(f"{dest_folder}/style.css", 'w') as f:
    127         f.write(tpl.render())
    128 
    129 
    130 def html_attachments(dest_folder, part_id, datasheet, image):
    131     if datasheet['datasheet'] is not None:
    132         with open(f"{dest_folder}/part_datasheet_{part_id}.pdf", 'wb') as f:
    133             f.write(datasheet['datasheet'])
    134     if image['image'] is not None:
    135         with open(f"{dest_folder}/part_image_{part_id}.jpg", 'wb') as f:
    136             f.write(image['image'])