-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourcery_analytics_cli_partials.py
147 lines (123 loc) · 4.86 KB
/
sourcery_analytics_cli_partials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Parts of larger commands."""
# This file is a copy from `sourcery_analytics.cli.partials` from the
# `sourcery_analytics` project.
#
# At the time of writing, this file violates the following rules:
# - `require-parameter-annotation`
# - `require-return-annotation`
#
# According to the Google Python Style Guide, all public functions must have their
# parameter and return types annotated, which is not the case here.
#
# File extracted from:
# https://github.com/sourcery-ai/sourcery-analytics/blob/d605829bbc0e3ae84decf21998c26df8980c66b9/sourcery_analytics/cli/partials.py
#
# No changes were introduced apart from this comment block.
import operator
import pathlib
import typing
import pydantic
import typer
import rich.progress
import rich.table
import rich.console
from sourcery_analytics import analyze
from sourcery_analytics.metrics.compounders import NamedMetricResult
from sourcery_analytics.settings import Settings
def analyze_rich_output(method_metric, methods, metrics, sort):
"""Performs analysis and displays results in a rich-formatted table."""
console = rich.console.Console()
methods_progress = rich.progress.track(methods, description="Analyzing methods...")
analysis: typing.List[NamedMetricResult] = sorted(
analyze(methods_progress, metrics=metrics),
key=operator.itemgetter(sort.method_method_name),
reverse=True,
)
console.print("[bold green]Analysis Complete")
table = rich.table.Table()
table.add_column("Method")
for metric_choice in method_metric:
table.add_column(metric_choice.value, justify="right")
for metric in analysis:
table.add_row(
*(
f"{value}" if isinstance(value, (float, int)) else str(value)
for _sub_metric_name, value in metric
)
)
console.print(table)
raise typer.Exit()
def analyze_plain_output(methods, metrics, sort):
"""Performs analysis and displays the python object's representation."""
analysis = sorted(
analyze(methods, metrics=metrics),
key=operator.itemgetter(sort.method_method_name),
reverse=True,
)
typer.echo(analysis)
def analyze_csv_output(method_metric, methods, metrics, sort):
"""Performs analysis and displays the results in CSV format."""
analysis = sorted(
analyze(methods, metrics=metrics),
key=operator.itemgetter(sort.method_method_name),
reverse=True,
)
result = ""
result += (
"qualname,"
+ ",".join([str(metric_choice.value) for metric_choice in method_metric])
+ "\n"
)
for metric in analysis:
result += ",".join([str(value) for _sub_metric_name, value in metric]) + "\n"
typer.echo(result)
def aggregate_rich_output(aggregation, aggregation_method, methods, metrics) -> None:
"""Analyse methods, and aggregates results.
Displays the results in a rich-formatted table.
"""
console = rich.console.Console()
methods_progress = rich.progress.track(methods, description="Analyzing methods...")
result = analyze(methods_progress, metrics=metrics, aggregation=aggregation_method)
table = rich.table.Table()
table.add_column("Metric")
table.add_column(f"{aggregation.value.title()} Value", justify="right")
for metric_name, metric_value in result:
table.add_row(metric_name, str(metric_value))
console.print(table)
def aggregate_plain_output(aggregation_method, methods, metrics) -> None:
"""Analyse methods, and aggregates results.
Displays the python representation of the results.
"""
result = analyze(methods, metrics=metrics, aggregation=aggregation_method)
typer.echo(result)
def aggregate_csv_output(aggregation_method, method_metric, methods, metrics) -> None:
"""Analyse methods, and aggregates results.
Displays the results in CSV format.
"""
analysis = analyze(methods, metrics=metrics, aggregation=aggregation_method)
result = ",".join([m.value for m in method_metric]) + "\n"
result += ",".join(str(value) for _metric_name, value in analysis)
typer.echo(result)
def read_settings(
settings_file: pathlib.Path, console: rich.console.Console
) -> Settings:
"""Loads settings in the CLI.
Wraps the basic settings loader in order to print relevant error messages and
exit with correct codes.
"""
if not settings_file.exists():
console.print(
f"[yellow]Warning:[/] could not find settings file "
f"[bold]{settings_file}[/], using defaults."
)
settings = Settings()
else:
try:
settings = Settings.from_toml_file(settings_file)
except pydantic.ValidationError as exc:
console.print(
f"[bold red]Error:[/] unable to parse settings file "
f"[bold]{settings_file}[/]."
)
raise typer.Exit(2) from exc
return settings