historical: Swap Ledger and Beancount formatters in the class hierarchy.

This makes sense for a couple of reasons:

* The Beancount formatter has "less features" than the Ledger formatter, so
  this is a more "logical" organization of the hierarchy anyway. Note how
  this eliminates the need for the BeancountFormatter.__init__ override to
  turn off Ledger features.

* Any future work will probably be focused on the Beancount formatter, so
  this reduces the amount of code you have to understand and hold in your
  head to do that.
This commit is contained in:
Brett Smith 2020-05-19 15:27:04 -04:00
parent c3fd55ec15
commit 8dede9d139

View file

@ -71,19 +71,15 @@ class Formatter:
) )
class LedgerFormatter(Formatter): class BeancountFormatter(Formatter):
COST_FMT = '{{={}}}' COST_FMT = '{{{}}}'
PRICE_FMT = ' @ {}' PRICE_FMT = ' @ {}'
def price_rate(self, from_amt, from_curr, to_curr): def price_rate(self, from_amt, from_curr, to_curr):
if self.price_rates is None: if self.price_rates is None:
rates = self.cost_rates return None
else: else:
rates = self.price_rates return self.price_rates.convert(from_amt, from_curr, to_curr)
return rates.convert(from_amt, from_curr, to_curr)
def can_sign_currency(self, code):
return len(babel.numbers.get_currency_symbol(code)) == 1
def normalize_rate(self, rate, prec=None): def normalize_rate(self, rate, prec=None):
if prec is None: if prec is None:
@ -172,26 +168,18 @@ class LedgerFormatter(Formatter):
) )
class BeancountFormatter(LedgerFormatter): class LedgerFormatter(BeancountFormatter):
COST_FMT = '{{{}}}' COST_FMT = '{{={}}}'
def __init__(self, cost_rates, price_rates=None, def can_sign_currency(self, code):
signed_currencies=(), base_fmt='#,##0.###', return len(babel.numbers.get_currency_symbol(code)) == 1
rate_precision=5, denomination=None):
super().__init__(
cost_rates,
price_rates,
(),
base_fmt,
rate_precision,
denomination,
)
def price_rate(self, from_amt, from_curr, to_curr): def price_rate(self, from_amt, from_curr, to_curr):
if self.price_rates is None: if self.price_rates is None:
return None rates = self.cost_rates
else: else:
return self.price_rates.convert(from_amt, from_curr, to_curr) rates = self.price_rates
return rates.convert(from_amt, from_curr, to_curr)
class Formats(enum.Enum): class Formats(enum.Enum):