Coverage for revjo/_cli.py: 93%
45 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:25 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-05 14:25 +0000
1import functools
2import json
3import sys
4from logging import DEBUG, INFO, basicConfig, getLogger
6import click
8from .revjo import convert
9from .version import VERSION
11log = getLogger(__name__)
14@click.version_option(version=VERSION, prog_name="revjo")
15@click.group(invoke_without_command=True)
16@click.pass_context
17def cli(ctx):
18 if ctx.invoked_subcommand is None: 18 ↛ 19line 18 didn't jump to line 19 because the condition on line 18 was never true
19 print(ctx.get_help())
22def set_verbose(flag):
23 fmt = "%(asctime)s %(levelname)s %(message)s"
24 if flag:
25 basicConfig(level=DEBUG, format=fmt)
26 else:
27 basicConfig(level=INFO, format=fmt)
30_cli_option = [
31 click.option("--verbose/--no-verbose"),
32]
35def multi_options(decs):
36 def deco(f):
37 for dec in reversed(decs):
38 f = dec(f)
39 return f
41 return deco
44def cli_option(func):
45 @functools.wraps(func)
46 def wrap(verbose, *args, **kwargs):
47 set_verbose(verbose)
48 return func(*args, **kwargs)
50 return multi_options(_cli_option)(wrap)
53@cli.command("convert")
54@cli_option
55@click.option("--input", type=click.File("r"))
56@click.argument("data", type=str, default="")
57def do_convert(input, data):
58 if input is not None:
59 d = json.load(input)
60 elif data != "":
61 d = json.loads(data)
62 else:
63 d = json.load(sys.stdin)
64 click.echo(convert(d))
67if __name__ == "__main__": 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true
68 cli()