diff --git a/README.md b/README.md index ca74284..1b6b04c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/pricehist/cli.py b/src/pricehist/cli.py index d676856..9e4a69d 100644 --- a/src/pricehist/cli.py +++ b/src/pricehist/cli.py @@ -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 diff --git a/src/pricehist/format.py b/src/pricehist/format.py index fb7a8de..14207e7 100644 --- a/src/pricehist/format.py +++ b/src/pricehist/format.py @@ -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): diff --git a/src/pricehist/outputs/json.py b/src/pricehist/outputs/json.py index 9deb90e..8983e8e 100644 --- a/src/pricehist/outputs/json.py +++ b/src/pricehist/outputs/json.py @@ -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( { diff --git a/tests/pricehist/outputs/test_json.py b/tests/pricehist/outputs/test_json.py index 608007e..4b3d3fd 100644 --- a/tests/pricehist/outputs/test_json.py +++ b/tests/pricehist/outputs/test_json.py @@ -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" + ) diff --git a/tests/pricehist/test_format.py b/tests/pricehist/test_format.py index 2795037..e816850 100644 --- a/tests/pricehist/test_format.py +++ b/tests/pricehist/test_format.py @@ -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)