Skip to content

Commit

Permalink
Merge pull request #4 from hildogjr/multifiles
Browse files Browse the repository at this point in the history
Multifiles
  • Loading branch information
hildogjr authored Jan 30, 2018
2 parents 19a9cc9 + 1f2b2c7 commit 30da492
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 143 deletions.
Binary file modified block_diag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 3 additions & 6 deletions kicost/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import logging
import time
#import inspect # To get the internal module and informations of a module/class.
from .kicost import kicost # kicost core functions.
from .kicost import * # kicost core functions.
from .kicost_gui import * # User guide.
#try:
# from .kicost_gui import * # User guide.
Expand Down Expand Up @@ -182,11 +182,8 @@ def main():
if args.input != None:
# Send output to spreadsheet with name of input file.
if len(args.input)>1:
# Compose a name with the multiple BOM input file names,
# limiting to the first 5 caracheters of each name (avoid
# huge names). This is dynamic if the number of input
# files passed.
args.output = '-'.join( [ os.path.splitext(args.input[i][:max(int(20/len(args.input)),5)])[0] for i in range(len(args.input))] ) + '.xlsx'
# Compose a name with the multiple BOM input file names.
args.output = output_filename_multipleinputs(args.input)
else:
args.output = os.path.splitext(args.input[0])[0] + '.xlsx'
else:
Expand Down
31 changes: 26 additions & 5 deletions kicost/distributors/digikey/digikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the Digikey product page.'''
'''@brief Get the pricing tiers from the parsed tree of the Digikey product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}
try:
for tr in html_tree.find('table', id='product-dollars').find_all('tr'):
Expand All @@ -65,7 +68,10 @@ def get_price_tiers(html_tree):


def part_is_reeled(html_tree):
'''Returns True if this Digi-Key part is reeled or Digi-reeled.'''
'''@brief Returns True if this Digi-Key part is reeled or Digi-reeled.
@param html_tree `str()` html of the distributor part page.
@return `True` or `False`.
'''
qty_tiers = list(get_price_tiers(html_tree).keys())
if len(qty_tiers) > 0 and min(qty_tiers) >= 100:
return True
Expand All @@ -76,7 +82,10 @@ def part_is_reeled(html_tree):


def get_part_num(html_tree):
'''Get the part number from the Digikey product page.'''
'''@brief Get the part number from the Digikey product page.
@param html_tree `str()` html of the distributor part page.
@return `list()`of the parts that match.
'''
try:
return re.sub('\s', '', html_tree.find('td',
id='reportPartNumber').text)
Expand All @@ -86,7 +95,10 @@ def get_part_num(html_tree):


def get_qty_avail(html_tree):
'''Get the available quantity of the part from the Digikey product page.'''
'''@brief Get the available quantity of the part from the Digikey product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''
try:
qty_tree = html_tree.find('td', id='quantityAvailable').find('span', id='dkQty')
qty_str = qty_tree.text
Expand All @@ -111,7 +123,16 @@ def get_qty_avail(html_tree):


def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=2, local_part_html=None, scrape_retries=2):
'''Find the Digikey HTML page for a part number and return the URL and parse tree.'''
'''@brief Find the Digikey HTML page for a part number and return the URL and parse tree.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, url)
'''

def merge_price_tiers(main_tree, alt_tree):
'''Merge the price tiers from the alternate-packaging tree into the main tree.'''
Expand Down
26 changes: 22 additions & 4 deletions kicost/distributors/farnell/farnell.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the farnell product page.'''
'''@brief Get the pricing tiers from the parsed tree of the farnell product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}
try:
qty_strs = []
Expand Down Expand Up @@ -61,7 +64,10 @@ def get_price_tiers(html_tree):
return price_tiers

def get_part_num(html_tree):
'''Get the part number from the farnell product page.'''
'''@brief Get the part number from the farnell product page.
@param html_tree `str()` html of the distributor part page.
@return `list()`of the parts that match.
'''
try:
# farnell catalog number is stored in a description list, so get
# all the list terms and descriptions, strip all the spaces from those,
Expand All @@ -78,7 +84,10 @@ def get_part_num(html_tree):
return '' # No ProductDescription found in page.

def get_qty_avail(html_tree):
'''Get the available quantity of the part from the farnell product page.'''
'''@brief Get the available quantity of the part from the farnell product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''
try:
qty_str = html_tree.find('p', class_='availabilityHeading').text
except (AttributeError, ValueError):
Expand All @@ -94,7 +103,16 @@ def get_qty_avail(html_tree):
return None

def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=2, local_part_html=None, scrape_retries=2):
'''Find the farnell HTML page for a part number and return the URL and parse tree.'''
'''@brief Find the farnell HTML page for a part number and return the URL and parse tree.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, url)
'''

# Use the part number to lookup the part using the site search function, unless a starting url was given.
if url is None:
Expand Down
26 changes: 22 additions & 4 deletions kicost/distributors/local/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the local product page.'''
'''@brief Get the pricing tiers from the parsed tree of the local product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}
try:
pricing = html_tree.find('div', class_='pricing').text
Expand All @@ -63,7 +66,10 @@ def get_price_tiers(html_tree):


def get_part_num(html_tree):
'''Get the part number from the local product page.'''
'''@brief Get the part number from the local product page.
@param html_tree `str()` html of the distributor part page.
@return `list()`of the parts that match.
'''
try:
part_num_str = html_tree.find('div', class_='cat#').text
return part_num_str
Expand All @@ -72,7 +78,10 @@ def get_part_num(html_tree):


