Skip to content

Commit

Permalink
Re-established Python 2 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Vandenbout committed Jun 14, 2019
1 parent 7bfbf6b commit 3e5ae1b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions kicost/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Júnior
Expand All @@ -20,6 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function

#Libraries.
import argparse as ap # Command argument parser.
import os, sys, platform
Expand Down
11 changes: 9 additions & 2 deletions kicost/distributors/api_partinfo_kitspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import logging, tqdm
import copy, re
from collections import Counter
from urllib.parse import quote_plus as urlquote
#from urllib.parse import quote_plus as urlquote

# KiCost definitions.
from .global_vars import * # Debug information, `distributor_dict` and `SEPRTR`.
Expand Down Expand Up @@ -151,11 +151,16 @@ def init_dist_dict():
})


@staticmethod
def query(query_parts, query_type=QUERY_MATCH):
'''Send query to server and return results.'''
#r = requests.post(QUERY_URL, {"query": QUERY_SEARCH, "variables": variables}) #TODO future use for ISSUE #17
variables = re.sub('\'', '\"', str(query_parts))
variables = re.sub('\s', '', variables)
# Python 2 prepends a 'u' before the query strings and this makes PartInfo
# complain, so remove them.
variables = re.sub(':u"', ':"', variables)
variables = re.sub('{u"', '{"', variables)
variables = '{{"input":{}}}'.format(variables)
response = requests.post(QUERY_URL, {'query': query_type, "variables": variables})
if response.status_code == requests.codes['ok']: #200
Expand All @@ -164,10 +169,11 @@ def query(query_parts, query_type=QUERY_MATCH):
elif response.status_code == requests.codes['not_found']: #404
raise Exception('Kitspace server not found.')
elif response.status_code == requests.codes['bad_request']: #400
raise Exception('Bad request to Kitspace server probably due to incorrect string format.')
raise Exception('Bad request to Kitspace server probably due to an incorrect string format.')
else:
raise Exception('Kitspace error: ' + str(response.status_code))

@staticmethod
def get_value(data, item, default=None):
'''Get the value of `value` field of a dictionary if the `name`field identifier.
Used to get information from the JSON response.'''
Expand All @@ -182,6 +188,7 @@ def get_value(data, item, default=None):
except:
return default

@staticmethod
def query_part_info(parts, distributors, currency=DEFAULT_CURRENCY):
'''Fill-in the parts with price/qty/etc info from KitSpace.'''
logger.log(DEBUG_OVERVIEW, '# Getting part data from KitSpace...')
Expand Down
1 change: 1 addition & 0 deletions kicost/distributors/dist_local_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def init_dist_dict():
}
})

@staticmethod
def query_part_info(parts, distributors, currency='USD'):
"""Fill-in part information for locally-sourced parts not handled by Octopart."""

Expand Down
2 changes: 2 additions & 0 deletions kicost/distributors/distributor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Max Maisel / Hildo Guillardi Júnior
Expand Down
2 changes: 2 additions & 0 deletions kicost/global_vars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Júnior
Expand Down
2 changes: 2 additions & 0 deletions kicost/kicost.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-

# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Júnior
Expand All @@ -21,6 +22,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function

# Libraries.
import sys, os
Expand Down
17 changes: 13 additions & 4 deletions kicost/kicost_gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-

# MIT license
#
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Junior
# Copyright (C) 2018 by XESS Corporation / Hildo Guillardi Júnior
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,6 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import print_function

# Author information.
__author__ = 'Hildo Guillardi Júnior'
__webpage__ = 'https://github.com/hildogjr/'
Expand All @@ -42,6 +45,7 @@
from datetime import datetime # To create the log name, when asked to save.
from distutils.version import StrictVersion # To comparative of versions.
import re # Regular expression parser.
import locale # For country location.
import babel # For language.
from babel import numbers # For currency presentation.
import logging
Expand Down Expand Up @@ -731,7 +735,10 @@ def str_to_arg(commands):
args.group_fields = str_to_arg(['--group_fields', '-grp']).split()
args.translate_fields = str_to_arg(['--translate_fields']).split()
args.variant = str_to_arg(['--variant', '-var'])
args.currency = re.findall('\((\w{3}) .*\).*', self.m_comboBox_currency.GetValue())[0]
try:
args.currency = re.findall('\((\w{3}) .*\).*', self.m_comboBox_currency.GetValue())[0]
except IndexError:
pass # Doesn't work under Python 2 so I'm just ignoring it.

args.collapse_refs = self.m_checkBox_collapseRefs.GetValue() # Collapse refs in the spreadsheet.

Expand Down Expand Up @@ -818,12 +825,13 @@ def set_properties(self):
#self.m_listBox_edatool.Append(eda_names)

# Get all the currencies present.
loc = locale.getdefaultlocale()[0]
currencyList = sorted(list(numbers.list_currencies()))
for c in range(len(currencyList)):
currency = currencyList[c]
currencyList[c] = '({a} {s}) {n}'.format(a=currency,
s=numbers.get_currency_symbol(currency),
n=numbers.get_currency_name(currency)
s=numbers.get_currency_symbol(currency, locale=loc),
n=numbers.get_currency_name(currency, locale=loc)
)
self.m_comboBox_currency.Insert(currencyList, 0)

Expand Down Expand Up @@ -861,6 +869,7 @@ def set_properties(self):
GUI by Hildo Guillardi Júnior
'''
credits = re.sub(r'\n[\t ]+', '\n', credits) # Remove leading whitespace

self.m_text_credits.SetValue(credits)

# Recovery the last configurations used (found the folder of the file by the OS).
Expand Down
2 changes: 1 addition & 1 deletion tests/RX LR lite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<value>Conn_02x02_Odd_Even</value>
<footprint>Socket_Strips:Socket_Strip_Straight_2x02_Pitch1.27mm_SMD</footprint>
<fields>
<field name="MPN">FLE102–01–G–DV</field>
<field name="MPN">FLE-102-01-G-DV</field>
</fields>
<libsource lib="RX LR lite-cache" part="Conn_02x02_Odd_Even"/>
<sheetpath names="/" tstamps="/"/>
Expand Down

0 comments on commit 3e5ae1b

Please sign in to comment.