From 5d02bea91728ca15418da5dd7e942c6470fb9764 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 7 Aug 2020 12:11:22 +0200 Subject: Move json_dumps to utils and minimize json output if not human readable --- patternutils/commands/pmatch.py | 12 +++--------- patternutils/utils.py | 10 +++++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/patternutils/commands/pmatch.py b/patternutils/commands/pmatch.py index 376776e..b889145 100644 --- a/patternutils/commands/pmatch.py +++ b/patternutils/commands/pmatch.py @@ -10,6 +10,7 @@ import click from .. import config from .. import match as mat +from .. import utils def walk( @@ -49,13 +50,6 @@ def apply_regex( return matches, has_invalid_matches -def json_dumps(data: Any, human_readable: bool = False) -> str: - if human_readable: - return json.dumps(data, indent=4, sort_keys=True) - else: - return json.dumps(data) - - @click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) @click.option("-d", "--directory", multiple=True, default=["./"]) @click.option( @@ -154,9 +148,9 @@ def pmatch( if stream: for m in matches: - click.echo(json_dumps(m, human_readable)) + click.echo(utils.json_dumps(m, human_readable)) else: - click.echo(json_dumps(matches, human_readable)) + click.echo(utils.json_dumps(matches, human_readable)) if __name__ == "__main__": diff --git a/patternutils/utils.py b/patternutils/utils.py index 84720b7..266f3b9 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,5 +1,6 @@ import os -from typing import Dict +import json +from typing import Dict, Any import click @@ -29,3 +30,10 @@ def edit(data: Dict[str, str]) -> Dict[str, str]: raise RuntimeError else: return data + + +def json_dumps(data: Any, humand_readable: bool = False) -> str: + if humand_readable: + return json.dumps(data, indent=4, sort_keys=True) + else: + return json.dumps(data, separators=(",", ":")) -- cgit v1.3.1