def get_qty_avail(html_tree):
'''Get the available quantity of the part from the local product page.'''
'''@brief Get the available quantity of the part from the local product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''
try:
qty_str = html_tree.find('div', class_='quantity').text
except (AttributeError, ValueError):
Expand All @@ -89,7 +98,16 @@ def get_qty_avail(html_tree):


def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=None, local_part_html=None, scrape_retries=2):
'''Extract the HTML tree from the HTML page for local parts.'''
'''Extract the HTML tree from the HTML page for local parts.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, `None`) The second argument is always `None` bacause there is not url to return.
'''

# Extract the HTML tree from the local part HTML page.
try:
Expand Down
26 changes: 22 additions & 4 deletions kicost/distributors/mouser/mouser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the Mouser product page.'''
'''@brief Get the pricing tiers from the parsed tree of the Mouser product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}
try:
pricing_tbl_tree = html_tree.find('div', class_='pdp-pricing-table')
Expand Down Expand Up @@ -86,7 +89,10 @@ def get_price_tiers(html_tree):


def get_part_num(html_tree):
'''Get the part number from the Mouser product page.'''
'''@brief Get the part number from the Mouser product page.
@param html_tree `str()` html of the distributor part page.
@return `list()`of the parts that match.
'''
try:
partnum = html_tree.find(
'span', id='spnMouserPartNumFormattedForProdInfo'
Expand All @@ -98,7 +104,10 @@ def get_part_num(html_tree):


def get_qty_avail(html_tree):
'''Get the available quantity of the part from the Mouser product page.'''
'''@brief Get the available quantity of the part from the Mouser product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''
try:
qty_str = html_tree.find(
'div', class_='pdp-product-availability').find(
Expand All @@ -120,7 +129,16 @@ def get_qty_avail(html_tree):


def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=2, local_part_html=None, scrape_retries=2):
'''Find the Mouser HTML page for a part number and return the URL and parse tree.'''
'''@brief Find the Mouser HTML page for a part number and return the URL and parse tree.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, url)
'''

# Use the part number to lookup the part using the site search function, unless a starting url was given.
if url is None:
Expand Down
26 changes: 22 additions & 4 deletions kicost/distributors/newark/newark.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the Newark product page.'''
'''@brief Get the pricing tiers from the parsed tree of the Newark product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}
try:
qty_strs = []
Expand Down Expand Up @@ -79,7 +82,10 @@ def get_price_tiers(html_tree):


def get_part_num(html_tree):
'''Get the part number from the Newark product page.'''
'''@brief Get the part number from the Newark product page.
@param html_tree `str()` html of the distributor part page.
@return `list()`of the parts that match.
'''
try:
# Newark catalog number is stored in a description list, so get
# all the list terms and descriptions, strip all the spaces from those,
Expand All @@ -98,7 +104,10 @@ def get_part_num(html_tree):


def get_qty_avail(html_tree):
'''Get the available quantity of the part from the Newark product page.'''
'''@brief Get the available quantity of the part from the Newark product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''
try:
qty_str = html_tree.find('p', class_='availabilityHeading').text
except (AttributeError, ValueError):
Expand All @@ -116,7 +125,16 @@ def get_qty_avail(html_tree):


def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=2, local_part_html=None, scrape_retries=2):
'''Find the Newark HTML page for a part number and return the URL and parse tree.'''
'''@brief Find the Newark HTML page for a part number and return the URL and parse tree.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, url)
'''

# Use the part number to lookup the part using the site search function, unless a starting url was given.
if url is None:
Expand Down
26 changes: 22 additions & 4 deletions kicost/distributors/rs/rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@


def get_price_tiers(html_tree):
'''Get the pricing tiers from the parsed tree of the RS Components product page.'''
'''@brief Get the pricing tiers from the parsed tree of the RS Components product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
price_tiers = {}

try:
Expand All @@ -53,7 +56,10 @@ def get_price_tiers(html_tree):
return price_tiers

def get_part_num(html_tree):
'''Get the part number from the farnell product page.'''
'''@brief Get the part number from the RS product page.
@param html_tree `str()` html of the distributor part page.
@return `dict()` price breaks, the keys are the quantities breaks.
'''
try:
pn_str = html_tree.find('span', class_='keyValue bold', itemprop='sku').text
pn = re.sub('[^0-9\-]','', pn_str)
Expand All @@ -64,7 +70,10 @@ def get_part_num(html_tree):
return '' # No ProductDescription found in page.

def get_qty_avail(html_tree):
'''Get the available quantity of the part from the farnell product page.'''
'''Get the available quantity of the part from the RS product page.
@param html_tree `str()` html of the distributor part page.
@return `int` avaliable quantity.
'''

try:
# Note that 'availability' is misspelled in the container class name!
Expand All @@ -82,7 +91,16 @@ def get_qty_avail(html_tree):
return None

def get_part_html_tree(dist, pn, extra_search_terms='', url=None, descend=2, local_part_html=None, scrape_retries=2):
'''Find the RS Components HTML page for a part number and return the URL and parse tree.'''
'''@brief Find the RS Components HTML page for a part number and return the URL and parse tree.
@param dist
@param pn Part number `str()`.
@param extra_search_terms
@param url
@param descend
@param local_part_html
@param scrape_retries `int` Quantity of retries in case of fail.
@return (html `str()` of the page, url)
'''

# Use the part number to lookup the part using the site search function, unless a starting url was given.
if url is None:
Expand Down
Loading

0 comments on commit 30da492

Please sign in to comment.