Skip to content

Commit

Permalink
Merge 6568c05 into 84be9dd
Browse files Browse the repository at this point in the history
  • Loading branch information
eeintech committed Nov 2, 2022
2 parents 84be9dd + 6568c05 commit 085ad10
Show file tree
Hide file tree
Showing 20 changed files with 755 additions and 279 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

strategy:
matrix:
python-version: ['3.8', '3.10']
python-version: ['3.9', '3.10']

steps:
- name: Checkout code
Expand All @@ -45,13 +45,14 @@ jobs:
INVENTREE_DB_NAME: ${{ github.workspace }}/InvenTree/inventree_default_db.sqlite3
INVENTREE_MEDIA_ROOT: ${{ github.workspace }}/InvenTree
INVENTREE_STATIC_ROOT: ${{ github.workspace }}/InvenTree/static
INVENTREE_BACKUP_DIR: ${{ github.workspace }}/InvenTree/backup
INVENTREE_ENV: 0
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TOKEN_DIGIKEY: ${{ secrets.TOKEN_DIGIKEY }}

strategy:
matrix:
python-version: ['3.8', '3.10']
python-version: ['3.9', '3.10']

steps:
- name: Checkout code
Expand All @@ -74,7 +75,7 @@ jobs:
run: |
invoke install
mkdir -p ~/.config/kintree/user/ && mkdir -p ~/.config/kintree/cache/search/
cp tests/files/inventree_test.yaml ~/.config/kintree/user/
cp tests/files/inventree_dev.yaml ~/.config/kintree/user/
cp tests/files/results.tgz ~/.config/kintree/cache/search/
cd ~/.config/kintree/cache/search/ && tar xvf results.tgz && cd -
- name: Digi-Key API setup
Expand Down
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Ki-nTree was developped by [@eeintech](https://github.com/eeintech) for [SPARK M

### Requirements

* Ki-nTree is currently tested for Python 3.8 to 3.10 versions.
* Ki-nTree is currently tested for Python 3.9 to 3.10 versions.
* Ki-nTree requires a Digi-Key **production** API instance. To create one, go to https://developer.digikey.com/. Create an account, an organization and add a **production** API to your organization. Save both Client ID and Secret keys.
> [Here is a video](https://youtu.be/OI1EGEc0Ju0) to help with the different steps
* Ki-nTree requires a Mouser Search API key. To request one, head over to https://www.mouser.ca/api-search/ and click on "Sign Up for Search API"
Expand Down Expand Up @@ -134,6 +134,19 @@ Lastly, a new page will open with a "You may now close this window." message, pr
</p>
</details>

#### Part Parameters

Ki-nTree uses **supplier** parameters to populate **InvenTree** parameters. In order to match between supplier and InvenTree, users need to setup the config file `~/.config/kintree/user/supplier_parameters.yaml` with the following mapping for each category:
``` yaml
CATEGORY_NAME:
INVENTREE_PARAMETER_NAME:
- SUPPLIER_1_PARAMETER_NAME_1
- SUPPLIER_1_PARAMETER_NAME_2
- SUPPLIER_2_PARAMETER_NAME_1
```

Refer to [this file](https://github.com/sparkmicro/Ki-nTree/blob/main/kintree/config/search/supplier_parameters.yaml) as a starting point / example.

#### Part Number Search

Ki-nTree currently supports APIs for the following electronics suppliers: Digi-Key, Mouser and LCSC.
Expand Down Expand Up @@ -187,11 +200,11 @@ $ python -m kintree_gui
1. Make sure you followed the previous installation steps, then run:
``` bash
$ poetry build
Building kintree (0.4.99)
Building kintree (0.6.99)
- Building sdist
- Built kintree-0.4.99.tar.gz
- Built kintree-0.6.99.tar.gz
- Building wheel
- Built kintree-0.4.99-py3-none-any.whl
- Built kintree-0.6.99-py3-none-any.whl
```
2. Exit the virtual environment (`Ctrl + D` on Linux; you can also close the
terminal and reopen it in the same folder).
Expand All @@ -200,7 +213,7 @@ Building kintree (0.4.99)
step. For example:

```bash
pip install dist/kintree-0.4.99-py3-none-any.whl
pip install dist/kintree-0.6.99-py3-none-any.whl
```

3. You can now start Ki-nTree by typing `kintree` in the terminal, provided
Expand Down
2 changes: 1 addition & 1 deletion images/python_versions.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion kintree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# VERSION INFORMATION
version_info = {
'MAJOR_REVISION': 0,
'MINOR_REVISION': 5,
'MINOR_REVISION': 6,
'RELEASE_STATUS': 99, # Dev = 99
}

Expand Down
1 change: 0 additions & 1 deletion kintree/config/inventree/inventree_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ PASSWORD: !!binary |
''
SERVER_ADDRESS: ''
USERNAME: ''
ENABLE: false
INVENTREE_DEFAULT_REV: 'A'
1 change: 0 additions & 1 deletion kintree/config/inventree/inventree_prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ PASSWORD: !!binary |
''
SERVER_ADDRESS: ''
USERNAME: ''
ENABLE: false
INVENTREE_DEFAULT_REV: 'A'
6 changes: 0 additions & 6 deletions kintree/config/inventree/inventree_test.yaml

This file was deleted.

76 changes: 32 additions & 44 deletions kintree/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def load_user_config():
AUTOMATIC_SUBCATEGORY_CREATE = CONFIG_GENERAL.get('AUTOMATIC_SUBCATEGORY_CREATE', False)
AUTOMATIC_BROWSER_OPEN = CONFIG_GENERAL.get('AUTOMATIC_BROWSER_OPEN', False)
DEFAULT_SUPPLIER = CONFIG_GENERAL.get('DEFAULT_SUPPLIER', 'Digi-Key')
# Load enable flags
try:
ENABLE_KICAD = CONFIG_GENERAL.get('ENABLE_KICAD', False)
ENABLE_INVENTREE = CONFIG_GENERAL.get('ENABLE_INVENTREE', False)
ENABLE_ALTERNATE = CONFIG_GENERAL.get('ENABLE_ALTERNATE', False)
except TypeError:
pass

# Supported suppliers APIs
SUPPORTED_SUPPLIERS_API = ['Digi-Key', 'Mouser', 'LCSC']
Expand Down Expand Up @@ -179,33 +186,18 @@ def load_kicad_settings():
global KICAD_SYMBOLS_PATH
global KICAD_TEMPLATES_PATH
global KICAD_FOOTPRINTS_PATH
global ENABLE_KICAD

kicad_user_settings = config_interface.load_file(CONFIG_KICAD, silent=False)
if kicad_user_settings:
KICAD_SYMBOLS_PATH = kicad_user_settings.get('KICAD_SYMBOLS_PATH', None)
KICAD_TEMPLATES_PATH = kicad_user_settings.get('KICAD_TEMPLATES_PATH', None)
KICAD_FOOTPRINTS_PATH = kicad_user_settings.get('KICAD_FOOTPRINTS_PATH', None)
ENABLE_KICAD = kicad_user_settings.get('KICAD_ENABLE', None)


# Load kicad settings
load_kicad_settings()


# Enable flag
def set_kicad_enable_flag(value: bool, save=False):
global ENABLE_KICAD
ENABLE_KICAD = value
if save:
global CONFIG_KICAD
kicad_user_settings = config_interface.load_inventree_user_settings(
CONFIG_KICAD)
kicad_user_settings['KICAD_ENABLE'] = value
config_interface.dump_file(kicad_user_settings, CONFIG_KICAD)
return


def set_default_supplier(value: str, save=False):
global DEFAULT_SUPPLIER
DEFAULT_SUPPLIER = value
Expand Down Expand Up @@ -241,13 +233,11 @@ def set_default_supplier(value: str, save=False):
# INVENTREE
class Environment(Enum):
'''
Local Development/Testing: TESTING
Server/Remote Development: DEVELOPMENT
Server/Remote Production: PRODUCTION
'''
TESTING = 0
DEVELOPMENT = 1
PRODUCTION = 2
DEVELOPMENT = 0
PRODUCTION = 1


# Pick environment
Expand All @@ -257,40 +247,17 @@ class Environment(Enum):
try:
environment = int(environment)
except TypeError:
environment = Environment.TESTING.value
environment = 0

# Load correct user file
if environment == Environment.PRODUCTION.value:
CONFIG_INVENTREE = os.path.join(CONFIG_USER_FILES, 'inventree_prod.yaml')
elif environment == Environment.DEVELOPMENT.value:
CONFIG_INVENTREE = os.path.join(CONFIG_USER_FILES, 'inventree_dev.yaml')
else:
CONFIG_INVENTREE = os.path.join(CONFIG_USER_FILES, 'inventree_test.yaml')
CONFIG_INVENTREE = os.path.join(CONFIG_USER_FILES, 'inventree_dev.yaml')

# Load user settings
inventree_settings = config_interface.load_inventree_user_settings(CONFIG_INVENTREE)

# Enable flag
try:
ENABLE_INVENTREE = inventree_settings.get('ENABLE', False)
except TypeError:
pass


def set_inventree_enable_flag(value: bool, save=False):
global ENABLE_INVENTREE
ENABLE_INVENTREE = value
if save:
global CONFIG_INVENTREE
inventree_settings = config_interface.load_inventree_user_settings(CONFIG_INVENTREE)
inventree_settings['ENABLE'] = value
config_interface.save_inventree_user_settings(enable=inventree_settings['ENABLE'],
server=inventree_settings.get('SERVER_ADDRESS', None),
username=inventree_settings.get('USERNAME', None),
password=inventree_settings.get('PASSWORD', None),
user_config_path=CONFIG_INVENTREE)
return


# Server settings
def load_inventree_settings():
Expand Down Expand Up @@ -333,3 +300,24 @@ def load_inventree_settings():
'supplier_link': None,
'parameters': {},
}


# Enable flags
def set_enable_flags(values: list):
global CONFIG_GENERAL
global ENABLE_KICAD
global ENABLE_INVENTREE
global ENABLE_ALTERNATE

ENABLE_KICAD = values[0]
ENABLE_INVENTREE = values[1]
ENABLE_ALTERNATE = values[2]

# Save user settings
user_settings = CONFIG_GENERAL
user_settings['ENABLE_KICAD'] = ENABLE_KICAD
user_settings['ENABLE_INVENTREE'] = ENABLE_INVENTREE
user_settings['ENABLE_ALTERNATE'] = ENABLE_ALTERNATE
config_interface.dump_file(user_settings, os.path.join(CONFIG_USER_FILES, 'general.yaml'))

return
6 changes: 5 additions & 1 deletion kintree/config/settings/general.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
AUTOMATIC_SUBCATEGORY_CREATE: false
AUTOMATIC_BROWSER_OPEN: true
INVENTREE_ENV: null
INVENTREE_ENV: null
DEFAULT_SUPPLIER: Digi-Key
ENABLE_KICAD: false
ENABLE_INVENTREE: false
ENABLE_ALTERNATE: false
53 changes: 50 additions & 3 deletions kintree/database/inventree_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,46 @@ def set_part_number(part_id: int, ipn: str) -> bool:
return False


def get_part_from_ipn(part_ipn='') -> int:
''' Get Part ID from Part IPN '''
global inventree_api

parts = Part.list(inventree_api)

for part in parts:
if part.IPN == part_ipn:
return part

# No part found
return None


def fetch_part(part_id='', part_ipn='') -> int:
''' Fetch part from database using either ID or IPN '''
from requests.exceptions import HTTPError
global inventree_api

part = None
if part_id:
try:
part = Part(inventree_api, part_id)
except TypeError:
# Part ID is invalid (eg. decimal value)
cprint('[TREE] Error: Part ID type is invalid')
except ValueError:
# Part ID is not a positive integer
cprint('[TREE] Error: Part ID must be positive')
except HTTPError:
# Part ID does not exist
cprint('[TREE] Error: Part ID does not exist in database')
elif part_ipn:
part = get_part_from_ipn(part_ipn)
else:
pass

return part


def is_new_part(category_id: int, part_info: dict) -> int:
''' Check if part exists based on parameters (or description) '''
global inventree_api
Expand Down Expand Up @@ -309,16 +349,23 @@ def create_company(company_name: str, manufacturer=False, supplier=False) -> boo
return company


def get_company_id(company_name: str) -> int:
''' Get company (supplier/manufacturer) primary key (ID) '''
def get_all_companies() -> dict:
''' Get all existing companies (supplier/manufacturer) from database '''
global inventree_api

company_list = Company.list(inventree_api)
companies = {}
for company in company_list:
companies[company.name] = company.pk

return companies


def get_company_id(company_name: str) -> int:
''' Get company (supplier/manufacturer) primary key (ID) '''

try:
return companies[company_name]
return get_all_companies()[company_name]
except:
return 0

Expand Down

0 comments on commit 085ad10

Please sign in to comment.