Use babel for formatting numbers, if available
This commit is contained in:
parent
3e10dca932
commit
81f7bf50fc
1 changed files with 25 additions and 11 deletions
|
@ -24,15 +24,18 @@ try:
|
||||||
import requests
|
import requests
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
try:
|
||||||
|
from babel.numbers import format_currency
|
||||||
|
except ImportError:
|
||||||
|
format_currency = None
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
SYMBOL = {
|
SYMBOL = {
|
||||||
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩"
|
"GBP": u"£", "EUR": u"€", "USD": u"$", "JPY": u"¥", "KRW": u"₩"
|
||||||
}
|
}
|
||||||
DEFAULT_DEST = "USD,EUR,GBP"
|
DEFAULT_DEST = "USD,EUR,auto"
|
||||||
DEFAULT_SRC = "auto"
|
DEFAULT_SRC = "GBP"
|
||||||
DEFAULT_SRC_FALLBACK = "GBP"
|
|
||||||
|
|
||||||
API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"
|
API_URL = "https://markets.ft.com/data/currencies/ajax/conversion?baseCurrency={}&comparison={}"
|
||||||
LOCATION_URL = "https://ipvigilante.com/"
|
LOCATION_URL = "https://ipvigilante.com/"
|
||||||
|
@ -88,13 +91,22 @@ class Module(bumblebee.engine.Module):
|
||||||
|
|
||||||
rates = []
|
rates = []
|
||||||
for sym, rate in self._data:
|
for sym, rate in self._data:
|
||||||
rate = self.fmt_rate(rate)
|
rate_float = float(rate.replace(',',''))
|
||||||
rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
if format_currency:
|
||||||
|
rates.append(format_currency(rate_float, sym))
|
||||||
|
else:
|
||||||
|
rate = self.fmt_rate(rate)
|
||||||
|
rates.append(u"{}{}".format(rate, SYMBOL[sym] if sym in SYMBOL else sym))
|
||||||
|
|
||||||
basefmt = u"{}".format(self.parameter("sourceformat", "1{}={}"))
|
basefmt = u"{}".format(self.parameter("sourceformat", "{}={}"))
|
||||||
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "="))
|
ratefmt = u"{}".format(self.parameter("destinationdelimiter", "="))
|
||||||
|
|
||||||
return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates))
|
if format_currency:
|
||||||
|
base_val = format_currency(1, self._base)
|
||||||
|
else:
|
||||||
|
base_val = '1{}'.format(SYMBOL[self._base] if self._base in SYMBOL else self._base)
|
||||||
|
|
||||||
|
return basefmt.format(base_val, ratefmt.join(rates))
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self, widgets):
|
||||||
self._data = []
|
self._data = []
|
||||||
|
@ -111,15 +123,17 @@ class Module(bumblebee.engine.Module):
|
||||||
try:
|
try:
|
||||||
country = get_local_country()
|
country = get_local_country()
|
||||||
currency_map = load_country_to_currency()
|
currency_map = load_country_to_currency()
|
||||||
return currency_map.get(country, DEFAULT_SRC_FALLBACK)
|
return currency_map.get(country, DEFAULT_SRC)
|
||||||
except:
|
except:
|
||||||
return DEFAULT_SRC_FALLBACK
|
return DEFAULT_SRC
|
||||||
|
|
||||||
def fmt_rate(self, rate):
|
def fmt_rate(self, rate):
|
||||||
float_rate = float(rate.replace(',', ''))
|
float_rate = float(rate.replace(',', ''))
|
||||||
if not 0.01 < float_rate < 100:
|
if not 0.01 < float_rate < 100:
|
||||||
return rate
|
ret = rate
|
||||||
else:
|
else:
|
||||||
return "%.3g" % float_rate
|
ret = "%.3g" % float_rate
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue