commit deedc8e07c66f69676dd8456ffbec19151774f0f
parent bca54f71abf2c4975d4cffc99a1de54d549c6d3b
Author: Paco Esteban <paco@e1e0.net>
Date: Tue, 16 Mar 2021 16:46:00 +0100
open datasheet automatically
Diffstat:
3 files changed, 54 insertions(+), 20 deletions(-)
diff --git a/database.py b/database.py
@@ -78,6 +78,8 @@ UPDATE_PART_QTY_QUERY = "UPDATE parts SET quantity = ? WHERE id = ?"
GET_PART_QTY = "SELECT quantity FROM parts where id = ?"
DELETE_PART_QUERY = "DELETE FROM parts WHERE id = ?"
DELETE_HISTORY_QUERY = "DELETE FROM parts_history WHERE part_id = ?"
+GET_IMAGE_QUERY = "SELECT image FROM parts WHERE id = ?"
+GET_DATASHEET_QUERY = "SELECT datasheet FROM parts WHERE id = ?"
class PartsDB():
@@ -157,5 +159,15 @@ class PartsDB():
c.execute(DELETE_HISTORY_QUERY, (part_id,))
c.execute(DELETE_PART_QUERY, (part_id,))
+ def get_image(self, part_id):
+ c = self.conn.cursor()
+ c.execute(GET_IMAGE_QUERY, (part_id,))
+ return c.fetchone()
+
+ def get_datasheet(self, part_id):
+ c = self.conn.cursor()
+ c.execute(GET_DATASHEET_QUERY, (part_id,))
+ return c.fetchone()
+
def close(self):
self.conn.close()
diff --git a/helpers.py b/helpers.py
@@ -2,7 +2,9 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
+import subprocess
import tempfile
+import time
def print_parts_list(parts, short=False):
@@ -63,7 +65,7 @@ def print_parts_list(parts, short=False):
)
-def print_part(p, history, get_files=False):
+def print_part(p, history):
print(f"PN: {p['pn']}\tManufacturer: {p['manufacturer']}")
print(f"Category: {p['cat']}\tType: {p['part_type']}"
f"\tFootprint: {p['footprint']}")
@@ -73,20 +75,12 @@ def print_part(p, history, get_files=False):
print(f"Description:\n{p['description']}\n")
print(f"Specs:\n{p['specs']}")
- if get_files:
- # extract the files from the blobs and store them on /tmp
- tmpdir = tempfile.gettempdir()
- basefile = f"{tmpdir}/{p['id']}"
- if p['datasheet'] is not None:
- with open(f'{basefile}.pdf', 'wb') as file:
- file.write(p['datasheet'])
- print(f'Datasheet: {basefile}.pdf')
- if p['image'] is not None:
- with open(f'{basefile}.jpg', 'wb') as file:
- file.write(p['image'])
- print(f'Image: {basefile}.jpg\n')
+ if p['datasheet'] is not None:
+ print("This part has a datasheet available.")
+ if p['image'] is not None:
+ print("This part has an image available.")
- print(f"Quantity: {p['quantity']}")
+ print(f"\nQuantity: {p['quantity']}")
# here should go the historical data.
if history:
print("History:")
@@ -100,3 +94,10 @@ def _sanitize_value(value):
if value is None:
return "-"
return value
+
+
+def open_file(content, extension):
+ with tempfile.NamedTemporaryFile(suffix=extension) as f:
+ f.write(content)
+ subprocess.Popen(['xdg-open', f.name], start_new_session=True)
+ time.sleep(2)
diff --git a/partsdb.py b/partsdb.py
@@ -137,10 +137,26 @@ def search_part(search_term):
print_parts_list(parts)
-def get_part(part_id, files):
+def get_part(part_id):
part = db.get_part(part_id)
history = db.get_part_history(part_id)
- print_part(part, history, files)
+ print_part(part, history)
+
+
+def open_image(part_id):
+ image = db.get_image(part_id)
+ if image is None:
+ print(f"There's no image for this part ID ({part_id})")
+ return
+ open_file(image['image'], '.jpg')
+
+
+def open_datasheet(part_id):
+ datasheet = db.get_datasheet(part_id)
+ if datasheet is None:
+ print(f"There's no datasheet for this part ID ({part_id})")
+ return
+ open_file(datasheet['datasheet'], '.pdf')
def delete_part(part_id):
@@ -195,9 +211,10 @@ if __name__ == '__main__':
# get
ap_get = asp.add_parser("get", help="Get all details for a part")
ap_get.add_argument("part_id", help="Part Id", type=int)
- ap_get.add_argument("-f", dest='files',
- action='store_true',
- help="Extract Files (datasheet, ...")
+ ap_get.add_argument("-d", dest='datasheet', action='store_true',
+ help="Open datasheet if available.")
+ ap_get.add_argument("-i", dest='image', action='store_true',
+ help="Open image if available.")
# delete
ap_delete = asp.add_parser("delete", help="Delete a part")
ap_delete.add_argument("part_id", help="Part Id", type=int)
@@ -224,7 +241,11 @@ if __name__ == '__main__':
elif args.command == 'search':
search_part(args.search_term)
elif args.command == 'get':
- get_part(args.part_id, args.files)
+ get_part(args.part_id)
+ if args.datasheet:
+ open_datasheet(args.part_id)
+ if args.image:
+ open_image(args.part_id)
elif args.command == 'delete':
delete_part(args.part_id)
elif args.command == 'stock':