historical: Introduce --output-format option.
Beancount output will be added here.
This commit is contained in:
parent
27dbe14b94
commit
80fd49a98a
3 changed files with 41 additions and 10 deletions
|
@ -6,6 +6,11 @@ import babel.numbers
|
||||||
|
|
||||||
from .. import rate as oxrrate
|
from .. import rate as oxrrate
|
||||||
|
|
||||||
|
try:
|
||||||
|
import enum
|
||||||
|
except ImportError:
|
||||||
|
import enum34 as enum
|
||||||
|
|
||||||
class Formatter:
|
class Formatter:
|
||||||
def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###'):
|
def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###'):
|
||||||
self.rate = rate
|
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):
|
def run(config, stdout, stderr):
|
||||||
loaders = config.get_loaders()
|
loaders = config.get_loaders()
|
||||||
with loaders.historical(config.args.date, config.args.base) as rate_json:
|
with loaders.historical(config.args.date, config.args.base) as rate_json:
|
||||||
rate = oxrrate.Rate.from_json_file(rate_json)
|
rate = oxrrate.Rate.from_json_file(rate_json)
|
||||||
if loaders.should_cache():
|
if loaders.should_cache():
|
||||||
config.cache.save_rate(rate)
|
config.cache.save_rate(rate)
|
||||||
if config.args.ledger:
|
if config.args.output_format is Formats.LEDGER:
|
||||||
formatter = LedgerFormatter(rate, config.args.signed_currencies,
|
formatter = LedgerFormatter(rate, config.args.signed_currencies,
|
||||||
denomination=config.args.denomination)
|
denomination=config.args.denomination)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -10,6 +10,7 @@ import babel.dates
|
||||||
import babel.numbers
|
import babel.numbers
|
||||||
|
|
||||||
from . import cache, loaders
|
from . import cache, loaders
|
||||||
|
from .commands import historical
|
||||||
|
|
||||||
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
|
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
|
||||||
|
|
||||||
|
@ -97,22 +98,35 @@ class Configuration:
|
||||||
command='historical',
|
command='historical',
|
||||||
amount=None,
|
amount=None,
|
||||||
from_currency=None,
|
from_currency=None,
|
||||||
ledger=None,
|
from_date=None,
|
||||||
|
to_currency=None,
|
||||||
|
output_format=None,
|
||||||
)
|
)
|
||||||
hist_parser.add_argument(
|
hist_parser.add_argument(
|
||||||
'--base',
|
'--base',
|
||||||
metavar='CODE', type=currency_code,
|
metavar='CODE', type=currency_code,
|
||||||
help="Base currency (default USD)",
|
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(
|
hist_parser.add_argument(
|
||||||
'--ledger', '-L',
|
'--ledger', '-L',
|
||||||
action='store_true',
|
action='store_const',
|
||||||
help="Output the rate or conversion in Ledger format",
|
dest='output_format',
|
||||||
|
const=historical.Formats.LEDGER,
|
||||||
|
help=argparse.SUPPRESS,
|
||||||
)
|
)
|
||||||
hist_parser.add_argument(
|
hist_parser.add_argument(
|
||||||
'--no-ledger',
|
'--no-ledger',
|
||||||
action='store_false', dest='ledger',
|
action='store_const',
|
||||||
help="Turn off an earlier --ledger setting",
|
dest='output_format',
|
||||||
|
const=historical.Formats.RAW,
|
||||||
|
help=argparse.SUPPRESS,
|
||||||
)
|
)
|
||||||
hist_parser.add_argument(
|
hist_parser.add_argument(
|
||||||
'--denomination',
|
'--denomination',
|
||||||
|
@ -213,7 +227,12 @@ class Configuration:
|
||||||
pref_currency = self.args.denomination or self._user_currency(self.args.base)
|
pref_currency = self.args.denomination or self._user_currency(self.args.base)
|
||||||
self._read_from_conffile('signed_currencies', 'Historical', pref_currency,
|
self._read_from_conffile('signed_currencies', 'Historical', pref_currency,
|
||||||
currency_list, convert_fallback=True)
|
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')
|
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)
|
words = iter(word for word in raw_words if word is not None)
|
||||||
try:
|
try:
|
||||||
|
@ -229,8 +248,6 @@ class Configuration:
|
||||||
self.args.from_currency = self._convert_or_error(currency_code, next_word)
|
self.args.from_currency = self._convert_or_error(currency_code, next_word)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
pass
|
pass
|
||||||
self.args.to_currency = None
|
|
||||||
self.args.from_date = None
|
|
||||||
for next_word in words:
|
for next_word in words:
|
||||||
next_lower = next_word.lower()
|
next_lower = next_word.lower()
|
||||||
if next_lower in self.CURRENCY_PREPOSITIONS:
|
if next_lower in self.CURRENCY_PREPOSITIONS:
|
||||||
|
|
|
@ -58,7 +58,7 @@ def build_config(
|
||||||
'amount': None if amount is None else decimal.Decimal(amount),
|
'amount': None if amount is None else decimal.Decimal(amount),
|
||||||
'from_currency': from_currency,
|
'from_currency': from_currency,
|
||||||
'to_currency': base if to_currency is None else to_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,
|
'signed_currencies': [base] if signed_currencies is None else signed_currencies,
|
||||||
'denomination': denomination,
|
'denomination': denomination,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue