historical: Introduce --output-format option.

Beancount output will be added here.
This commit is contained in:
Brett Smith 2020-05-16 15:07:20 -04:00
parent 27dbe14b94
commit 80fd49a98a
3 changed files with 41 additions and 10 deletions

View file

@ -6,6 +6,11 @@ import babel.numbers
from .. import rate as oxrrate
try:
import enum
except ImportError:
import enum34 as enum
class Formatter:
def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###'):
self.rate = rate
@ -130,13 +135,22 @@ class LedgerFormatter(Formatter):
)
class Formats(enum.Enum):
RAW = Formatter
LEDGER = LedgerFormatter
@classmethod
def from_arg(cls, s):
return cls[s.upper()]
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 config.args.ledger:
if config.args.output_format is Formats.LEDGER:
formatter = LedgerFormatter(rate, config.args.signed_currencies,
denomination=config.args.denomination)
else:

View file

@ -10,6 +10,7 @@ import babel.dates
import babel.numbers
from . import cache, loaders
from .commands import historical
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
@ -97,22 +98,35 @@ class Configuration:
command='historical',
amount=None,
from_currency=None,
ledger=None,
from_date=None,
to_currency=None,
output_format=None,
)
hist_parser.add_argument(
'--base',
metavar='CODE', type=currency_code,
help="Base currency (default USD)",
)
hist_parser.add_argument(
'--output-format',
type=historical.Formats.from_arg,
choices=[fmt.name.lower() for fmt in historical.Formats],
help="Output format. Choices are %(choices)s. Default `raw`.",
)
# --ledger and --no-ledger predate --output-format.
hist_parser.add_argument(
'--ledger', '-L',
action='store_true',
help="Output the rate or conversion in Ledger format",
action='store_const',
dest='output_format',
const=historical.Formats.LEDGER,
help=argparse.SUPPRESS,
)
hist_parser.add_argument(
'--no-ledger',
action='store_false', dest='ledger',
help="Turn off an earlier --ledger setting",
action='store_const',
dest='output_format',
const=historical.Formats.RAW,
help=argparse.SUPPRESS,
)
hist_parser.add_argument(
'--denomination',
@ -213,7 +227,12 @@ class Configuration:
pref_currency = self.args.denomination or self._user_currency(self.args.base)
self._read_from_conffile('signed_currencies', 'Historical', pref_currency,
currency_list, convert_fallback=True)
self._read_from_conffile('ledger', 'Historical', False, getter='getboolean')
self._read_from_conffile('output_format', 'Historical', None, historical.Formats.from_arg)
if self.args.output_format is None:
if self.conffile.getboolean('Historical', 'ledger', fallback=None):
self.args.output_format = historical.Formats.LEDGER
else:
self.args.output_format = historical.Formats.RAW
raw_words = iter(getattr(self.args, 'word' + c) for c in '123456')
words = iter(word for word in raw_words if word is not None)
try:
@ -229,8 +248,6 @@ class Configuration:
self.args.from_currency = self._convert_or_error(currency_code, next_word)
except StopIteration:
pass
self.args.to_currency = None
self.args.from_date = None
for next_word in words:
next_lower = next_word.lower()
if next_lower in self.CURRENCY_PREPOSITIONS:

View file

@ -58,7 +58,7 @@ def build_config(
'amount': None if amount is None else decimal.Decimal(amount),
'from_currency': from_currency,
'to_currency': base if to_currency is None else to_currency,
'ledger': ledger,
'output_format': oxrhist.Formats['LEDGER' if ledger else 'RAW'],
'signed_currencies': [base] if signed_currencies is None else signed_currencies,
'denomination': denomination,
})