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]() source = sources.by_id[args.source]()
output = outputs.by_type[args.output]() output = outputs.by_type[args.output]()
start = args.start or source.start() start = args.start or source.start()
type = args.type or (source.types() + ["unknown"])[0]
if start < source.start(): if start < source.start():
logging.warn( logging.warn(
@ -94,7 +95,7 @@ def cmd_fetch(args):
f"source start date of {source.start()}." 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: if args.renamebase or args.renamequote:
prices = [ prices = [
@ -127,7 +128,7 @@ def cmd_fetch(args):
decimal_places=if_not_none(args.quantize, default.decimal_places), 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(): def build_parser():

View file

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

View file

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

View file

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

View file

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