Skip to content

Commit

Permalink
Merge pull request #26 from rohaquinlop/issue-25
Browse files Browse the repository at this point in the history
feat(build|docs): #25 add quiet option
  • Loading branch information
rohaquinlop authored Mar 7, 2024
2 parents 2648acf + 49b6afa commit 21f23c6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ complexipy path/to/directory -c 0 # Set the maximum cognitive complexity to 0
complexipy path/to/directory -o # Use the -o option to output the results to a CSV file, default is False
complexipy path/to/directory -d low # Use the -d option to set detail level, default is "normal". If set to "low" will show only files with complexity greater than the maximum complexity
complexipy path/to/directory -l file # Use the -l option to set the level of measurement, default is "function". If set to "file" will measure the complexity of the file and will validate the maximum complexity according to the file complexity.
complexipy path/to/directory -q # Use the -q option to disable the output to the console, default is False.
```

### Options
Expand All @@ -90,6 +91,7 @@ complexipy path/to/directory -l file # Use the -l option to set the level of mea
according to the function complexity. This option is useful if you want to set
a maximum complexity according for each file or for each function in the file
(or files).
- `-q` or `--quiet`: Disable the output to the console, default is False.

If the cognitive complexity of a file or a function is greater than the maximum
cognitive cognitive complexity, then the return code will be 1 and exit with
Expand Down
31 changes: 14 additions & 17 deletions complexipy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
Level,
)
from .utils import (
create_table_file_level,
create_table_function_level,
output_summary,
has_success_file_level,
has_success_function_level,
)
from complexipy import (
rust,
Expand All @@ -17,9 +18,6 @@
Path,
)
import re
from rich.align import (
Align,
)
from rich.console import (
Console,
)
Expand Down Expand Up @@ -58,6 +56,9 @@ def main(
"-l",
help="Specify the level of measurement, it can be 'function' or 'file'. Default is 'function'.",
),
quiet: bool = typer.Option(
False, "--quiet", "-q", help="Suppress the output to the console."
),
):
is_dir = Path(path).is_dir()
_url_pattern = (
Expand All @@ -83,19 +84,15 @@ def main(
console.print(f"Results saved in {output_csv_path}")

# Summary
if file_level:
table, has_success, total_complexity = create_table_file_level(
files, max_complexity, details
)
else:
table, has_success, total_complexity = create_table_function_level(
files, max_complexity, details
if not quiet:
has_success = output_summary(
console, file_level, files, max_complexity, details, path, execution_time
)
console.print(Align.center(table))
console.print(f":brain: Total Cognitive Complexity in {path}: {total_complexity}")
console.print(
f"{len(files)} file{'s' if len(files)> 1 else ''} analyzed in {execution_time:.4f} seconds"
)
if quiet and not file_level:
has_success = has_success_function_level(files, max_complexity)
if quiet and file_level:
has_success = has_success_file_level(files, max_complexity)

console.rule(":tada: Analysis completed! :tada:")

if not has_success:
Expand Down
47 changes: 47 additions & 0 deletions complexipy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,41 @@
from complexipy.rust import (
FileComplexity,
)
from rich.align import (
Align,
)
from rich.console import (
Console,
)
from rich.table import Table


def output_summary(
console: Console,
file_level: bool,
files: list[FileComplexity],
max_complexity: int,
details: DetailTypes,
path: str,
execution_time: float,
) -> bool:
if file_level:
table, has_success, total_complexity = create_table_file_level(
files, max_complexity, details
)
else:
table, has_success, total_complexity = create_table_function_level(
files, max_complexity, details
)
console.print(Align.center(table))
console.print(f":brain: Total Cognitive Complexity in {path}: {total_complexity}")
console.print(
f"{len(files)} file{'s' if len(files)> 1 else ''} analyzed in {execution_time:.4f} seconds"
)

return has_success


def create_table_file_level(
files: list[FileComplexity], max_complexity: int, details: DetailTypes
) -> tuple[Table, bool, int]:
Expand Down Expand Up @@ -70,3 +102,18 @@ def create_table_function_level(
f"[blue]{function.complexity}[/blue]",
)
return table, has_success, total_complexity


def has_success_file_level(files: list[FileComplexity], max_complexity: int) -> bool:
for file in files:
if file.complexity > max_complexity and max_complexity != 0:
return False
return True


def has_success_function_level(files: list[FileComplexity], complexity: int) -> bool:
for file in files:
for function in file.functions:
if function.complexity > complexity and complexity != 0:
return False
return True
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ complexipy path/to/directory -c 0 # Set the maximum cognitive complexity to 0
complexipy path/to/directory -o # Use the -o option to output the results to a CSV file, default is False
complexipy path/to/directory -d low # Use the -d option to set detail level, default is "normal". If set to "low" will show only files with complexity greater than the maximum complexity
complexipy path/to/directory -l file # Use the -l option to set the level of measurement, default is "function". If set to "file" will measure the complexity of the file and will validate the maximum complexity according to the file complexity.
complexipy path/to/directory -q # Use the -q option to disable the output to the console, default is False.
```

### Options
Expand All @@ -90,6 +91,7 @@ complexipy path/to/directory -l file # Use the -l option to set the level of mea
according to the function complexity. This option is useful if you want to set
a maximum complexity according for each file or for each function in the file
(or files).
- `-q` or `--quiet`: Disable the output to the console, default is False.

If the cognitive complexity of a file or a function is greater than the maximum
cognitive cognitive complexity, then the return code will be 1 and exit with
Expand Down

0 comments on commit 21f23c6

Please sign in to comment.