|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import typer |
| 5 | +from rich.console import Console |
| 6 | +from rich.markdown import Markdown |
| 7 | +from rich.prompt import Prompt |
| 8 | +from rich.syntax import Syntax |
| 9 | + |
| 10 | +from sourcery_rules_generator import expensive_loop |
| 11 | + |
| 12 | +app = typer.Typer(rich_markup_mode="markdown") |
| 13 | + |
| 14 | + |
| 15 | +DESCRIPTION_MARKDOWN = """ |
| 16 | +# Expensive Loop Template |
| 17 | +
|
| 18 | +With the "Expensive Loop" template, |
| 19 | +you can generate rules that ensure that an expensive operation isn't called in a loop. |
| 20 | +
|
| 21 | +For example: |
| 22 | +
|
| 23 | +* Call to an external API. |
| 24 | +* Complex calculations. |
| 25 | +
|
| 26 | +## Parameters for the "Expensive Loop" Template |
| 27 | +
|
| 28 | +1. The fully qualified name of the expensive function, that you want to avoid in loops. Required. |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +@app.command() |
| 33 | +def create( |
| 34 | + avoided_function_option: str = typer.Option( |
| 35 | + None, |
| 36 | + "--avoided-function", |
| 37 | + help="The function that should be avoided in loops.", |
| 38 | + ), |
| 39 | + interactive_flag: bool = typer.Option( |
| 40 | + True, |
| 41 | + "--interactive/--no-interactive", |
| 42 | + "--input/--no-input", |
| 43 | + help="Switch whether interactive prompts are shown. Use `--no-input` when you call this command from scripts.", |
| 44 | + ), |
| 45 | + plain: bool = typer.Option(False, help="Print only plain text."), |
| 46 | + quiet: bool = typer.Option( |
| 47 | + False, |
| 48 | + "--quiet", |
| 49 | + "-q", |
| 50 | + help='Display less info about the "Expensive Loop" template.', |
| 51 | + ), |
| 52 | +): |
| 53 | + """Create a new Sourcery Expensive Loop rule.""" |
| 54 | + interactive = sys.stdin.isatty() and interactive_flag |
| 55 | + stderr_console = Console(stderr=True) |
| 56 | + if interactive and not quiet: |
| 57 | + stderr_console.print(Markdown(DESCRIPTION_MARKDOWN)) |
| 58 | + stderr_console.rule() |
| 59 | + |
| 60 | + if interactive: |
| 61 | + stderr_console.print( |
| 62 | + Markdown('## Parameters for the "Expensive Loop" Template') |
| 63 | + ) |
| 64 | + |
| 65 | + function_name = ( |
| 66 | + avoided_function_option |
| 67 | + or interactive |
| 68 | + and Prompt.ask( |
| 69 | + "The fully qualified name of the expensive function (required)", |
| 70 | + console=stderr_console, |
| 71 | + ) |
| 72 | + ) |
| 73 | + if not function_name: |
| 74 | + _raise_error("No function name provided. Can't create Expensive Loop rule.") |
| 75 | + |
| 76 | + result = expensive_loop.create_yaml_rules(function_name) |
| 77 | + |
| 78 | + stderr_console.rule() |
| 79 | + stderr_console.print(Markdown("## Generated YAML Rules")) |
| 80 | + if plain: |
| 81 | + Console().print(result) |
| 82 | + else: |
| 83 | + Console().print(Syntax(result, "YAML")) |
| 84 | + |
| 85 | + |
| 86 | +def _raise_error(error_msg: str, code: int = 1) -> None: |
| 87 | + stderr_console = Console(stderr=True, style="bold red") |
| 88 | + stderr_console.print(error_msg) |
| 89 | + raise typer.Exit(code=code) |
0 commit comments