diff options
Diffstat (limited to 'patternutils/commands/targs.py')
| -rw-r--r-- | patternutils/commands/targs.py | 79 |
1 files changed, 47 insertions, 32 deletions
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index 5971ea2..e25679f 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -34,25 +34,46 @@ def run_command( run(prepared_command) -@click.command(context_settings={'help_option_names': ('-h', '--help', '-?')}) -@click.option('--read-from', default='-', type=click.File('r')) -@click.option('-s', '--shell', is_flag=True, default=False, - help='run command in a shell') -@click.option('-t', '--template-engine', default='python', type=click.Choice(['python', 'jinja2']), - help='select the template engine to use') -@click.option('-v', '--verbose', is_flag=True, default=False, - help='explain what is being done') -@click.option('-P', '--max-procs', default=1, type=click.IntRange(min=0, clamp=True)) -@click.option('-p', '--use-predefined-template', is_flag=True, default=False, - help='use predefined template') -@click.option('--redirect-stdout', is_flag=True, default=False, - help='Redirect stdout to /dev/null') -@click.option('--redirect-stderr', is_flag=True, default=False, - help='Redirect stderr to /dev/null') -@click.option('--stream', is_flag=True, defaults=False, - help='Input is a stream of JSON objects') +@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) +@click.option("--read-from", default="-", type=click.File("r")) +@click.option( + "-s", "--shell", is_flag=True, default=False, help="run command in a shell" +) +@click.option( + "-t", + "--template-engine", + default="python", + type=click.Choice(["python", "jinja2"]), + help="select the template engine to use", +) +@click.option( + "-v", "--verbose", is_flag=True, default=False, help="explain what is being done" +) +@click.option("-P", "--max-procs", default=1, type=click.IntRange(min=0, clamp=True)) +@click.option( + "-p", + "--use-predefined-template", + is_flag=True, + default=False, + help="use predefined template", +) +@click.option( + "--redirect-stdout", + is_flag=True, + default=False, + help="Redirect stdout to /dev/null", +) +@click.option( + "--redirect-stderr", + is_flag=True, + default=False, + help="Redirect stderr to /dev/null", +) +@click.option( + "--stream", is_flag=True, defaults=False, help="Input is a stream of JSON objects" +) @click.version_option() -@click.argument('command') +@click.argument("command") def targs( command: str, read_from: click.File, @@ -72,23 +93,23 @@ def targs( else: data = [json.loads(line) for line in read_from] except json.JSONDecodeError: - click.secho('JSON not valid!', fg='red', err=True) + click.secho("JSON not valid!", fg="red", err=True) raise SystemExit if use_predefined_template: try: - command, template_engine = config.load_template(command, 'targs') + command, template_engine = config.load_template(command, "targs") except FileNotFoundError: - click.secho('File not found!', fg='red', err=True) + click.secho("File not found!", fg="red", err=True) raise SystemExit except RuntimeError: - click.secho('Multiple files found!', fg='red', err=True) + click.secho("Multiple files found!", fg="red", err=True) raise SystemExit try: render = template.get_render_function(command, engine=template_engine) except RuntimeError: - click.secho('jinja2 not installed!', fg='red', err=True) + click.secho("jinja2 not installed!", fg="red", err=True) raise SystemExit max_procs = utils.max_procs() if max_procs == 0 else max_procs @@ -96,16 +117,10 @@ def targs( stdout = subprocess.DEVNULL if redirect_stdout else None stderr = subprocess.DEVNULL if redirect_stderr else None patched_subprocess = functools.partial( - subprocess.run, - shell=shell, - stdout=stdout, - stderr=stderr, + subprocess.run, shell=shell, stdout=stdout, stderr=stderr, ) patched_run_command = functools.partial( - run_command, - render=render, - run=patched_subprocess, - shell=shell, + run_command, render=render, run=patched_subprocess, shell=shell, ) if max_procs == 1: for r in data: @@ -115,5 +130,5 @@ def targs( p.map(patched_run_command, data) -if __name__ == '__main__': +if __name__ == "__main__": targs() |
