Coverage for kysy/cli.py: 72.22%
52 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 19:30:32 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 19:30:32 +00:00
1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3# pylint: disable=line-too-long
4"""Commandline API gateway for kysy."""
5import pathlib
6import sys
7from typing import List, Union
9import typer
11import kysy
12import kysy.kysy as ky
14# from kysy.render import Template # type: ignore
17class Template:
18 NONE = ''
19 JINJA_STRING = ''
20 F_STRING = ''
21 JINJA_PATH = ''
22 STRING = ''
25CWD = '.'
26APP_NAME = 'Ask or know (kysy tai tiedä).'
27APP_ALIAS = 'kysy'
28app = typer.Typer(
29 add_completion=False,
30 context_settings={'help_option_names': ['-h', '--help']},
31 no_args_is_help=True,
32)
35@app.callback(invoke_without_command=True)
36def callback(
37 version: bool = typer.Option(
38 False,
39 '-V',
40 '--version',
41 help='Display the kysy version and exit',
42 is_eager=True,
43 )
44) -> None:
45 """
46 Ask or know (kysy tai tiedä).
47 Relating answers to files and folders (Finnish: Vastaukset tiedostoihin ja kansioihin).
48 """
49 if version:
50 typer.echo(f'{APP_NAME} version {kysy.__version__}')
51 raise typer.Exit()
54def classify_template(template: str) -> str: # TODO: Template
55 """
56 Process the received template and return the classification.
57 """
58 if not template:
59 return Template.NONE
61 if '{{' in str(template):
62 return Template.JINJA_STRING
64 if '{' in str(template):
65 return Template.F_STRING
67 if pathlib.Path(str(template)).is_file():
68 return Template.JINJA_PATH
70 return Template.STRING
73@app.command('diff')
74def diff_repo(
75 source: str = typer.Argument(CWD),
76 input: str = typer.Option('', '-i', '--input'),
77 output: str = typer.Option('', '-o', '--output'),
78 template: str = typer.Option('', '-t', '--template'),
79) -> int:
80 """
81 Match the local data to the remote information (no template handling yet).
82 """
83 repo_root = input if input else source
84 target = 'STD_OUT' if not str(output) else str(output)
85 return ky.main(['diff', repo_root, target, template])
88@app.command('label')
89def label_repo(
90 source: str = typer.Argument(CWD),
91 input: str = typer.Option('', '-i', '--input'),
92 output: str = typer.Option('', '-o', '--output'),
93 template: str = typer.Option('', '-t', '--template'),
94) -> int:
95 """
96 Labels the local data (no template handling yet).
97 """
98 repo_root = input if input else source
99 target = 'STD_OUT' if not str(output) else str(output)
100 return ky.main(['label', repo_root, target, template])
103@app.command('template')
104def app_template(output: str = typer.Option('', '-o', '--output')) -> None:
105 """
106 Output an example jinja template representing the defaults.
107 """
108 target = 'STD_OUT' if not output else output
109 typer.echo(f'Example template generated per {target}')
112@app.command('version')
113def app_version() -> None:
114 """
115 Display the kysy version and exit.
116 """
117 callback(True)
120# pylint: disable=expression-not-assigned
121# @app.command()
122def main(argv: Union[List[str], None] = None) -> int:
123 """Delegate processing to functional module."""
124 argv = sys.argv[1:] if argv is None else argv
125 return ky.main(argv)