Skip to content

Commit

Permalink
Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hildogjr committed Aug 13, 2019
1 parent 7e0053d commit d4466c3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pip-log.txt
nosetests.xml
htmlcov

# Test results
tests/*.xlsx
tests/*.log

# Translations
*.mo

Expand Down
6 changes: 3 additions & 3 deletions kicost/distributors/api_octopart.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
from urllib.parse import quote_plus as urlquote

# KiCost definitions.
from ..global_vars import logger, DEBUG_OVERVIEW, DEBUG_OBSESSIVE # Debug configurations.
from ..global_vars import SEPRTR
from .global_vars import * # Debug information, `distributor_dict` and `SEPRTR`.
from ..edas.tools import order_refs

# Distributors definitions.
from .distributor import distributor_class
Expand All @@ -47,12 +47,12 @@
OCTOPART_MAX_PARTBYQUERY = 20 # Maximum part list length to one single query.

__all__ = ['api_octopart']
from .distributors_info import distributors_info

class api_octopart(distributor_class):

@staticmethod
def init_dist_dict():
from .distributors_info import distributors_info
api_distributors = ['arrow', 'digikey', 'farnell', 'mouser', 'newark', 'rs', 'tme']
dists = {k:v for k,v in distributors_info.items() if k in api_distributors}
if not 'enabled' in distributors_modules_dict['api_octopart']:
Expand Down
66 changes: 35 additions & 31 deletions kicost/distributors/api_partinfo_kitspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

# KiCost definitions.
from .global_vars import * # Debug information, `distributor_dict` and `SEPRTR`.
from ..edas.tools import order_refs

# Distributors definitions.
from .distributor import distributor_class
Expand Down Expand Up @@ -68,12 +69,12 @@
QUERY_URL = 'https://dev-partinfo.kitspace.org/graphql'

__all__ = ['api_partinfo_kitspace']
from .distributors_info import distributors_info

class api_partinfo_kitspace(distributor_class):

@staticmethod
def init_dist_dict():
from .distributors_info import distributors_info
api_distributors = ['digikey', 'farnell', 'mouser', 'newark', 'rs']
dists = {k:v for k,v in distributors_info.items() if k in api_distributors}
if not 'enabled' in distributors_modules_dict['api_partinfo_kitspace']:
Expand Down Expand Up @@ -187,7 +188,7 @@ def get_part_info(query, parts, distributor_info=None):
for part_query, part, dist_info, result in zip(query, parts, distributor_info, results['data']['match']):

if not result:
logger.warning('No information found for part {}'.format(str(part_query)))
logger.warning('No information found for parts \'{}\' query `{}`'.format(order_refs(part.refs), str(part_query)))

else:

Expand All @@ -207,36 +208,39 @@ def get_part_info(query, parts, distributor_info=None):
# Get pricing information from this distributor.
try:
price_tiers = {} # Empty dict in case of exception.
dist_currency = list(offer['prices'].keys())

# Get the price tiers prioritizing:
# 1) The asked currency by KiCOst user;
# 2) The default currency given by `DEFAULT_CURRENCY` in root `global_vars.py`;
# 3) The first not null tier.s
prices = None
if currency in dist_currency and offer['prices'][currency]:
prices = offer['prices'][currency]
part.currency[dist] = currency
elif DEFAULT_CURRENCY in dist_currency and offer['prices'][DEFAULT_CURRENCY]:# and DEFAULT_CURRENCY!=currency:
prices = offer['prices'][DEFAULT_CURRENCY]
part.currency[dist] = DEFAULT_CURRENCY
if not offer['prices']:
logger.warning('No price information found for parts \'{}\' query `{}`'.format(order_refs(part.refs), str(part_query)))
else:
for dist_c in dist_currency:
if offer['prices'][dist_c]:
prices = offer['prices'][dist_c]
part.currency[dist] = dist_c
break

# Some times the API returns minimum purchase 0 and a not valid `price_tiers`.
if prices:
price_tiers = {qty: float(price)
for qty, price in list(prices)
}
# Combine price lists for multiple offers from the same distributor
# to build a complete list of cut-tape and reeled components.
if not dist in part.price_tiers:
part.price_tiers[dist] = {}
part.price_tiers[dist].update(price_tiers)
dist_currency = list(offer['prices'].keys())

# Get the price tiers prioritizing:
# 1) The asked currency by KiCOst user;
# 2) The default currency given by `DEFAULT_CURRENCY` in root `global_vars.py`;
# 3) The first not null tier.s
prices = None
if currency in dist_currency and offer['prices'][currency]:
prices = offer['prices'][currency]
part.currency[dist] = currency
elif DEFAULT_CURRENCY in dist_currency and offer['prices'][DEFAULT_CURRENCY]:# and DEFAULT_CURRENCY!=currency:
prices = offer['prices'][DEFAULT_CURRENCY]
part.currency[dist] = DEFAULT_CURRENCY
else:
for dist_c in dist_currency:
if offer['prices'][dist_c]:
prices = offer['prices'][dist_c]
part.currency[dist] = dist_c
break

# Some times the API returns minimum purchase 0 and a not valid `price_tiers`.
if prices:
price_tiers = {qty: float(price)
for qty, price in list(prices)
}
# Combine price lists for multiple offers from the same distributor
# to build a complete list of cut-tape and reeled components.
if not dist in part.price_tiers:
part.price_tiers[dist] = {}
part.price_tiers[dist].update(price_tiers)
except TypeError:
pass # Price list is probably missing so leave empty default dict in place.

Expand Down
6 changes: 3 additions & 3 deletions kicost/kicost.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def kicost(in_file, eda_name, out_filename,
modules. If `None`, no web/local distributors will be scraped.
@param collapse_refs `bool()` Collapse or not the designator references in the spreadsheet.
Default `True`.
@param collapse_refs `bool()` Suppress the distributors catalogue links into the catalogue code in the spreadsheet.
@param supress_cat_url `bool()` Suppress the distributors catalogue links into the catalogue code in the spreadsheet.
Default `True`.
@param currency `str()` Currency in ISO4217. Default 'USD'.
'''
Expand Down Expand Up @@ -224,8 +224,8 @@ def kicost(in_file, eda_name, out_filename,
distributors = distributor_dict.copy().keys()
for d in distributors:
if not d in dist_not_rmv:
logger.warning("No 'manf#' and '%s#' field in any part: distributor '%s' will be not scraped.",
d, distributor_dict[d]['label'])
logger.warning("No 'manf#' and '%s#' field in any part: no information by '%s'.",
d, distributor_dict[d]['label']['name'])
distributor_dict.pop(d, None)

if logger.isEnabledFor(DEBUG_DETAILED):
Expand Down
10 changes: 9 additions & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ BOMs=$(find *.xml)

while read -r eachBOM; do
echo "##### Testing file: $eachBOM"
kicost -wqi "$eachBOM"
kicost -wi "$eachBOM"
echo ""
done <<< "$BOMs"

BOMs=$(find *.csv)

while read -r eachBOM; do
echo "##### Testing file: $eachBOM"
kicost -wi "$eachBOM" --eda csv
echo ""
done <<< "$BOMs"

Expand Down

0 comments on commit d4466c3

Please sign in to comment.