-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmastercardConvert.py
executable file
·49 lines (42 loc) · 2.4 KB
/
mastercardConvert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
import argparse
import logging
from functools import partial
from domain import date, transaction
# Parse command line arguments
parser = argparse.ArgumentParser(description="Convert currency using MasterCard exchange rates",
epilog='If no date is specified, the most recent date with rates is used.')
parser.add_argument('from_quantity', type=float, help='Quantity of from_currency used in transaction')
parser.add_argument('from_currency', type=str.upper,
help='The currency to convert from, i.e. the transaction currency, e.g. GBP, USD, JPY')
parser.add_argument('to_currency', type=str.upper,
help='The currency to convert to, i.e. the card currency, e.g. GBP, USD, JPY')
parser.add_argument('-d', '--date',
help='Day the exchange was made in format YYYY-MM-DD. Only today and yesterday appear to be supported by MasterCard. Defaults to most recent day with rates.')
parser.add_argument('--log_level', help='Set logging level', default='WARNING',
type=str.upper,
choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'])
parser.add_argument('-t', '--today', action='store_true',
help='Use today\'s exchange rates. This may error if today\'s rates have not been uploaded')
parser.add_argument('-y', '--yesterday', action='count', default=0,
help='Uses yesterday\'s exchange rates. Repeat to go further back in time')
args = parser.parse_args()
logging.basicConfig(level=logging.getLevelName(args.log_level))
logging.debug(args)
# Figure out which date to use
if args.date is not None: # User-specified date
settle = partial(transaction.settle, exchange_rate_date=date.parse(args.date))
elif args.today: # Today
settle = partial(transaction.settle, exchange_rate_date=date.date_today())
elif args.yesterday > 0: # Yesterday (note that yesterday can be specified multiple times)
settle = partial(transaction.settle, exchange_rate_date=date.date_n_days_ago(args.yesterday))
else: # Use most recent date with published rates, discover date from initial MasterCard call
settle = transaction.settle_latest
# Get card amount from MasterCard
transaction = settle(
transaction_amount=args.from_quantity,
transaction_currency=args.from_currency,
card_currency=args.to_currency,
)
# Output conversion
print(transaction['card_amount'])