diff --git a/oxrlib/commands/historical.py b/oxrlib/commands/historical.py index eb41f560823e15d8ae4ac1702a9c1679cda03865..854eba7960f7cc34e058b8deee62335299685410 100644 --- a/oxrlib/commands/historical.py +++ b/oxrlib/commands/historical.py @@ -12,9 +12,11 @@ except ImportError: import enum34 as enum class Formatter: - def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###', + def __init__(self, cost_rates, price_rates=None, + signed_currencies=(), base_fmt='#,##0.###', rate_precision=5, denomination=None): - self.rate = rate + self.cost_rates = cost_rates + self.price_rates = price_rates self.base_fmt = base_fmt self.base_fmt_noprec = base_fmt.rsplit('.', 1)[0] self.signed_currencies = set(code for code in signed_currencies @@ -48,7 +50,7 @@ class Formatter: def format_rate_pair(self, from_curr, to_curr): from_amt = 1 - to_amt = self.rate.convert(from_amt, from_curr, to_curr) + to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr) return "{} {} = {} {}".format( self.format_rate(from_amt), from_curr, self.format_rate(to_amt), to_curr, @@ -62,7 +64,7 @@ class Formatter: ) def format_conversion(self, from_amt, from_curr, to_curr): - to_amt = self.rate.convert(from_amt, from_curr, to_curr) + to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr) return "{} = {}".format( self.format_currency(from_amt, from_curr), self.format_currency(to_amt, to_curr), @@ -70,6 +72,16 @@ class Formatter: class LedgerFormatter(Formatter): + COST_FMT = '{{={}}}' + PRICE_FMT = ' @ {}' + + def price_rate(self, from_amt, from_curr, to_curr): + if self.price_rates is None: + rates = self.cost_rates + else: + rates = self.price_rates + return rates.convert(from_amt, from_curr, to_curr) + def can_sign_currency(self, code): return len(babel.numbers.get_currency_symbol(code)) == 1 @@ -87,21 +99,42 @@ class LedgerFormatter(Formatter): qrate = rate return qrate.normalize() - def format_rate(self, rate): - return str(self.normalize_rate(rate)) - - def format_ledger_rate_raw(self, rate, curr): - rate_s = self.format_currency(rate, curr, currency_digits=False) - return "{{={0}}} @ {0}".format(rate_s) + def normalize_enough(self, rate, curr, from_amt, to_amt, prec=None): + if prec is None: + prec = self.rate_prec + # Starting from prec, find the least amount of precision to + # make sure from_amt converts exactly to to_amt. + for try_prec in itertools.count(prec): + try_rate = self.normalize_rate(rate, try_prec) + got_amt = self.currency_decimal(from_amt * try_rate, curr) + # If got_amt == to_amt, this is enough precision to do the + # conversion exactly, so we're done. + # If try_rate == rate, there's no more precision available, so stop. + if (got_amt == to_amt) or (try_rate == rate): + break + return try_rate - def format_ledger_rate(self, rate, curr): - return self.format_ledger_rate_raw(self.normalize_rate(rate), curr) + def _pretty_rate(self, fmt, rate, curr, from_amt=None, to_amt=None): + if to_amt is None: + rate = self.normalize_rate(rate) + else: + rate = self.normalize_enough(rate, curr, from_amt, to_amt) + return fmt.format(self.format_currency(rate, curr, currency_digits=False)) def format_rate_pair(self, from_curr, to_curr): from_amt = 1 - to_amt = self.rate.convert(from_amt, from_curr, to_curr) - return "{} {} {}".format( - from_amt, from_curr, self.format_ledger_rate(to_amt, to_curr)) + cost = self.cost_rates.convert(from_amt, from_curr, to_curr) + price = self.price_rate(from_amt, from_curr, to_curr) + if price is None: + price_s = '' + else: + price_s = self._pretty_rate(self.PRICE_FMT, price, to_curr) + return "{} {} {}{}".format( + from_amt, + from_curr, + self._pretty_rate(self.COST_FMT, cost, to_curr), + price_s, + ) def _denomination_for(self, currency, default): if self.denomination is None: @@ -116,22 +149,23 @@ class LedgerFormatter(Formatter): amt_s = self.format_currency(amount, currency) if denomination is None: return amt_s - full_rate = self.rate.convert(1, currency, denomination) - # Starting from self.rate_prec, find the least amount of precision to - # make sure the `from` amount converts exactly to the `to` amount. - to_amt = self.currency_decimal(amount * full_rate, denomination) - for prec in itertools.count(self.rate_prec): - rate = self.normalize_rate(full_rate, prec) - got_amt = self.currency_decimal(amount * rate, denomination) - # If got_amt == to_amt, this is enough precision to do the - # conversion exactly, so we're done. - # If rate == full_rate, there's no more precision available, so stop. - if (got_amt == to_amt) or (rate == full_rate): - break - return "{} {}".format(amt_s, self.format_ledger_rate_raw(rate, denomination)) + cost = self.cost_rates.convert(1, currency, denomination) + price = self.price_rate(1, currency, denomination) + to_amt = self.currency_decimal(amount * cost, denomination) + if price is None: + price_s = '' + else: + price_s = self._pretty_rate( + self.PRICE_FMT, price, denomination, amount, to_amt, + ) + return "{} {}{}".format( + amt_s, + self._pretty_rate(self.COST_FMT, cost, denomination, amount, to_amt), + price_s, + ) def format_conversion(self, from_amt, from_curr, to_curr): - to_amt = self.rate.convert(from_amt, from_curr, to_curr) + to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr) return "{}\n{}".format( self.format_denominated_rate(from_amt, from_curr, to_curr), self.format_denominated_rate(to_amt, to_curr, None), @@ -147,19 +181,28 @@ class Formats(enum.Enum): return cls[s.upper()] +def load_rates(config, loaders, date): + with loaders.historical(date, config.args.base) as rate_json: + rates = oxrrate.Rate.from_json_file(rate_json) + if loaders.should_cache(): + config.cache.save_rate(rates) + return rates + def run(config, stdout, stderr): loaders = config.get_loaders() - with loaders.historical(config.args.date, config.args.base) as rate_json: - rate = oxrrate.Rate.from_json_file(rate_json) - if loaders.should_cache(): - config.cache.save_rate(rate) + cost_rates = load_rates(config, loaders, config.args.date) + if config.args.from_date is None: + price_rates = None + else: + price_rates = load_rates(config, loaders, config.args.from_date) formatter = config.args.output_format.value( - rate, + cost_rates, + price_rates, config.args.signed_currencies, denomination=config.args.denomination, ) if not config.args.from_currency: - for from_curr in sorted(rate.rates): + for from_curr in sorted(cost_rates.rates): print(formatter.format_rate_pair_bidir(from_curr, config.args.to_currency), file=stdout) elif config.args.amount is None: