diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a56e7..1858e96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ ### [2.4][v2.4] ### -Released XXXX-XX-XX. +Released 2015-11-28. -- Poop +- New money icon +- Update pint, docopt and Alfred-Workflow libraries +- Reorganise code and repo ### [2.3][v2.3] ### diff --git a/Convert-2.3.alfredworkflow b/Convert-2.3.alfredworkflow deleted file mode 100644 index 81bcd62..0000000 Binary files a/Convert-2.3.alfredworkflow and /dev/null differ diff --git a/Convert-2.4.alfredworkflow b/Convert-2.4.alfredworkflow new file mode 100644 index 0000000..0b4b52a Binary files /dev/null and b/Convert-2.4.alfredworkflow differ diff --git a/README.md b/README.md index d314d84..a89ea3f 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ See [CHANGELOG][changelog] for more information. | Release | Date | |-------------|----------------| -| [2.4][v2.4] | XXXX-XX-XX | +| [2.4][v2.4] | 2015-11-28 | | [2.3][v2.3] | 2015-11-26 | | [2.2][v2.2] | 2015-07-16 | | 2.1 | Never released | diff --git a/currencies/currencies_yahoo.py b/currencies/currencies_yahoo.py index 0b1047a..e0b2677 100755 --- a/currencies/currencies_yahoo.py +++ b/currencies/currencies_yahoo.py @@ -42,7 +42,7 @@ # Max = 50 symbols_per_request = 50 -parse_yahoo_response = re.compile(r'{}(.+)=X'.format(reference_currency)).match +parse_yahoo_response = re.compile(r'{0}(.+)=X'.format(reference_currency)).match def grouper(n, iterable, fillvalue=None): @@ -78,12 +78,12 @@ def load_yahoo_rates(symbols): if symbol == reference_currency: count -= 1 continue - parts.append('{}{}=X'.format(reference_currency, symbol)) + parts.append('{0}{1}=X'.format(reference_currency, symbol)) query = ','.join(parts) url = yahoo_base_url.format(query) # Fetch data - print('Fetching {} ...'.format(url), file=sys.stderr) + print('Fetching {0} ...'.format(url), file=sys.stderr) r = requests.get(url) r.raise_for_status() @@ -96,7 +96,7 @@ def load_yahoo_rates(symbols): name, rate = row m = parse_yahoo_response(name) if not m: - print('Invalid currency : {}'.format(name), file=sys.stderr) + print('Invalid currency : {0}'.format(name), file=sys.stderr) continue symbol = m.group(1) try: @@ -108,7 +108,7 @@ def load_yahoo_rates(symbols): ycount += 1 # print(row) - assert ycount == count, 'Yahoo! returned {} results, not {}'.format( + assert ycount == count, 'Yahoo! returned {0} results, not {1}'.format( ycount, count) return rates @@ -158,7 +158,7 @@ def main(): all_currencies = load_currencies(*currency_source_files) to_check = all_currencies - print('{} currencies to check ...'.format(len(to_check)), file=sys.stderr) + print('{0} currencies to check ...'.format(len(to_check)), file=sys.stderr) rates = get_exchange_rates(to_check) for symbol in sorted(rates): @@ -166,12 +166,12 @@ def main(): if rate == 0: unknown_currencies.append(symbol) else: - print('{}\t{}'.format(symbol, rate)) + print('{0}\t{1}'.format(symbol, rate)) print('\n\nUnsupported currencies:') print('-----------------------') for symbol in unknown_currencies: - print('{}\t{}'.format(symbol, all_currencies[symbol])) + print('{0}\t{1}'.format(symbol, all_currencies[symbol])) supported_currencies = {k: v for k, v in all_currencies.items() if k not in unknown_currencies} diff --git a/src/config.py b/src/config.py index a1765fc..bf924cd 100644 --- a/src/config.py +++ b/src/config.py @@ -12,7 +12,7 @@ from __future__ import print_function, unicode_literals -import json +import csv import os # ---------------------------------------------------------------------- @@ -38,13 +38,17 @@ # ---------------------------------------------------------------------- # Unit definition files # ---------------------------------------------------------------------- +CURRENCIES = {} CUSTOM_DEFINITIONS_FILENAME = 'unit_definitions.txt' BUILTIN_UNIT_DEFINITIONS = os.path.join(os.path.dirname(__file__), CUSTOM_DEFINITIONS_FILENAME) with open(os.path.join(os.path.dirname(__file__), - 'currencies.json'), 'rb') as fp: - CURRENCIES = json.load(fp) + 'currencies.tsv'), 'rb') as fp: + reader = csv.reader(fp, delimiter=b'\t') + for sym, rate in reader: + sym = unicode(sym, 'utf-8') + CURRENCIES[sym] = rate # ---------------------------------------------------------------------- # Help/support URLs diff --git a/src/convert.py b/src/convert.py index ebe3dd2..f65a426 100755 --- a/src/convert.py +++ b/src/convert.py @@ -50,7 +50,7 @@ def register_units(): ureg.load_definitions(user_definitions) else: # Copy template to data dir shutil.copy( - wf.workflowfile('{}.sample'.format(CUSTOM_DEFINITIONS_FILENAME)), + wf.workflowfile('{0}.sample'.format(CUSTOM_DEFINITIONS_FILENAME)), wf.datafile(CUSTOM_DEFINITIONS_FILENAME)) @@ -221,11 +221,11 @@ def main(wf): 2)) except UndefinedUnitError as err: log.critical('Unknown unit : %s', err.unit_names) - error = 'Unknown unit : {}'.format(err.unit_names) + error = 'Unknown unit : {0}'.format(err.unit_names) except DimensionalityError as err: log.critical('Invalid conversion : %s', err) - error = "Can't convert from {} {} to {} {}".format( + error = "Can't convert from {0} {1} to {2} {3}".format( err.units1, err.dim1, err.units2, err.dim2) except ValueError as err: diff --git a/src/currencies.json b/src/currencies.json deleted file mode 100644 index b10f816..0000000 --- a/src/currencies.json +++ /dev/null @@ -1,159 +0,0 @@ -{ - "AED": "United Arab Emirates Dirham", - "AFN": "Afghanistan Afghani", - "ALL": "Albania Lek", - "AMD": "Armenia Dram", - "ANG": "Netherlands Antilles Guilder", - "AOA": "Angola Kwanza", - "ARS": "Argentina Peso", - "AUD": "Australia Dollar", - "AWG": "Aruba Guilder", - "AZN": "Azerbaijan New Manat", - "BAM": "Bosnia and Herzegovina Convertible Marka", - "BBD": "Barbados Dollar", - "BDT": "Bangladesh Taka", - "BGN": "Bulgaria Lev", - "BHD": "Bahrain Dinar", - "BIF": "Burundi Franc", - "BMD": "Bermuda Dollar", - "BND": "Brunei Darussalam Dollar", - "BOB": "Bolivia Boliviano", - "BRL": "Brazil Real", - "BSD": "Bahamas Dollar", - "BTC": "Bitcoin", - "BTN": "Bhutan Ngultrum", - "BWP": "Botswana Pula", - "BYR": "Belarus Ruble", - "BZD": "Belize Dollar", - "CAD": "Canada Dollar", - "CDF": "Congo/Kinshasa Franc", - "CHF": "Switzerland Franc", - "CLP": "Chile Peso", - "CNY": "China Yuan Renminbi", - "COP": "Colombia Peso", - "CRC": "Costa Rica Colon", - "CUP": "Cuba Peso", - "CVE": "Cape Verde Escudo", - "CZK": "Czech Republic Koruna", - "DJF": "Djibouti Franc", - "DKK": "Denmark Krone", - "DOP": "Dominican Republic Peso", - "DZD": "Algeria Dinar", - "EGP": "Egypt Pound", - "ERN": "Eritrea Nakfa", - "ETB": "Ethiopia Birr", - "EUR": "Euro Member Countries", - "FJD": "Fiji Dollar", - "FKP": "Falkland Islands (Malvinas) Pound", - "GBP": "United Kingdom Pound", - "GEL": "Georgia Lari", - "GHS": "Ghana Cedi", - "GIP": "Gibraltar Pound", - "GMD": "Gambia Dalasi", - "GNF": "Guinea Franc", - "GTQ": "Guatemala Quetzal", - "GYD": "Guyana Dollar", - "HKD": "Hong Kong Dollar", - "HNL": "Honduras Lempira", - "HRK": "Croatia Kuna", - "HTG": "Haiti Gourde", - "HUF": "Hungary Forint", - "IDR": "Indonesia Rupiah", - "ILS": "Israel Shekel", - "INR": "India Rupee", - "IQD": "Iraq Dinar", - "IRR": "Iran Rial", - "ISK": "Iceland Krona", - "JMD": "Jamaica Dollar", - "JOD": "Jordan Dinar", - "JPY": "Japan Yen", - "KES": "Kenya Shilling", - "KGS": "Kyrgyzstan Som", - "KHR": "Cambodia Riel", - "KMF": "Comoros Franc", - "KPW": "Korea (North) Won", - "KRW": "Korea (South) Won", - "KWD": "Kuwait Dinar", - "KYD": "Cayman Islands Dollar", - "KZT": "Kazakhstan Tenge", - "LAK": "Laos Kip", - "LBP": "Lebanon Pound", - "LKR": "Sri Lanka Rupee", - "LRD": "Liberia Dollar", - "LSL": "Lesotho Loti", - "LTL": "Lithuania Litas", - "LYD": "Libya Dinar", - "MAD": "Morocco Dirham", - "MDL": "Moldova Leu", - "MGA": "Madagascar Ariary", - "MKD": "Macedonia Denar", - "MMK": "Myanmar (Burma) Kyat", - "MNT": "Mongolia Tughrik", - "MOP": "Macau Pataca", - "MRO": "Mauritania Ouguiya", - "MUR": "Mauritius Rupee", - "MVR": "Maldives (Maldive Islands) Rufiyaa", - "MWK": "Malawi Kwacha", - "MXN": "Mexico Peso", - "MYR": "Malaysia Ringgit", - "MZN": "Mozambique Metical", - "NAD": "Namibia Dollar", - "NGN": "Nigeria Naira", - "NIO": "Nicaragua Cordoba", - "NOK": "Norway Krone", - "NPR": "Nepal Rupee", - "NZD": "New Zealand Dollar", - "OMR": "Oman Rial", - "PAB": "Panama Balboa", - "PEN": "Peru Nuevo Sol", - "PGK": "Papua New Guinea Kina", - "PHP": "Philippines Peso", - "PKR": "Pakistan Rupee", - "PLN": "Poland Zloty", - "PYG": "Paraguay Guarani", - "QAR": "Qatar Riyal", - "RON": "Romania New Leu", - "RSD": "Serbia Dinar", - "RUB": "Russia Ruble", - "RWF": "Rwanda Franc", - "SAR": "Saudi Arabia Riyal", - "SBD": "Solomon Islands Dollar", - "SCR": "Seychelles Rupee", - "SDG": "Sudan Pound", - "SEK": "Sweden Krona", - "SGD": "Singapore Dollar", - "SHP": "Saint Helena Pound", - "SLL": "Sierra Leone Leone", - "SOS": "Somalia Shilling", - "SRD": "Suriname Dollar", - "STD": "S\u00e3o Tom\u00e9 and Pr\u00edncipe Dobra", - "SVC": "El Salvador Colon", - "SYP": "Syria Pound", - "SZL": "Swaziland Lilangeni", - "THB": "Thailand Baht", - "TJS": "Tajikistan Somoni", - "TMT": "Turkmenistan Manat", - "TND": "Tunisia Dinar", - "TOP": "Tonga Pa'anga", - "TRY": "Turkey Lira", - "TTD": "Trinidad and Tobago Dollar", - "TWD": "Taiwan New Dollar", - "TZS": "Tanzania Shilling", - "UAH": "Ukraine Hryvnia", - "UGX": "Uganda Shilling", - "USD": "United States Dollar", - "UYU": "Uruguay Peso", - "UZS": "Uzbekistan Som", - "VEF": "Venezuela Bolivar", - "VND": "Viet Nam Dong", - "VUV": "Vanuatu Vatu", - "WST": "Samoa Tala", - "XAF": "Communaut\u00e9 Financi\u00e8re Africaine (BEAC) CFA Franc BEAC", - "XCD": "East Caribbean Dollar", - "XDR": "International Monetary Fund (IMF) Special Drawing Rights", - "XOF": "Communaut\u00e9 Financi\u00e8re Africaine (BCEAO) Franc", - "XPF": "Comptoirs Fran\u00e7ais du Pacifique (CFP) Franc", - "YER": "Yemen Rial", - "ZAR": "South Africa Rand", - "ZMW": "Zambia Kwacha" -} \ No newline at end of file diff --git a/src/currency.py b/src/currency.py index 7579d61..f86ddeb 100644 --- a/src/currency.py +++ b/src/currency.py @@ -8,9 +8,7 @@ # Created on 2014-02-24 # -""" -Script to update exchange rates from Yahoo! in the background. -""" +"""Script to update exchange rates from Yahoo! in the background.""" from __future__ import print_function, unicode_literals @@ -31,7 +29,7 @@ log = None -parse_yahoo_response = re.compile(r'{}(.+)=X'.format(REFERENCE_CURRENCY)).match +parse_yahoo_response = re.compile(r'{0}(.+)=X'.format(REFERENCE_CURRENCY)).match def grouper(n, iterable, fillvalue=None): @@ -71,13 +69,13 @@ def load_yahoo_rates(symbols): if symbol == REFERENCE_CURRENCY: count -= 1 continue - parts.append('{}{}=X'.format(REFERENCE_CURRENCY, symbol)) + parts.append('{0}{1}=X'.format(REFERENCE_CURRENCY, symbol)) query = ','.join(parts) url = YAHOO_BASE_URL.format(query) # Fetch data - # log.debug('Fetching {} ...'.format(url)) + # log.debug('Fetching {0} ...'.format(url)) r = web.get(url) r.raise_for_status() @@ -92,21 +90,32 @@ def load_yahoo_rates(symbols): m = parse_yahoo_response(name) if not m: # Couldn't get symbol - log.error('Invalid currency : {}'.format(name)) + log.error('Invalid currency : {0}'.format(name)) ycount += 1 continue symbol = m.group(1) - rate = float(rate) - if rate == 0: # Yahoo! returns 0.0 as rate for unsupported currencies - log.error('No exchange rate for : {}'.format(name)) + # Yahoo! returns 0.0 as rate for unsupported currencies + # NOTE: This has changed. "N/A" is now returned for + # unsupported currencies. That's handled in the script + # that generates the currency list, however: an invalid + # currency should never end up here. + + try: + rate = float(rate) + except ValueError: + log.error('No exchange rate for : {0}'.format(name)) + continue + + if rate == 0: + log.error('No exchange rate for : {0}'.format(name)) ycount += 1 continue rates[symbol] = rate ycount += 1 - assert ycount == count, 'Yahoo! returned {} results, not {}'.format( + assert ycount == count, 'Yahoo! returned {0} results, not {1}'.format( ycount, count) return rates diff --git a/src/info.py b/src/info.py index f62d232..7cfbd38 100755 --- a/src/info.py +++ b/src/info.py @@ -124,7 +124,7 @@ def main(wf): path = wf.datafile(CUSTOM_DEFINITIONS_FILENAME) if not os.path.exists(path): shutil.copy( - wf.workflowfile('{}.sample'.format( + wf.workflowfile('{0}.sample'.format( CUSTOM_DEFINITIONS_FILENAME)), path) @@ -148,15 +148,15 @@ def main(wf): wf.add_item('View Supported Currencies', 'View and search list of supported currencies', - autocomplete=' currencies {} '.format(DELIMITER), + autocomplete=' currencies {0} '.format(DELIMITER), icon=ICON_CURRENCY) wf.add_item(('Decimal Places in Results ' - '(current : {})'.format(wf.settings.get( + '(current : {0})'.format(wf.settings.get( 'decimal_places', DECIMAL_PLACES_DEFAULT))), 'View and search list of supported currencies', - autocomplete=' places {} '.format(DELIMITER), + autocomplete=' places {0} '.format(DELIMITER), icon=ICON_SETTINGS) wf.add_item('Edit Custom Units', @@ -200,8 +200,8 @@ def main(wf): icon=ICON_WARNING) for name, symbol in currencies: - wf.add_item('{} // {}'.format(name, symbol), - 'Use `{}` in conversions'.format(symbol), + wf.add_item('{0} // {1}'.format(name, symbol), + 'Use `{0}` in conversions'.format(symbol), icon=ICON_CURRENCY) wf.send_feedback() @@ -210,18 +210,18 @@ def main(wf): if query: if not query.isdigit(): - wf.add_item('Invalid number : {}'.format(query), + wf.add_item('Invalid number : {0}'.format(query), 'Please enter a number', icon=ICON_WARNING) else: - wf.add_item('Set decimal places to : {}'.format(query), + wf.add_item('Set decimal places to : {0}'.format(query), 'Hit `ENTER` to save', valid=True, - arg='--places {}'.format(query), + arg='--places {0}'.format(query), icon=ICON_SETTINGS) else: wf.add_item('Enter a number of decimal places', - 'Current number is {}'.format( + 'Current number is {0}'.format( wf.settings.get('decimal_places', DECIMAL_PLACES_DEFAULT)), icon=ICON_INFO) diff --git a/src/unit_definitions.txt b/src/unit_definitions.txt index c0e76d6..9349344 100644 --- a/src/unit_definitions.txt +++ b/src/unit_definitions.txt @@ -1,3 +1,12 @@ +# This file contains additional units for the Pint library. +# These units are registered automatically when the workflow +# is run. +# +# If you want to add your own units, *don't* add them here! +# Add them to the `unit_definitions.txt` file in the workflow's +# data directory. You can edit this file by entering "convinfo" +# in Alfred and selecting "Edit Custom Units". + # Barrel of oil equivalent barrel_of_oil_equivalent = 169.902 m**3 = boe diff --git a/src/workflow/update.py b/src/workflow/update.py index b946e79..687a0b0 100644 --- a/src/workflow/update.py +++ b/src/workflow/update.py @@ -179,7 +179,7 @@ def download_workflow(url): if (not url.endswith('.alfredworkflow') or not filename.endswith('.alfredworkflow')): - raise ValueError('Attachment `{}` not a workflow'.format(filename)) + raise ValueError('Attachment `{0}` not a workflow'.format(filename)) local_path = os.path.join(tempfile.gettempdir(), filename)