diff options
| author | Dennis Fink | 2020-08-07 12:11:22 +0200 |
|---|---|---|
| committer | Dennis Fink | 2020-08-07 12:11:22 +0200 |
| commit | 5d02bea91728ca15418da5dd7e942c6470fb9764 (patch) | |
| tree | c13eff5026dec585d56092a9d6b4d67fcfe49bfc | |
| parent | 2802c9584bd9a1c56805a0593e1191f9867b9918 (diff) | |
| download | patternutils-5d02bea91728ca15418da5dd7e942c6470fb9764.tar.gz patternutils-5d02bea91728ca15418da5dd7e942c6470fb9764.zip | |
Move json_dumps to utils and minimize json output if not human readable
| -rw-r--r-- | patternutils/commands/pmatch.py | 12 | ||||
| -rw-r--r-- | 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=(",", ":")) |
