0 1 0
Brett Smith - 3 months ago 2020-05-19 19:27:04
brettcsmith@brettcsmith.org
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.
1 file changed with 11 insertions and 23 deletions:
↑ Collapse Diff ↑
...
 
@@ -71,19 +71,15 @@ class Formatter:
71 71
 
        )
72 72
 

	
73 73
 

	
74
 
class LedgerFormatter(Formatter):
75
 
    COST_FMT = '{{={}}}'
74
 
class BeancountFormatter(Formatter):
75
 
    COST_FMT = '{{{}}}'
76 76
 
    PRICE_FMT = ' @ {}'
77 77
 

	
78 78
 
    def price_rate(self, from_amt, from_curr, to_curr):
79 79
 
        if self.price_rates is None:
80
 
            rates = self.cost_rates
80
 
            return None
81 81
 
        else:
82
 
            rates = self.price_rates
83
 
        return rates.convert(from_amt, from_curr, to_curr)
84
 

	
85
 
    def can_sign_currency(self, code):
86
 
        return len(babel.numbers.get_currency_symbol(code)) == 1
82
 
            return self.price_rates.convert(from_amt, from_curr, to_curr)
87 83
 

	
88 84
 
    def normalize_rate(self, rate, prec=None):
89 85
 
        if prec is None:
...
 
@@ -172,26 +168,18 @@ class LedgerFormatter(Formatter):
172 168
 
        )
173 169
 

	
174 170
 

	
175
 
class BeancountFormatter(LedgerFormatter):
176
 
    COST_FMT = '{{{}}}'
171
 
class LedgerFormatter(BeancountFormatter):
172
 
    COST_FMT = '{{={}}}'
177 173
 

	
178
 
    def __init__(self, cost_rates, price_rates=None,
179
 
                 signed_currencies=(), base_fmt='#,##0.###',
180
 
                 rate_precision=5, denomination=None):
181
 
        super().__init__(
182
 
            cost_rates,
183
 
            price_rates,
184
 
            (),
185
 
            base_fmt,
186
 
            rate_precision,
187
 
            denomination,
188
 
        )
174
 
    def can_sign_currency(self, code):
175
 
        return len(babel.numbers.get_currency_symbol(code)) == 1
189 176
 

	
190 177
 
    def price_rate(self, from_amt, from_curr, to_curr):
191 178
 
        if self.price_rates is None:
192
 
            return None
179
 
            rates = self.cost_rates
193 180
 
        else:
194
 
            return self.price_rates.convert(from_amt, from_curr, to_curr)
181
 
            rates = self.price_rates
182
 
        return rates.convert(from_amt, from_curr, to_curr)
195 183
 

	
196 184
 

	
197 185
 
class Formats(enum.Enum):
0 comments (0 inline, 0 general)