Skip to content

Commit

Permalink
Dynamically initialize distributor_dict & fix #186
Browse files Browse the repository at this point in the history
Dynamically initialize distributor_dict.
  • Loading branch information
hildogjr authored Jun 15, 2018
2 parents c5c27ba + 7d5208f commit 56e1504
Show file tree
Hide file tree
Showing 25 changed files with 320 additions and 270 deletions.
26 changes: 13 additions & 13 deletions kicost/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
except ImportError:
pass # If the wxPython dependences are not installed and
# the user just want the KiCost CLI.
from .distributors import distributor_dict
from .distributors.global_vars import distributor_dict
from .eda_tools import eda_tool_dict
from . import __version__ # Version control by @xesscorp.

Expand All @@ -48,6 +48,18 @@

from .globals import *

###############################################################################
# Additional functions
###############################################################################

def kicost_gui_notdependences():
print('You don\'t have the wxPython dependence to run the GUI interface. Run once of the follow commands in terminal to install them:')
print('pip3 install -U wxPython # For Windows & macOS')

print('pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython # For Linux 16.04')
print('Or download from last version from <https://wxpython.org/pages/downloads/>')
sys.exit(1)

###############################################################################
# Command-line interface.
###############################################################################
Expand Down Expand Up @@ -284,15 +296,3 @@ def main():
logger = logging.getLogger('kicost')
logger.log(logging.DEBUG-2, 'Elapsed time: %f seconds', time.time() - start_time)


###############################################################################
# Additional functions
###############################################################################

def kicost_gui_notdependences():
print('You don\'t have the wxPython dependence to run the GUI interface. Run once of the follow commands in terminal to install them:')
print('pip3 install -U wxPython # For Windows & macOS')

print('pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython # For Linux 16.04')
print('Or download from last version from <https://wxpython.org/pages/downloads/>')
sys.exit(1)
58 changes: 33 additions & 25 deletions kicost/distributors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@
__author__ = 'XESS Corporation'
__email__ = '[email protected]'

# Extra informations to by got by each part in the distributors.
EXTRA_INFO_DIST = ['value', 'tolerance', 'footprint', 'power', 'current', 'voltage', 'frequency', 'temp_coeff', 'manf',
'size', 'op temp', 'orientation', 'color',
'datasheet', 'image', # Links.
]
extra_info_dist_name_translations = {
#TODO it will need to put here language translation after implementation of ISSUE #65?
'resistance': 'value',
'inductance': 'value',
'capacitance': 'value',
'manufacturer': 'manf',
'package': 'footprint',
'package / case': 'footprint',
'datasheets': 'datasheet',
'dimension': 'size',
'size / dimension': 'size',
'operating temperature': 'op temp',
'voltage - rated': 'voltage',
'Mating Orientation': 'orientation',
'coulor': 'color',
'wire gauge': 'wire',
}

# The global dictionary of distributor information starts out empty.
distributor_dict = {}
import os

# The distributor module directories will be found in this directory.
directory = os.path.dirname(__file__)

# Search for the distributor modules and import them.
for module in os.listdir(directory):

# Avoid importing non-directories.
abs_module = os.path.join(directory, module)
if not os.path.isdir(abs_module):
continue

# Avoid directories like __pycache__.
if module.startswith('__'):
continue

# Import the module.
tmp = __import__(module, globals(), locals(), [], level=1)
tmp_mod = getattr(tmp, module);
globals()["dist_"+module] = getattr(tmp_mod, "dist_"+module)

from .global_vars import distributor_dict

def init_distributor_dict():
# Clear distributor_dict, then let all distributor modules recreate their entries.
distributor_dict = {}
for x in globals():
if x.startswith("dist_"):
globals()[x].dist_init_distributor_dict()

# Init distirbutor dict during import.
init_distributor_dict()
28 changes: 0 additions & 28 deletions kicost/distributors/digikey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,3 @@

from .digikey import *

