Add fetch command with start and end dates.
This commit is contained in:
parent
bed7db7ee7
commit
f1a1869e35
1 changed files with 26 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from pricehist import sources
|
from pricehist import sources
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ def cli(args=None):
|
||||||
cmd_sources(args)
|
cmd_sources(args)
|
||||||
elif (args.command == 'source'):
|
elif (args.command == 'source'):
|
||||||
cmd_source(args)
|
cmd_source(args)
|
||||||
|
elif (args.command == 'fetch'):
|
||||||
|
cmd_fetch(args)
|
||||||
else:
|
else:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
|
@ -28,7 +30,13 @@ def cmd_source(args):
|
||||||
print(f'URL : {source.source_url()}')
|
print(f'URL : {source.source_url()}')
|
||||||
print(f'Bases : {", ".join(source.bases())}')
|
print(f'Bases : {", ".join(source.bases())}')
|
||||||
print(f'Quotes : {", ".join(source.quotes())}')
|
print(f'Quotes : {", ".join(source.quotes())}')
|
||||||
pass
|
|
||||||
|
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}')
|
||||||
|
|
||||||
def build_parser():
|
def build_parser():
|
||||||
def valid_date(s):
|
def valid_date(s):
|
||||||
|
@ -41,6 +49,9 @@ def build_parser():
|
||||||
msg = "Not a valid date: '{0}'.".format(s)
|
msg = "Not a valid date: '{0}'.".format(s)
|
||||||
raise argparse.ArgumentTypeError(msg)
|
raise argparse.ArgumentTypeError(msg)
|
||||||
|
|
||||||
|
def following_valid_date(s):
|
||||||
|
return str(datetime.strptime(valid_date(s), "%Y-%m-%d").date() + timedelta(days=1))
|
||||||
|
|
||||||
def today():
|
def today():
|
||||||
return str(datetime.now().date())
|
return str(datetime.now().date())
|
||||||
|
|
||||||
|
@ -51,17 +62,22 @@ def build_parser():
|
||||||
sources_parser = subparsers.add_parser('sources', help='list sources')
|
sources_parser = subparsers.add_parser('sources', help='list sources')
|
||||||
|
|
||||||
source_parser = subparsers.add_parser('source', help='show source details')
|
source_parser = subparsers.add_parser('source', help='show source details')
|
||||||
source_parser.add_argument('identifier', metavar='ID', type=str,
|
source_parser.add_argument('identifier', metavar='id', type=str,
|
||||||
choices=sources.by_id.keys(),
|
choices=sources.by_id.keys(),
|
||||||
help='the source identifier')
|
help='the source identifier')
|
||||||
|
|
||||||
# parser.add_argument('--start', dest='start', type=valid_date,
|
fetch_parser = subparsers.add_parser('fetch', help='fetch prices')
|
||||||
# default='2009-01-03',
|
fetch_parser.add_argument('source', metavar='source_id', type=str,
|
||||||
# help='start date (default: 2009-01-03)')
|
choices=sources.by_id.keys(),
|
||||||
|
help='the source identifier')
|
||||||
# parser.add_argument('--end', dest='end', type=valid_date,
|
fetch_start_group = fetch_parser.add_mutually_exclusive_group(required=True)
|
||||||
# default=today(),
|
fetch_start_group.add_argument('-s', '--start', dest='start', type=valid_date,
|
||||||
# help='end date (default: today)')
|
help='start date (inclusive)')
|
||||||
|
fetch_start_group.add_argument('-sx', '--startx', dest='start', type=following_valid_date,
|
||||||
|
help='start date (exclusive)')
|
||||||
|
fetch_parser.add_argument('-e', '--end', dest='end', type=valid_date,
|
||||||
|
default=today(),
|
||||||
|
help='end date (inclusive, default: today)')
|
||||||
|
|
||||||
# parser.add_argument('--csv', dest='csv', action='store_true',
|
# parser.add_argument('--csv', dest='csv', action='store_true',
|
||||||
# help='print full data as csv (instead of Ledger pricedb format)')
|
# help='print full data as csv (instead of Ledger pricedb format)')
|
||||||
|
|
Loading…
Add table
Reference in a new issue