Extract Format#format_quote_amount.
This commit is contained in:
parent
8060c92092
commit
b26b3494d0
3 changed files with 20 additions and 24 deletions
|
@ -9,6 +9,22 @@ class Format:
|
||||||
symbol: str = "rightspace"
|
symbol: str = "rightspace"
|
||||||
datesep: str = "-"
|
datesep: str = "-"
|
||||||
|
|
||||||
|
def format_quote_amount(self, quote, amount):
|
||||||
|
formatted_amount = self.format_num(amount)
|
||||||
|
|
||||||
|
if self.symbol == "left":
|
||||||
|
qa_parts = [quote, formatted_amount]
|
||||||
|
elif self.symbol == "leftspace":
|
||||||
|
qa_parts = [quote, " ", formatted_amount]
|
||||||
|
elif self.symbol == "right":
|
||||||
|
qa_parts = [formatted_amount, quote]
|
||||||
|
else:
|
||||||
|
qa_parts = [formatted_amount, " ", quote]
|
||||||
|
|
||||||
|
quote_amount = "".join(qa_parts)
|
||||||
|
|
||||||
|
return quote_amount
|
||||||
|
|
||||||
def format_num(self, num):
|
def format_num(self, num):
|
||||||
parts = f"{num:,}".split(".")
|
parts = f"{num:,}".split(".")
|
||||||
parts[0] = parts[0].replace(",", self.thousands)
|
parts[0] = parts[0].replace(",", self.thousands)
|
||||||
|
|
|
@ -7,16 +7,9 @@ class Beancount(BaseOutput):
|
||||||
def format(self, series, source=None, fmt=Format()):
|
def format(self, series, source=None, fmt=Format()):
|
||||||
lines = []
|
lines = []
|
||||||
for price in series.prices:
|
for price in series.prices:
|
||||||
|
quote_amount = fmt.format_quote_amount(series.quote, price.amount)
|
||||||
amount = fmt.format_num(price.amount)
|
# TODO warn if fmt settings make an invalid number (not . for decimal)
|
||||||
# TODO warn if fmt settings make an invalid number
|
# TODO warn if fmt settings make an invalid quote (not right/rightspace)
|
||||||
|
|
||||||
qa_parts = [amount]
|
|
||||||
if fmt.symbol == "right":
|
|
||||||
qa_parts = qa_parts + [series.quote]
|
|
||||||
else:
|
|
||||||
qa_parts = qa_parts + [" ", series.quote]
|
|
||||||
quote_amount = "".join(qa_parts)
|
|
||||||
|
|
||||||
date = str(price.date).replace("-", fmt.datesep)
|
date = str(price.date).replace("-", fmt.datesep)
|
||||||
lines.append(f"{date} price {series.base} {quote_amount}")
|
lines.append(f"{date} price {series.base} {quote_amount}")
|
||||||
|
|
|
@ -8,20 +8,7 @@ class Ledger(BaseOutput):
|
||||||
lines = []
|
lines = []
|
||||||
for price in series.prices:
|
for price in series.prices:
|
||||||
date = str(price.date).replace("-", fmt.datesep)
|
date = str(price.date).replace("-", fmt.datesep)
|
||||||
|
quote_amount = fmt.format_quote_amount(series.quote, price.amount)
|
||||||
amount = fmt.format_num(price.amount)
|
|
||||||
|
|
||||||
qa_parts = [amount]
|
|
||||||
if fmt.symbol == "left":
|
|
||||||
qa_parts = [series.quote] + qa_parts
|
|
||||||
elif fmt.symbol == "leftspace":
|
|
||||||
qa_parts = [series.quote, " "] + qa_parts
|
|
||||||
elif fmt.symbol == "right":
|
|
||||||
qa_parts = qa_parts + [series.quote]
|
|
||||||
else:
|
|
||||||
qa_parts = qa_parts + [" ", series.quote]
|
|
||||||
quote_amount = "".join(qa_parts)
|
|
||||||
|
|
||||||
lines.append(f"P {date} {fmt.time} {series.base} {quote_amount}")
|
lines.append(f"P {date} {fmt.time} {series.base} {quote_amount}")
|
||||||
return "\n".join(lines) + "\n"
|
return "\n".join(lines) + "\n"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue