Skip to content

Commit

Permalink
Merge pull request #138 from sparkmicro/1.0.0b2
Browse files Browse the repository at this point in the history
1.0.0b2
  • Loading branch information
eeintech committed Mar 23, 2023
2 parents bb05c74 + fb6e31f commit cdb24b5
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 641 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TOKEN_DIGIKEY: ${{ secrets.TOKEN_DIGIKEY }}

continue-on-error: true
strategy:
matrix:
python-version: ['3.8', '3.10']
Expand Down Expand Up @@ -82,6 +83,13 @@ jobs:
cp tests/files/kicad_map.yaml ~/.config/kintree/user/
cp tests/files/results.tgz ~/.config/kintree/cache/search/
cd ~/.config/kintree/cache/search/ && tar xvf results.tgz && cd -
- name: GUI test
run: |
python kintree_gui.py b > gui.log 2>&1 &
sleep 2
cat gui.log
export len_log=$(cat gui.log | wc -l)
[[ ${len_log} -eq 0 ]] && true || false
- name: Digi-Key API setup
run: |
git clone https://$TOKEN_DIGIKEY@github.com/eeintech/digikey-token.git
Expand Down
2 changes: 1 addition & 1 deletion kintree/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=
except (urllib.error.URLError, ValueError):
cprint(f'[INFO]\tWarning: {filetype} download failed (URL Error)', silent=silent)
except FileNotFoundError:
cprint(f'[INFO]\tWarning: {os.path.dirname(fileoutput)} folder does not exist')
cprint(f'[INFO]\tWarning: {os.path.dirname(fileoutput)} folder does not exist', silent=silent)
return None


Expand Down
4 changes: 2 additions & 2 deletions kintree/config/config_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def load_file(file_path: str, silent=True) -> dict:
print(exc)
return None
except FileNotFoundError:
if not silent:
cprint(f'[ERROR] File {file_path} does not exists!')
cprint(f'[ERROR]\tFile {file_path} does not exists!', silent=silent)
return None

return data
Expand Down Expand Up @@ -66,6 +65,7 @@ def load_config(path):
# Join user data to template data
user_settings = {**template_data, **user_data}
except TypeError:
cprint(f'[INFO]\tCreating new {filename} configuration file', silent=silent)
# Config file does not exists
user_settings = template_data

Expand Down
2 changes: 1 addition & 1 deletion kintree/database/inventree_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

def connect(server: str, username: str, password: str, connect_timeout=5, silent=False) -> bool:
''' Connect to InvenTree server and create API object '''
from ..wrapt_timeout_decorator import timeout
from wrapt_timeout_decorator import timeout
global inventree_api

