Initial output formats.
This commit is contained in:
parent
ae69b9ec4e
commit
55243bc093
4 changed files with 37 additions and 5 deletions
|
@ -3,6 +3,7 @@ import sys
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
from pricehist import sources
|
||||
from pricehist import outputs
|
||||
|
||||
def cli(args=None):
|
||||
parser = build_parser()
|
||||
|
@ -34,11 +35,9 @@ def cmd_source(args):
|
|||
def cmd_fetch(args):
|
||||
source = sources.by_id[args.source]()
|
||||
start = args.start or args.after
|
||||
print(f'source name = {source.name()}')
|
||||
print(f'start = {args.start}')
|
||||
print(f'end = {args.end}')
|
||||
print(f'pair = {args.pair}')
|
||||
print(str(source.fetch(args.pair, args.start, args.end)))
|
||||
output = outputs.by_type[args.output]()
|
||||
prices = source.fetch(args.pair, args.start, args.end)
|
||||
print(output.format(prices))
|
||||
|
||||
def build_parser():
|
||||
def valid_date(s):
|
||||
|
@ -83,6 +82,10 @@ def build_parser():
|
|||
fetch_parser.add_argument('-e', '--end', dest='end', type=valid_date,
|
||||
default=today(),
|
||||
help='end date, inclusive (default: today)')
|
||||
fetch_parser.add_argument('-o', '--output', dest='output', metavar='FORMAT', type=str,
|
||||
choices=outputs.by_type.keys(),
|
||||
default=next(iter(outputs.by_type)),
|
||||
help=f'output format (default: {next(iter(outputs.by_type))})')
|
||||
|
||||
# parser.add_argument('--csv', dest='csv', action='store_true',
|
||||
# help='print full data as csv (instead of Ledger pricedb format)')
|
||||
|
|
7
src/pricehist/outputs/__init__.py
Normal file
7
src/pricehist/outputs/__init__.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from .ledger import Ledger
|
||||
from .csv import CSV
|
||||
|
||||
by_type = {
|
||||
'ledger': Ledger,
|
||||
'csv': CSV
|
||||
}
|
9
src/pricehist/outputs/csv.py
Normal file
9
src/pricehist/outputs/csv.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
class CSV():
|
||||
|
||||
def format(self, prices):
|
||||
lines = ["date,base,quote,amount"]
|
||||
for price in prices:
|
||||
date = str(price.date).translate(str.maketrans('-','/'))
|
||||
line = ','.join([price.date, price.base, price.quote, str(price.amount)])
|
||||
lines.append(line)
|
||||
return "\n".join(lines)
|
13
src/pricehist/outputs/ledger.py
Normal file
13
src/pricehist/outputs/ledger.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Ledger():
|
||||
|
||||
def format(self, prices):
|
||||
lines = []
|
||||
for price in prices:
|
||||
date = str(price.date).translate(str.maketrans('-','/'))
|
||||
lines.append(f"P {price.date} 00:00:00 {price.base} {price.amount} {price.quote}")
|
||||
return "\n".join(lines)
|
||||
|
||||
# TODO support additional details of the format:
|
||||
# https://www.ledger-cli.org/3.0/doc/ledger3.html#Commodities-and-Currencies
|
||||
# https://www.ledger-cli.org/3.0/doc/ledger3.html#Commoditized-Amounts
|
||||
|
Loading…
Add table
Reference in a new issue