Skip to content

Commit

Permalink
Merge pull request #126 from sparkmicro/1.0.0a2
Browse files Browse the repository at this point in the history
1.0.0a2
  • Loading branch information
eeintech committed Feb 27, 2023
2 parents 45a94e0 + 6ee989b commit e066813
Show file tree
Hide file tree
Showing 12 changed files with 667 additions and 300 deletions.
8 changes: 4 additions & 4 deletions kintree/config/config_interface.py
Expand Up @@ -392,10 +392,10 @@ def sync_inventree_supplier_categories(inventree_config_path: str, supplier_conf
def add_supplier_category(categories: dict, supplier_config_path: str) -> bool:
''' Add Supplier category mapping to Supplier settings file
categories = {
'Capacitors':
{ 'Tantalum': 'Tantalum Capacitors' }
}
categories = {
'Capacitors':
{ 'Tantalum': 'Tantalum Capacitors' }
}
'''
try:
supplier_categories = load_file(supplier_config_path)
Expand Down
165 changes: 70 additions & 95 deletions kintree/config/inventree/categories.yaml
@@ -1,113 +1,88 @@
# Coding
CODES:
Assemblies: PCA
Capacitors: CAP
Circuit Protections: PRO
Connectors: CON
Crystals and Oscillators: CLK
Diodes: DIO
Inductors: IND
Integrated Circuits: ICS
Mechanicals: MEC
Miscellaneous: MIS
Modules: MOD
Power Management: PWR
Printed-Circuit Boards: PCB
RF: RFC
Resistors: RES
Transistors: TRA
# Parents and Children Categories
CATEGORIES:
Assemblies:
- Printed-Circuit Board Assembly
- Product
Printed-Circuit Board Assembly: null
Product: null
Capacitors:
Aluminium: null
Ceramic:
'0603':
10V:
- X5R
- X7R
16V:
- X5R
- X7R
'0402':
10V:
- X5R
- X7R
16V:
- X5R
- X7R
Aluminium:
Tantalum:
Polymer:
Super Capacitors:
'0402': null
'0603': null
'0805': null
Polymer: null
Super Capacitors: null
Tantalum: null
Circuit Protections:
- TVS
- Fuses
- PTC
Fuses: null
PTC: null
TVS: null
Connectors:
- Battery
- Board-to-Board
- Interface
- Coaxial
- FPC
- Header
# - Wire-to-Board
# - High-Speed
# - RF
# - Power
Battery: null
Board-to-Board: null
Coaxial: null
FPC: null
Header: null
Interface: null
Crystals and Oscillators:
- Crystals
- Oscillators
Crystals: null
Oscillators: null
Diodes:
- Standard
- Schottky
- Zener
- LED
LED: null
Schottky: null
Standard: null
Zener: null
Inductors:
- Power
- Ferrite Bead
# - RF
Ferrite Bead: null
Power: null
Integrated Circuits:
- Logic
- Interface
- Microcontroller
- Memory
- Sensor
# - SoC-CPU-GPU
# - FPGA-ASIC-CPLD
# - Driver
Interface: null
Logic: null
Memory: null
Microcontroller: null
Sensor: null
Mechanicals:
- Switch
- Standoff
- Nuts
- Screws
Nuts: null
Screws: null
Standoff: null
Switch: null
Miscellaneous:
- Batteries
Batteries: null
Modules: null
Printed-Circuit Boards: null
Power Management:
- PMIC
- LDO
- Buck
- Boost
Boost: null
Buck: null
LDO: null
PMIC: null
Printed-Circuit Boards: null
RF:
- Chipset
- Antenna
- Filter
- Shield
Antenna: null
Chipset: null
Filter: null
Shield: null
Resistors:
- Surface Mount
- Through Hole
- Potentiometers
- NTC
# - Array
NTC: null
Potentiometers: null
Surface Mount: null
Through Hole: null
Transistors:
- N-Channel FET
- P-Channel FET
- NPN
- PNP
- Load Switches
# - N+N-Channel FET
# - N+P-Channel FET
Load Switches: null
N-Channel FET: null
NPN: null
P-Channel FET: null
PNP: null
CODES:
Assemblies: PCA
Capacitors: CAP
Circuit Protections: PRO
Connectors: CON
Crystals and Oscillators: CLK
Diodes: DIO
Inductors: IND
Integrated Circuits: ICS
Mechanicals: MEC
Miscellaneous: MIS
Modules: MOD
Power Management: PWR
Printed-Circuit Boards: PCB
RF: RFC
Resistors: RES
Transistors: TRA
22 changes: 13 additions & 9 deletions kintree/config/settings.py
Expand Up @@ -338,14 +338,18 @@ def set_enable_flag(key: str, value: bool):
global CONFIG_GENERAL

user_settings = CONFIG_GENERAL
if key == 'kicad':
user_settings['ENABLE_KICAD'] = value
elif key == 'inventree':
user_settings['ENABLE_INVENTREE'] = value
elif key == 'alternate':
user_settings['ENABLE_ALTERNATE'] = value

# Save
config_interface.dump_file(user_settings, os.path.join(CONFIG_USER_FILES, 'general.yaml'))
if key in ['kicad', 'inventree', 'alternate']:
if key == 'kicad':
user_settings['ENABLE_KICAD'] = value
elif key == 'inventree':
user_settings['ENABLE_INVENTREE'] = value
elif key == 'alternate':
user_settings['ENABLE_ALTERNATE'] = value

# Save
config_interface.dump_file(
data=user_settings,
file_path=os.path.join(CONFIG_USER_FILES, 'general.yaml'),
)

return reload_enable_flags()
35 changes: 35 additions & 0 deletions kintree/database/inventree_api.py
Expand Up @@ -72,6 +72,41 @@ def get_inventree_category_id(category_tree: list) -> int:
return -1


def get_categories() -> dict:
'''Fetch InvenTree categories'''
global inventree_api

categories = {}
# Get all categories (list)
db_categories = PartCategory.list(inventree_api)

def deep_add(tree: dict, keys: list, item: dict):
if len(keys) == 1:
try:
tree[keys[0]].update(item)
except (KeyError, AttributeError):
tree[keys[0]] = item
return
return deep_add(tree.get(keys[0]), keys[1:], item)

for category in db_categories:
parent = category.getParentCategory()
children = category.getChildCategories()

if not parent and not children:
categories[category.name] = None
continue
elif parent:
parent_list = []
while parent:
parent_list.insert(0, parent.name)
parent = parent.getParentCategory()
cat = {category.name: None}
deep_add(categories, parent_list, cat)

return categories


def get_category_parameters(category_id: int) -> list:
''' Get all default parameter templates for category '''
global inventree_api
Expand Down
60 changes: 55 additions & 5 deletions kintree/database/inventree_interface.py
Expand Up @@ -8,6 +8,8 @@
from fuzzywuzzy import fuzz
from ..search import search_api, digikey_api, mouser_api, element14_api, lcsc_api

category_separator = '/'


def connect_to_server(timeout=5) -> bool:
''' Connect to InvenTree server using user settings '''
Expand Down Expand Up @@ -41,6 +43,58 @@ def connect_to_server(timeout=5) -> bool:
return connect


def category_tree(tree: str) -> str:
import re
find_prefix = re.match(r'^-+ (.+?)$', tree)
if find_prefix:
return find_prefix.group(1)
return tree


def split_category_tree(tree: str) -> list:
return category_tree(tree).split(category_separator)


def build_category_tree(reload=False, category=None) -> dict:
'''Build InvenTree category tree from database data'''

category_data = config_interface.load_file(settings.CONFIG_CATEGORIES)

def build_tree(tree, left_to_go, level) -> list:
try:
last_entry = f' {category_tree(tree[-1])}{category_separator}'
except IndexError:
last_entry = ''
if type(left_to_go) == dict:
for key, value in left_to_go.items():
tree.append(f'{"-" * level}{last_entry}{key}')
build_tree(tree, value, level + 1)
elif type(left_to_go) == list:
# Supports legacy structure
for item in left_to_go:
tree.append(f'{"-" * level}{last_entry}{item}')
elif left_to_go is None:
pass
return

if reload:
categories = inventree_api.get_categories()
category_data.update({'CATEGORIES': categories})
config_interface.dump_file(category_data, settings.CONFIG_CATEGORIES)
else:
categories = category_data.get('CATEGORIES', {})

# Get specified branch
if category:
categories = {category: categories.get(category, {})}

inventree_categories = []
# Build category tree
build_tree(inventree_categories, categories, 0)

return inventree_categories


def get_categories_from_supplier_data(part_info: dict, supplier_only=False) -> list:
''' Find categories from part supplier data, use "somewhat automatic" matching '''
categories = [None, None]
Expand Down Expand Up @@ -598,10 +652,6 @@ def inventree_create_alternate(part_info: dict, part_id='', part_ipn='', show_pr
cprint('\n[MAIN]\tSearching for original part in database', silent=settings.SILENT)
part = inventree_api.fetch_part(part_id, part_ipn)

# Progress Update
if not progress.update_progress_bar(show_progress, increment=0.05):
return

if part:
part_pk = part.pk
part_description = part.description
Expand All @@ -624,7 +674,7 @@ def inventree_create_alternate(part_info: dict, part_id='', part_ipn='', show_pr
description=part_description)

# Progress Update
if not progress.update_progress_bar(show_progress, increment=0.05):
if not progress.update_progress_bar(show_progress, increment=0.5):
return

supplier_name = part_info.get('supplier_name', '')
Expand Down

0 comments on commit e066813

Please sign in to comment.