@timeout(dec_timeout=connect_timeout)
Expand Down
10 changes: 7 additions & 3 deletions kintree/database/inventree_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def translate_form_to_inventree(part_info: dict, category_tree: list, is_custom=
inventree_part['supplier_part_number'] = part_info['supplier_part_number']
inventree_part['manufacturer_name'] = part_info['manufacturer_name']
inventree_part['manufacturer_part_number'] = part_info['manufacturer_part_number']
inventree_part['IPN'] = part_info['IPN']
inventree_part['IPN'] = part_info.get('IPN', '')
# Replace whitespaces in URL
inventree_part['supplier_link'] = part_info['supplier_link'].replace(' ', '%20')
inventree_part['datasheet'] = part_info['datasheet'].replace(' ', '%20')
Expand Down Expand Up @@ -663,12 +663,14 @@ def inventree_create_alternate(part_info: dict, part_id='', part_ipn='', show_pr
datasheet = part_info.get('datasheet', '')

# Create manufacturer part
if manufacturer_mpn:
if manufacturer_name and manufacturer_mpn:
inventree_create_manufacturer_part(part_id=part_pk,
manufacturer_name=manufacturer_name,
manufacturer_mpn=manufacturer_mpn,
datasheet=datasheet,
description=part_description)
else:
cprint('[INFO]\tWarning: No manufacturer part to create', silent=settings.SILENT)

# Progress Update
if not progress.update_progress_bar(show_progress, increment=0.2):
Expand All @@ -679,7 +681,7 @@ def inventree_create_alternate(part_info: dict, part_id='', part_ipn='', show_pr
supplier_link = part_info.get('supplier_link', '')

# Add supplier alternate
if supplier_sku:
if supplier_name and supplier_sku:
cprint('\n[MAIN]\tCreating supplier part', silent=settings.SILENT)
is_new_supplier_part = inventree_api.is_new_supplier_part(supplier_name=supplier_name,
supplier_sku=supplier_sku)
Expand All @@ -699,5 +701,7 @@ def inventree_create_alternate(part_info: dict, part_id='', part_ipn='', show_pr
if is_supplier_part_created:
cprint('[INFO]\tSuccess: Added new supplier part', silent=settings.SILENT)
result = True
else:
cprint('[INFO]\tWarning: No supplier part to create', silent=settings.SILENT)

return result
2 changes: 1 addition & 1 deletion kintree/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def init_gui(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.scroll = ft.ScrollMode.ALWAYS

# Theme
update_theme(page)

Expand Down
6 changes: 3 additions & 3 deletions kintree/gui/views/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from math import pi
from typing import Optional
from typing import Optional, List

import flet as ft

Expand Down Expand Up @@ -182,7 +182,7 @@ class SwitchWithRefs(ft.Switch):

def __init__(
self,
refs: list[ft.Ref] = None,
refs: List[ft.Ref] = None,
**kwargs,
):
super().__init__(**kwargs)
Expand Down Expand Up @@ -211,7 +211,7 @@ def refs(self):
return self.linked_refs

@refs.setter
def refs(self, references: list[ft.Ref]):
def refs(self, references: List[ft.Ref]):
if references:
self.linked_refs = []
for ref in references:
Expand Down
108 changes: 77 additions & 31 deletions kintree/gui/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def call_settings(self, e):
handle_transition(self.page, transition=True)
self.page.go('/settings')

def reset_view(self, e, ignore=['enable']):
def reset_view(self, e, ignore=['enable'], hidden={}):
def reset_field(field):
if type(field) is ft.ProgressBar:
field.value = 0
Expand All @@ -160,6 +160,14 @@ def reset_field(field):
else:
if name not in ignore:
reset_field(field)

if hidden:
for key, value in hidden.items():
if not value:
self.data[key] = value
else:
self.data[key] = None

# Clear data
self.push_data()

Expand Down Expand Up @@ -188,12 +196,17 @@ def process_enable(self, e, value=None, ignore=['enable']):
def sanitize_data(self):
return

def push_data(self, e=None, label=None, value=None):
def push_data(self, e=None, hidden={}):
for key, field in self.fields.items():
try:
self.data[key] = field.value
except AttributeError:
pass

if hidden:
for key, value in hidden.items():
self.data[key] = value

# Sanitize data before pushing
self.sanitize_data()
# Push
Expand Down Expand Up @@ -259,6 +272,13 @@ class PartSearchView(MainView):
'search_form': {},
}

def reset_view(self, e, ignore=['enable']):
hidden_fields = {
'searched_part_number': '',
'custom_part': None,
}
return super().reset_view(e, ignore=ignore, hidden=hidden_fields)

def enable_search_fields(self):
for form_field in self.fields['search_form'].values():
form_field.disabled = False
Expand Down Expand Up @@ -336,11 +356,14 @@ def run_search(self, e):
return

def push_data(self, e=None):
self.data['searched_part_number'] = self.fields['part_number'].value
hidden_fields = {
'searched_part_number': self.fields['part_number'].value,
'custom_part': self.data.get('custom_part', None),
}
for key, field in self.fields['search_form'].items():
self.data[key] = field.value
data_from_views[self.title] = self.data

return super().push_data(e, hidden=hidden_fields)
def partial_update(self):
# Update supplier options
self.update_suppliers()
Expand All @@ -367,18 +390,25 @@ def build_column(self):

self.column = ft.Column(
controls=[
ft.Row(),
ft.Row(
controls=[
self.fields['part_number'],
self.fields['supplier'],
self.fields['search_button'],
],
ft.Container(
content=ft.Column(
controls=[
ft.Row(),
ft.Row(
controls=[
self.fields['part_number'],
self.fields['supplier'],
self.fields['search_button'],
],
),
ft.Divider(),
],
scroll=ft.ScrollMode.HIDDEN,
),
expand=True,
),
ft.Divider(),
],
alignment=ft.MainAxisAlignment.START,
scroll=ft.ScrollMode.HIDDEN,
alignment=ft.MainAxisAlignment.END,
expand=True,
)

Expand All @@ -393,9 +423,21 @@ def build_column(self):
expand=True,
on_change=self.push_data,
)
self.column.controls.append(ft.Row([text_field]))
self.column.controls[0].content.controls.append(ft.Row([text_field]))
self.fields['search_form'][field] = text_field

def did_mount(self, enable=False):
if (
not self.fields['part_number'].value
and self.fields['supplier'].value is None
and self.data.get('custom_part', None) is None
):
self.show_dialog(
d_type=DialogType.WARNING,
message='To create a Custom Part click on the Submit button',
)
return super().did_mount(enable)


class InventreeView(MainView):
'''InvenTree categories view'''
Expand Down Expand Up @@ -477,7 +519,7 @@ def process_enable(self, e):
if e.data.lower() == 'false':
inventree_enable = False

super().process_enable(e, value=inventree_enable, ignore=['enable'])
super().process_enable(e, value=inventree_enable, ignore=['enable', 'IPN: Category Code'])
if not inventree_enable:
# If InvenTree disabled
self.fields['alternate'].value = inventree_enable
Expand Down Expand Up @@ -520,7 +562,7 @@ def process_alternate(self, e, value=None):
if alt_visible:
self.show_dialog(
d_type=DialogType.WARNING,
message='Enter Existing Part ID or Part IPN',
message='Alternate Mode Enabled: Enter Existing Part ID or Part IPN',
)

self.push_data(e)
Expand Down Expand Up @@ -754,7 +796,7 @@ def process_enable(self, e, value=None, ignore=['enable']):
self.fields['Footprint'].update()

def push_data(self, e=None, label=None, value=None):
super().push_data(e, label, value)
super().push_data(e)
if label or e:
try:
if 'Footprint Library' in [label, e.control.label]:
Expand Down Expand Up @@ -798,6 +840,9 @@ def check_snapeda(self, e):

def update_footprint_options(self, library: str):
footprint_options = []
if library is None:
return footprint_options

# Load paths
footprint_paths = self.get_footprint_libraries()
# Get path matching selected footprint library
Expand Down Expand Up @@ -1054,13 +1099,13 @@ def create_part(self, e=None):

# Part number check
part_number = data_from_views['Part Search'].get('manufacturer_part_number', None)
if not part_number:
if not custom:
if not custom:
if not part_number:
self.show_dialog(DialogType.ERROR, 'Missing Part Number')
return
else:
# Update IPN (later overwritten)
part_info['IPN'] = part_number
else:
# Update IPN (later overwritten)
part_info['IPN'] = part_number

# Button update
self.enable_create(False)
Expand Down Expand Up @@ -1231,13 +1276,14 @@ def create_part(self, e=None):
# Download datasheet
if settings.DATASHEET_SAVE_ENABLED:
datasheet_url = part_info.get('datasheet', None)
filename = os.path.join(
settings.DATASHEET_SAVE_PATH,
f'{part_info.get("IPN", "datasheet")}.pdf',
)
cprint('\n[MAIN]\tDownloading Datasheet')
if download(datasheet_url, filetype='PDF', fileoutput=filename, timeout=10):
cprint(f'[INFO]\tSuccess: Datasheet saved to {filename}')
if datasheet_url:
filename = os.path.join(
settings.DATASHEET_SAVE_PATH,
f'{part_info.get("IPN", "datasheet")}.pdf',
)
cprint('\n[MAIN]\tDownloading Datasheet')
if download(datasheet_url, filetype='PDF', fileoutput=filename, timeout=10):
cprint(f'[INFO]\tSuccess: Datasheet saved to {filename}')
# Open browser
if settings.ENABLE_INVENTREE:
if part_info.get('inventree_url', None):
Expand Down
9 changes: 4 additions & 5 deletions kintree/kintree_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from .gui.gui import kintree_gui


def main():
browser = False
if browser:
def main(view='flet_app'):
if view == 'browser':
ft.app(target=kintree_gui, view=ft.WEB_BROWSER)
else:
ft.app(target=kintree_gui)
return
ft.app(target=kintree_gui, view=ft.FLET_APP)
2 changes: 1 addition & 1 deletion kintree/search/digikey_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def find_categories(part_details: str):

def fetch_part_info(part_number: str) -> dict:
''' Fetch part data from API '''
from ..wrapt_timeout_decorator import timeout
from wrapt_timeout_decorator import timeout

part_info = {}
if not setup_environment():
Expand Down
2 changes: 1 addition & 1 deletion kintree/search/mouser_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def find_categories(part_details: str):
def fetch_part_info(part_number: str) -> dict:
''' Fetch part data from API '''

from ..wrapt_timeout_decorator import timeout
from wrapt_timeout_decorator import timeout

setup_environment()
part_info = {}
Expand Down
12 changes: 0 additions & 12 deletions kintree/wrapt_timeout_decorator/__init__.py

This file was deleted.

0 comments on commit cdb24b5

Please sign in to comment.