@@ -68,25 +68,21 @@ class Formatter:
return "{} = {}".format(
self.format_currency(from_amt, from_curr),
self.format_currency(to_amt, to_curr),
)
class LedgerFormatter(Formatter):
COST_FMT = '{{={}}}'
class BeancountFormatter(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
return None
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
return self.price_rates.convert(from_amt, from_curr, to_curr)
def normalize_rate(self, rate, prec=None):
if prec is None:
prec = self.rate_prec
_, digits, exponent = rate.normalize().as_tuple()
# Return ``prec`` nonzero digits of precision, if available.
@@ -169,32 +165,24 @@ class LedgerFormatter(Formatter):
return "{}\n{}".format(
self.format_denominated_rate(from_amt, from_curr, to_curr),
self.format_denominated_rate(to_amt, to_curr, None),
class BeancountFormatter(LedgerFormatter):
class LedgerFormatter(BeancountFormatter):
def __init__(self, cost_rates, price_rates=None,
signed_currencies=(), base_fmt='#,##0.###',
rate_precision=5, denomination=None):
super().__init__(
cost_rates,
price_rates,
(),
base_fmt,
rate_precision,
denomination,
class Formats(enum.Enum):
RAW = Formatter
LEDGER = LedgerFormatter
BEANCOUNT = BeancountFormatter