Improve source info printing, add notes field.

This commit is contained in:
Chris Berkhout 2021-05-04 13:53:51 +02:00
parent 02ab5212b9
commit 85205eaba9
4 changed files with 43 additions and 7 deletions

View file

@ -1,5 +1,7 @@
import argparse import argparse
from datetime import datetime, timedelta from datetime import datetime, timedelta
from textwrap import TextWrapper
import shutil
from pricehist import outputs, sources from pricehist import outputs, sources
from pricehist import __version__ from pricehist import __version__
@ -33,13 +35,35 @@ def cmd_sources(args):
def cmd_source(args): def cmd_source(args):
def print_field(key, value, key_width, output_width, force=True):
separator = " : "
initial_indent = key + (" " * (key_width - len(key))) + separator
subsequent_indent = " " * len(initial_indent)
wrapper = TextWrapper(
width=output_width,
drop_whitespace=True,
initial_indent=initial_indent,
subsequent_indent=subsequent_indent,
break_long_words=force,
)
first, *rest = value.split("\n")
first_output = wrapper.wrap(first)
wrapper.initial_indent = subsequent_indent
rest_output = sum([wrapper.wrap(line) for line in rest], [])
# TODO use str.splitlines()?
print("\n".join(first_output + rest_output))
source = sources.by_id[args.identifier] source = sources.by_id[args.identifier]
print(f"ID : {source.id()}") key_width = 11
print(f"Name : {source.name()}") output_width = shutil.get_terminal_size().columns
print(f"Description : {source.description()}")
print(f"URL : {source.source_url()}") print_field("ID", source.id(), key_width, output_width)
print(f'Bases : {", ".join(source.bases())}') print_field("Name", source.name(), key_width, output_width)
print(f'Quotes : {", ".join(source.quotes())}') print_field("Description", source.description(), key_width, output_width)
print_field("URL", source.source_url(), key_width, output_width, force=False)
print_field("Notes", source.notes(), key_width, output_width)
print_field("Bases", ", ".join(source.bases()), key_width, output_width)
print_field("Quotes", ", ".join(source.quotes()), key_width, output_width)
def cmd_fetch(args): def cmd_fetch(args):

View file

@ -18,7 +18,7 @@ class CoinDesk:
@staticmethod @staticmethod
def description(): def description():
return ( return (
"An average of bitcoin prices across leading global exchanges. " "An average of bitcoin prices across leading global exchanges. \n"
"Powered by CoinDesk, https://www.coindesk.com/price/bitcoin" "Powered by CoinDesk, https://www.coindesk.com/price/bitcoin"
) )
@ -26,6 +26,10 @@ class CoinDesk:
def source_url(): def source_url():
return "https://www.coindesk.com/coindesk-api" return "https://www.coindesk.com/coindesk-api"
@staticmethod
def notes():
return ""
@staticmethod @staticmethod
def bases(): def bases():
return ["BTC"] return ["BTC"]

View file

@ -29,6 +29,10 @@ class CoinMarketCap:
# curl '.../v1/fiat/map?include_metals=true' | jq . | tee fiat-map.json # curl '.../v1/fiat/map?include_metals=true' | jq . | tee fiat-map.json
# curl '.../v1/cryptocurrency/map' | jq . | tee cryptocurrency-map.json # curl '.../v1/cryptocurrency/map' | jq . | tee cryptocurrency-map.json
@staticmethod
def notes():
return ""
@staticmethod @staticmethod
def bases(): def bases():
return [] return []

View file

@ -24,6 +24,10 @@ class ECB:
def source_url(): def source_url():
return "https://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html" return "https://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html"
@staticmethod
def notes():
return ""
@staticmethod @staticmethod
def bases(): def bases():
return ["EUR"] return ["EUR"]