# Place information about this distributor into the distributor dictionary.
from .. import distributor_dict
distributor_dict.update(
{
'digikey': {
'module': 'digikey', # The directory name containing this file.
'scrape': 'web', # Allowable values: 'web' or 'local'.
'label': 'Digi-Key', # Distributor label used in spreadsheet columns.
'order_cols': ['purch', 'part_num', 'refs'], # Sort-order for online orders.
'order_delimiter': ',', # Delimiter for online orders.
# Formatting for distributor header in worksheet.
'wrk_hdr_format': {
'font_size': 14,
'font_color': 'white',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#CC0000' # Digi-Key red.
},
# Web site defitions.
'site': {
'url': 'https://www.digikey.com',
'currency': 'USD',
'locale': 'US'
},
}
}
)
34 changes: 31 additions & 3 deletions kicost/distributors/digikey/digikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import re, difflib
from bs4 import BeautifulSoup
import http.client # For web scraping exceptions.
from .. import fake_browser
from .. import EXTRA_INFO_DIST, extra_info_dist_name_translations
from ...globals import PartHtmlError
from ...globals import logger, DEBUG_OVERVIEW, DEBUG_DETAILED, DEBUG_OBSESSIVE, DEBUG_HTTP_RESPONSES

from .. import distributor, distributor_dict
from .. import fake_browser
from .. import distributor
from ..global_vars import distributor_dict, EXTRA_INFO_DIST, extra_info_dist_name_translations

from urllib.parse import quote_plus as urlquote

Expand All @@ -46,6 +46,34 @@ def __init__(self, name, scrape_retries, throttle_delay):
super(dist_digikey, self).__init__(name, distributor_dict[name]['site']['url'],
scrape_retries, throttle_delay)

@staticmethod
def dist_init_distributor_dict():
distributor_dict.update(
{
'digikey': {
'module': 'digikey', # The directory name containing this file.
'scrape': 'web', # Allowable values: 'web' or 'local'.
'label': 'Digi-Key', # Distributor label used in spreadsheet columns.
'order_cols': ['purch', 'part_num', 'refs'], # Sort-order for online orders.
'order_delimiter': ',', # Delimiter for online orders.
# Formatting for distributor header in worksheet.
'wrk_hdr_format': {
'font_size': 14,
'font_color': 'white',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#CC0000' # Digi-Key red.
},
# Web site defitions.
'site': {
'url': 'https://www.digikey.com',
'currency': 'USD',
'locale': 'US'
},
}
})

