diff options
| -rw-r--r-- | patternutils/commands/pjson.py | 23 | ||||
| -rw-r--r-- | patternutils/commands/targs.py | 2 | ||||
| -rw-r--r-- | patternutils/utils.py | 10 |
3 files changed, 22 insertions, 13 deletions
diff --git a/patternutils/commands/pjson.py b/patternutils/commands/pjson.py index a6373ac..73786a4 100644 --- a/patternutils/commands/pjson.py +++ b/patternutils/commands/pjson.py @@ -84,23 +84,24 @@ def pjson( click.secho(e.msg, fg="red", err=True) raise SystemExit - has_no_key = False - has_no_match = False - if stream: data = map(utils.json_loads, read_from) # type: ignore else: - data = utils.json_loads(read_from.read()) + data = utils.json_loads(read_from.read()) # type: ignore matches = [] + has_no_key = False + has_no_match = False + has_empty_data = False for line in data: + if not line: - if abort_on_empty_data: - click.echo(f"{line} has no input.", fg="red", err=True) + has_empty_data = True + click.secho(f"{line} has no input.", fg="red", err=True) + if stream and abort_on_empty_data: raise SystemExit - else: - continue + continue try: match = apply_regex(line, key, regex_pattern_c) @@ -120,7 +121,11 @@ def pjson( matches.append(line) if not stream: - if (abort_on_no_key and has_no_key) or (abort_on_no_match and has_no_match): + if ( + (abort_on_no_key and has_no_key) + or (abort_on_no_match and has_no_match) + or (abort_on_empty_data and has_empty_data) + ): raise SystemExit click.echo(utils.json_dumps(matches, human_readable)) diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index ae8b74d..b262149 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -101,7 +101,7 @@ def targs( click.secho("jinja2 not installed!", fg="red", err=True) raise SystemExit - commands = {r: render(r) for r in data} + commands = [render(r) for r in data] if editor: try: diff --git a/patternutils/utils.py b/patternutils/utils.py index 5a33127..ae8a5fa 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,6 +1,6 @@ import json import os -from typing import Any, Dict, Generator +from typing import Any, Dict, Generator, List, Union import click @@ -10,10 +10,14 @@ def max_procs() -> int: return 1 if cpu_count is None else cpu_count * 2 -def edit(data: Dict[str, str]) -> Dict[str, str]: +def edit(data: Union[Dict[str, str], List[str]]) -> Dict[str, str]: longest = len(max(data, key=len)) - temporary = [f"{key.ljust(longest)}\t{value}\n" for key, value in data.items()] + + if isinstance(data, dict): + temporary = [f"{key.ljust(longest)}\t{value}\n" for key, value in data.items()] + else: + temporary = [f"{value.ljust(longest)}\t{value}\n" for value in data] if (edited := click.edit("".join(temporary))) is not None: new_data = dict() |
