Source object and price type are available during output generation and used.

This commit is contained in:
Chris Berkhout 2021-05-25 12:09:03 +02:00
parent 03ea7664c5
commit e714c37e1f
5 changed files with 15 additions and 15 deletions

View file

@ -87,6 +87,7 @@ def cmd_fetch(args):
source = sources.by_id[args.source]()
output = outputs.by_type[args.output]()
start = args.start or source.start()
type = args.type or (source.types() + ["unknown"])[0]
if start < source.start():
logging.warn(
@ -94,7 +95,7 @@ def cmd_fetch(args):
f"source start date of {source.start()}."
)
prices = source.fetch(args.pair, args.type, start, args.end)
prices = source.fetch(args.pair, type, start, args.end)
if args.renamebase or args.renamequote:
prices = [
@ -127,7 +128,7 @@ def cmd_fetch(args):
decimal_places=if_not_none(args.quantize, default.decimal_places),
)
print(output.format(prices, fmt=fmt), end="")
print(output.format(prices, source, type, fmt=fmt), end="")
def build_parser():

View file

@ -2,7 +2,7 @@ from pricehist.format import Format
class Beancount:
def format(self, prices, fmt=Format()):
def format(self, prices, source=None, type=None, fmt=Format()):
lines = []
for price in prices:

View file

@ -2,13 +2,13 @@ from pricehist.format import Format
class CSV:
def format(self, prices, fmt=Format()):
lines = ["date,base,quote,amount"]
def format(self, prices, source=None, type=None, fmt=Format()):
lines = ["date,base,quote,amount,source,type"]
for price in prices:
date = str(price.date).replace("-", fmt.datesep)
amount_parts = f"{fmt.quantize(price.amount):,}".split(".")
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
amount = fmt.decimal.join(amount_parts)
line = ",".join([date, price.base, price.quote, amount])
line = ",".join([date, price.base, price.quote, amount, source.id(), type])
lines.append(line)
return "\n".join(lines) + "\n"

View file

@ -7,9 +7,8 @@ from pricehist.format import Format
class GnuCashSQL:
def format(self, prices, fmt=Format()):
source = "pricehist"
typ = "unknown"
def format(self, prices, source=None, type=None, fmt=Format()):
src = f"pricehist:{source.id()}"
values_parts = []
for price in prices:
@ -17,9 +16,9 @@ class GnuCashSQL:
amount = fmt.quantize(price.amount)
m = hashlib.sha256()
m.update(
"".join(
[date, price.base, price.quote, source, typ, str(amount)]
).encode("utf-8")
"".join([date, price.base, price.quote, src, type, str(amount)]).encode(
"utf-8"
)
)
guid = m.hexdigest()[0:32]
value_num = str(amount).replace(".", "")
@ -30,8 +29,8 @@ class GnuCashSQL:
f"'{date}', "
f"'{price.base}', "
f"'{price.quote}', "
f"'{source}', "
f"'{typ}', "
f"'{src}', "
f"'{type}', "
f"{value_num}, "
f"{value_denom}"
")"

View file

@ -2,7 +2,7 @@ from pricehist.format import Format
class Ledger:
def format(self, prices, fmt=Format()):
def format(self, prices, source=None, type=None, fmt=Format()):
lines = []
for price in prices:
date = str(price.date).replace("-", fmt.datesep)