def dist_get_price_tiers(self, html_tree):
'''@brief Get the pricing tiers from the parsed tree of the Digikey product page.
@param html_tree `str()` html of the distributor part page.
Expand Down
9 changes: 7 additions & 2 deletions kicost/distributors/distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
import logging
import time
from random import choice
from ..eda_tools.eda_tools import order_refs # To better print the warnings about the parts.

from .global_vars import distributor_dict
from . import fake_browser

from ..eda_tools.eda_tools import order_refs # To better print the warnings about the parts.

import http.client # For web scraping exceptions.

from ..globals import logger, DEBUG_OVERVIEW, DEBUG_DETAILED, DEBUG_OBSESSIVE # Debug configurations.
from ..globals import SEPRTR
from ..globals import PartHtmlError
from . import distributor_dict

import os, re

Expand All @@ -58,6 +59,10 @@ def __init__(self, name, domain, scrape_retries, throttle_delay):
(self.domain, self.logger, self.scrape_retries, throttle_delay)

# Abstract methods, implemented in distributor specific modules
@staticmethod
def dist_init_distributor_dict():
raise NotImplementedError()

def dist_get_part_html_tree(self, pn, extra_search_terms, url, descend):
raise NotImplementedError()

Expand Down
28 changes: 0 additions & 28 deletions kicost/distributors/farnell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,3 @@

from .farnell import *

# Place information about this distributor into the distributor dictionary.
from .. import distributor_dict
distributor_dict.update(
{
'farnell': {
'module': 'farnell', # The directory name containing this file.
'scrape': 'web', # Allowable values: 'web' or 'local'.
'label': 'Farnell', # Distributor label used in spreadsheet columns.
'order_cols': ['part_num', 'purch', 'refs'], # Sort-order for online orders.
'order_delimiter': ' ', # Delimiter for online orders.
# Formatting for distributor header in worksheet.
'wrk_hdr_format': {
'font_size': 14,
'font_color': 'white',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#FF6600' # Farnell/E14 orange.
},
# Web site defitions.
'site': {
'url': 'https://it.farnell.com/',
'currency': 'USD',
'locale': 'US'
},
}
}
)
33 changes: 31 additions & 2 deletions kicost/distributors/farnell/farnell.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import re, difflib
from bs4 import BeautifulSoup
import http.client # For web scraping exceptions.
from .. import fake_browser
from ...globals import PartHtmlError
from ...globals import currency
from ...globals import logger, DEBUG_OVERVIEW, DEBUG_DETAILED, DEBUG_OBSESSIVE, DEBUG_HTTP_RESPONSES

from .. import distributor, distributor_dict
from .. import fake_browser
from .. import distributor
from ..global_vars import distributor_dict

from urllib.parse import quote_plus as urlquote

Expand All @@ -46,6 +47,34 @@ def __init__(self, name, scrape_retries, throttle_delay):
super(dist_farnell, self).__init__(name, distributor_dict[name]['site']['url'],
scrape_retries, throttle_delay)

@staticmethod
def dist_init_distributor_dict():
distributor_dict.update(
{
'farnell': {
'module': 'farnell', # The directory name containing this file.
'scrape': 'web', # Allowable values: 'web' or 'local'.
'label': 'Farnell', # Distributor label used in spreadsheet columns.
'order_cols': ['part_num', 'purch', 'refs'], # Sort-order for online orders.
'order_delimiter': ' ', # Delimiter for online orders.
# Formatting for distributor header in worksheet.
'wrk_hdr_format': {
'font_size': 14,
'font_color': 'white',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#FF6600' # Farnell/E14 orange.
},
# Web site defitions.
'site': {
'url': 'https://it.farnell.com/',
'currency': 'USD',
'locale': 'US'
},
}
})

def dist_get_price_tiers(self, html_tree):
'''@brief Get the pricing tiers from the parsed tree of the farnell product page.
@param html_tree `str()` html of the distributor part page.
Expand Down
48 changes: 48 additions & 0 deletions kicost/distributors/global_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Junior
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# The global dictionary of distributor information starts out empty.
distributor_dict = {}

# Extra informations to by got by each part in the distributors.
EXTRA_INFO_DIST = ['value', 'tolerance', 'footprint', 'power', 'current', 'voltage', 'frequency', 'temp_coeff', 'manf',
'size', 'op temp', 'orientation', 'color',
'datasheet', 'image', # Links.
]
extra_info_dist_name_translations = {
#TODO it will need to put here language translation after implementation of ISSUE #65?
'resistance': 'value',
'inductance': 'value',
'capacitance': 'value',
'manufacturer': 'manf',
'package': 'footprint',
'package / case': 'footprint',
'datasheets': 'datasheet',
'dimension': 'size',
'size / dimension': 'size',
'operating temperature': 'op temp',
'voltage - rated': 'voltage',
'Mating Orientation': 'orientation',
'coulor': 'color',
'wire gauge': 'wire',
}
22 changes: 0 additions & 22 deletions kicost/distributors/local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,3 @@

from .local import *

# Place information about this distributor into the distributor dictionary.
from .. import distributor_dict
distributor_dict.update(
{
'local_template': {
'module': 'local', # The directory name containing this file.
'scrape': 'local', # Allowable values: 'web' or 'local'.
'label': 'Local', # Distributor label used in spreadsheet columns.
'order_cols': ['part_num', 'purch', 'refs'], # Sort-order for online orders.
'order_delimiter': ' ', # Delimiter for online orders.
# Formatting for distributor header in worksheet.
'wrk_hdr_format': {
'font_size': 14,
'font_color': 'white',
'bold': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#008000' # Darker green.
},
}
}
)
Loading

0 comments on commit 56e1504

Please sign in to comment.