Files
@ 3b5a563ef64b
Branch filter:
Location: NPO-Accounting/oxrlib/oxrlib/commands/historical.py - annotation
3b5a563ef64b
1.2 KiB
text/x-python
historical: Write results to cache when appropriate.
f9c045a63efc 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 f9c045a63efc 3b5a563ef64b 3b5a563ef64b 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 667c214e9191 | from .. import rate as oxrrate
def format_one_rate(rate, from_amt, from_curr, to_curr):
return "{:g} {} = {:g} {}".format(
from_amt, from_curr, rate.convert(from_amt, from_curr, to_curr), to_curr)
def format_rate_pair(rate, from_curr, to_curr):
yield format_one_rate(rate, 1, from_curr, to_curr)
yield format_one_rate(rate, 1, to_curr, from_curr)
def run(config, stdout, stderr):
loaders = config.get_loaders()
with loaders.historical(config.args.date, config.args.base) as rate_json:
rate = oxrrate.Rate.from_json_file(rate_json)
if loaders.should_cache():
config.cache.save_rate(rate)
if not config.args.from_currency:
for from_curr in sorted(rate.rates):
print(*format_rate_pair(rate, from_curr, config.args.to_currency),
sep='\n', file=stdout)
elif config.args.amount is None:
print(*format_rate_pair(rate, config.args.from_currency, config.args.to_currency),
sep='\n', file=stdout)
else:
print(format_one_rate(rate, config.args.amount,
config.args.from_currency, config.args.to_currency),
file=stdout)
|