|
1 | 1 | from dataclasses import dataclass |
2 | | -from enum import Enum |
| 2 | +from enum import Enum, unique |
3 | 3 | from pathlib import Path |
4 | | -from typing import Any, Callable, List, Optional, TypeVar, cast |
| 4 | +from typing import IO, Any, AnyStr, Callable, Dict, List, Optional, TypeVar, Union, cast |
5 | 5 |
|
6 | 6 | import click |
7 | 7 | import pluggy |
8 | 8 |
|
9 | | -__all__ = ["hookimpl", "ClickCommonConfig", "pass_common_config"] |
| 9 | +from robotcode.core.dataclasses import as_json |
| 10 | + |
| 11 | +__all__ = ["hookimpl", "CommonConfig", "pass_application"] |
10 | 12 |
|
11 | 13 | F = TypeVar("F", bound=Callable[..., Any]) |
12 | 14 | hookimpl = cast(Callable[[F], F], pluggy.HookimplMarker("robotcode")) |
13 | 15 |
|
14 | 16 |
|
| 17 | +@unique |
15 | 18 | class ColoredOutput(str, Enum): |
16 | 19 | AUTO = "auto" |
17 | 20 | YES = "yes" |
18 | 21 | NO = "no" |
19 | 22 |
|
20 | 23 |
|
| 24 | +@unique |
| 25 | +class OutputFormat(str, Enum): |
| 26 | + TOML = "toml" |
| 27 | + JSON = "json" |
| 28 | + FLAT = "flat" |
| 29 | + |
| 30 | + def __str__(self) -> str: |
| 31 | + return self.value |
| 32 | + |
| 33 | + |
21 | 34 | @dataclass |
22 | | -class ClickCommonConfig: |
| 35 | +class CommonConfig: |
23 | 36 | config_file: Optional[Path] = None |
24 | 37 | profiles: Optional[List[str]] = None |
25 | 38 | dry: bool = False |
26 | 39 | verbose: bool = False |
27 | 40 | colored_output: ColoredOutput = ColoredOutput.AUTO |
28 | 41 |
|
29 | 42 |
|
30 | | -pass_common_config = click.make_pass_decorator(ClickCommonConfig, ensure=True) |
| 43 | +class Application: |
| 44 | + def __init__(self) -> None: |
| 45 | + self.config = CommonConfig() |
| 46 | + |
| 47 | + @property |
| 48 | + def colored(self) -> bool: |
| 49 | + return self.config.colored_output in [ColoredOutput.AUTO, ColoredOutput.YES] |
| 50 | + |
| 51 | + def verbose( |
| 52 | + self, |
| 53 | + message: Union[str, Callable[[], Any], None], |
| 54 | + file: Optional[IO[AnyStr]] = None, |
| 55 | + nl: bool = True, |
| 56 | + err: bool = False, |
| 57 | + ) -> None: |
| 58 | + if self.config.verbose: |
| 59 | + click.secho( |
| 60 | + message() if callable(message) else message, |
| 61 | + file=file, |
| 62 | + nl=nl, |
| 63 | + err=err, |
| 64 | + color=self.colored, |
| 65 | + fg="bright_black", |
| 66 | + ) |
| 67 | + |
| 68 | + def warning( |
| 69 | + self, |
| 70 | + message: Union[str, Callable[[], Any], None], |
| 71 | + file: Optional[IO[AnyStr]] = None, |
| 72 | + nl: bool = True, |
| 73 | + err: bool = False, |
| 74 | + ) -> None: |
| 75 | + click.secho( |
| 76 | + f"WARNING: {message() if callable(message) else message}", |
| 77 | + file=file, |
| 78 | + nl=nl, |
| 79 | + err=err, |
| 80 | + color=self.colored, |
| 81 | + fg="bright_yellow", |
| 82 | + ) |
| 83 | + |
| 84 | + def print_dict(self, config: Dict[str, Any], format: OutputFormat) -> None: |
| 85 | + text = None |
| 86 | + if format == "toml": |
| 87 | + try: |
| 88 | + import tomli_w |
| 89 | + |
| 90 | + text = tomli_w.dumps(config) |
| 91 | + except ImportError: |
| 92 | + self.warning("Package 'tomli_w' is required to use TOML output. Using JSON format instead.") |
| 93 | + format = OutputFormat.JSON |
| 94 | + |
| 95 | + if text is None: |
| 96 | + text = as_json(config, indent=True) |
| 97 | + |
| 98 | + if not text: |
| 99 | + return |
| 100 | + |
| 101 | + if self.colored: |
| 102 | + try: |
| 103 | + from rich.console import Console |
| 104 | + from rich.syntax import Syntax |
| 105 | + |
| 106 | + Console().print(Syntax(text, format, background_color="default")) |
| 107 | + |
| 108 | + return |
| 109 | + except ImportError: |
| 110 | + if self.config.colored_output == ColoredOutput.YES: |
| 111 | + self.warning('Package "rich" is required to use colored output.') |
| 112 | + |
| 113 | + click.echo(text) |
| 114 | + |
| 115 | + return |
| 116 | + |
| 117 | + |
| 118 | +pass_application = click.make_pass_decorator(Application, ensure=True) |
0 commit comments