Add --fmt-jsonnums option.
This commit is contained in:
parent
a3e19f9bcf
commit
aabce7fe6f
6 changed files with 58 additions and 3 deletions
|
@ -95,7 +95,8 @@ pricehist fetch -h
|
|||
usage: pricehist fetch SOURCE PAIR [-h] [-vvv] [-t TYPE] [-s DATE | -sx DATE] [-e DATE | -ex DATE]
|
||||
[-o beancount|csv|json|jsonl|gnucash-sql|ledger] [--invert] [--quantize INT]
|
||||
[--fmt-base SYM] [--fmt-quote SYM] [--fmt-time TIME] [--fmt-decimal CHAR] [--fmt-thousands CHAR]
|
||||
[--fmt-symbol rightspace|right|leftspace|left] [--fmt-datesep CHAR] [--fmt-csvdelim CHAR]
|
||||
[--fmt-symbol rightspace|right|leftspace|left] [--fmt-datesep CHAR]
|
||||
[--fmt-csvdelim CHAR] [--fmt-jsonnums]
|
||||
|
||||
positional arguments:
|
||||
SOURCE the source identifier
|
||||
|
@ -120,6 +121,7 @@ optional arguments:
|
|||
--fmt-symbol LOCATION commodity symbol placement in output (default: rightspace)
|
||||
--fmt-datesep CHAR date separator in output (default: '-')
|
||||
--fmt-csvdelim CHAR field delimiter for CSV output (default: ',')
|
||||
--fmt-jsonnums numbers not strings for JSON output (default: False)
|
||||
```
|
||||
|
||||
### Choose and customize the output format
|
||||
|
|
|
@ -205,7 +205,7 @@ def build_parser():
|
|||
"[--fmt-base SYM] [--fmt-quote SYM] [--fmt-time TIME] "
|
||||
"[--fmt-decimal CHAR] [--fmt-thousands CHAR] "
|
||||
"[--fmt-symbol rightspace|right|leftspace|left] [--fmt-datesep CHAR] "
|
||||
"[--fmt-csvdelim CHAR]"
|
||||
"[--fmt-csvdelim CHAR] [--fmt-jsonnums]"
|
||||
),
|
||||
formatter_class=formatter,
|
||||
)
|
||||
|
@ -353,5 +353,11 @@ def build_parser():
|
|||
type=valid_char,
|
||||
help=f"field delimiter for CSV output (default: '{default_fmt.csvdelim}')",
|
||||
)
|
||||
fetch_parser.add_argument(
|
||||
"--fmt-jsonnums",
|
||||
dest="formatjsonnums",
|
||||
action="store_true",
|
||||
help=f"numbers not strings for JSON output (default: {default_fmt.jsonnums})",
|
||||
)
|
||||
|
||||
return parser
|
||||
|
|
|
@ -11,6 +11,7 @@ class Format:
|
|||
symbol: str = "rightspace"
|
||||
datesep: str = "-"
|
||||
csvdelim: str = ","
|
||||
jsonnums: bool = False
|
||||
|
||||
@classmethod
|
||||
def fromargs(cls, args):
|
||||
|
@ -27,6 +28,7 @@ class Format:
|
|||
symbol=if_not_none(args.formatsymbol, default.symbol),
|
||||
datesep=if_not_none(args.formatdatesep, default.datesep),
|
||||
csvdelim=if_not_none(args.formatcsvdelim, default.csvdelim),
|
||||
jsonnums=if_not_none(args.formatjsonnums, default.jsonnums),
|
||||
)
|
||||
|
||||
def format_date(self, date):
|
||||
|
|
|
@ -30,7 +30,10 @@ class JSON(BaseOutput):
|
|||
|
||||
for price in series.prices:
|
||||
date = fmt.format_date(price.date)
|
||||
amount = fmt.format_num(price.amount)
|
||||
if fmt.jsonnums:
|
||||
amount = float(price.amount)
|
||||
else:
|
||||
amount = fmt.format_num(price.amount)
|
||||
|
||||
data.append(
|
||||
{
|
||||
|
|
|
@ -125,3 +125,44 @@ def test_format_custom(json_out, series, mocker):
|
|||
).strip()
|
||||
+ "\n"
|
||||
)
|
||||
|
||||
|
||||
def test_format_numbers(json_out, series, mocker):
|
||||
source = mocker.MagicMock()
|
||||
source.id = mocker.MagicMock(return_value="sourceid")
|
||||
fmt = Format(jsonnums=True)
|
||||
result = json_out.format(series, source, fmt)
|
||||
assert (
|
||||
result
|
||||
== dedent(
|
||||
"""
|
||||
[
|
||||
{
|
||||
"date": "2021-01-01",
|
||||
"base": "BTC",
|
||||
"quote": "EUR",
|
||||
"amount": 24139.4648,
|
||||
"source": "sourceid",
|
||||
"type": "close"
|
||||
},
|
||||
{
|
||||
"date": "2021-01-02",
|
||||
"base": "BTC",
|
||||
"quote": "EUR",
|
||||
"amount": 26533.576,
|
||||
"source": "sourceid",
|
||||
"type": "close"
|
||||
},
|
||||
{
|
||||
"date": "2021-01-03",
|
||||
"base": "BTC",
|
||||
"quote": "EUR",
|
||||
"amount": 27001.2846,
|
||||
"source": "sourceid",
|
||||
"type": "close"
|
||||
}
|
||||
]
|
||||
"""
|
||||
).strip()
|
||||
+ "\n"
|
||||
)
|
||||
|
|
|
@ -14,6 +14,7 @@ def test_fromargs():
|
|||
"formatdatesep": None,
|
||||
"formatcsvdelim": None,
|
||||
"formatbase": None,
|
||||
"formatjsonnums": None,
|
||||
}
|
||||
args = namedtuple("args", arg_values.keys())(**arg_values)
|
||||
fmt = Format.fromargs(args)
|
||||
|
|
Loading…
Add table
Reference in a new issue