Add beancount output and tidy up output option.
This commit is contained in:
parent
55243bc093
commit
4bdfbcaa0a
3 changed files with 26 additions and 13 deletions
|
@ -68,26 +68,23 @@ def build_parser():
|
||||||
help='the source identifier')
|
help='the source identifier')
|
||||||
|
|
||||||
fetch_parser = subparsers.add_parser('fetch', help='fetch prices',
|
fetch_parser = subparsers.add_parser('fetch', help='fetch prices',
|
||||||
usage='pricehist fetch ID [-h] -p PAIR (-s START | -sx START) [-e END]')
|
usage='pricehist fetch ID [-h] -p PAIR (-s DATE | -sx DATE) [-e DATE] [-o FMT]')
|
||||||
fetch_parser.add_argument('source', metavar='ID', type=str,
|
fetch_parser.add_argument('source', metavar='ID', type=str,
|
||||||
choices=sources.by_id.keys(),
|
choices=sources.by_id.keys(),
|
||||||
help='the source identifier')
|
help='the source identifier')
|
||||||
fetch_parser.add_argument('-p', '--pair', dest='pair', type=str, required=True,
|
fetch_parser.add_argument('-p', '--pair', dest='pair', type=str, required=True,
|
||||||
help='pair, usually BASE/QUOTE, e.g. BTC/USD')
|
help='pair, usually BASE/QUOTE, e.g. BTC/USD')
|
||||||
fetch_start_group = fetch_parser.add_mutually_exclusive_group(required=True)
|
fetch_start_group = fetch_parser.add_mutually_exclusive_group(required=True)
|
||||||
fetch_start_group.add_argument('-s', '--start', dest='start', type=valid_date,
|
fetch_start_group.add_argument('-s', '--start', dest='start', metavar='DATE', type=valid_date,
|
||||||
help='start date, inclusive')
|
help='start date, inclusive')
|
||||||
fetch_start_group.add_argument('-sx', '--startx', dest='start', type=following_valid_date,
|
fetch_start_group.add_argument('-sx', '--startx', dest='start', metavar='DATE', type=following_valid_date,
|
||||||
help='start date, exclusive')
|
help='start date, exclusive')
|
||||||
fetch_parser.add_argument('-e', '--end', dest='end', type=valid_date,
|
fetch_parser.add_argument('-e', '--end', dest='end', metavar='DATE', type=valid_date,
|
||||||
default=today(),
|
default=today(),
|
||||||
help='end date, inclusive (default: today)')
|
help='end date, inclusive (default: today)')
|
||||||
fetch_parser.add_argument('-o', '--output', dest='output', metavar='FORMAT', type=str,
|
fetch_parser.add_argument('-o', '--output', dest='output', metavar='FMT', type=str,
|
||||||
choices=outputs.by_type.keys(),
|
choices=outputs.by_type.keys(),
|
||||||
default=next(iter(outputs.by_type)),
|
default=outputs.default,
|
||||||
help=f'output format (default: {next(iter(outputs.by_type))})')
|
help=f'output format (default: {outputs.default})')
|
||||||
|
|
||||||
# parser.add_argument('--csv', dest='csv', action='store_true',
|
|
||||||
# help='print full data as csv (instead of Ledger pricedb format)')
|
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
from .ledger import Ledger
|
from .beancount import Beancount
|
||||||
from .csv import CSV
|
from .csv import CSV
|
||||||
|
from .ledger import Ledger
|
||||||
|
|
||||||
|
default = 'ledger'
|
||||||
|
|
||||||
by_type = {
|
by_type = {
|
||||||
'ledger': Ledger,
|
'beancount': Beancount,
|
||||||
'csv': CSV
|
'csv': CSV,
|
||||||
|
'ledger': Ledger
|
||||||
}
|
}
|
||||||
|
|
12
src/pricehist/outputs/beancount.py
Normal file
12
src/pricehist/outputs/beancount.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
class Beancount():
|
||||||
|
|
||||||
|
def format(self, prices):
|
||||||
|
lines = []
|
||||||
|
for price in prices:
|
||||||
|
date = str(price.date).translate(str.maketrans('-','/'))
|
||||||
|
lines.append(f"{price.date} price {price.base} {price.amount} {price.quote}")
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
# https://beancount.github.io/docs/fetching_prices_in_beancount.html
|
||||||
|
# https://beancount.github.io/docs/beancount_language_syntax.html#commodities-currencies
|
||||||
|
# https://beancount.github.io/docs/beancount_language_syntax.html#comments
|
Loading…
Add table
Reference in a new issue