Skip to content

Commit

Permalink
feat(back): add output to XML file option
Browse files Browse the repository at this point in the history
- Add the option to output the results to a XML file.
- Update the README.md to include the new option.
- Update the README.md to include the -m 0 option to
disable the exit with error.
  • Loading branch information
rohaquinlop committed Feb 11, 2024
1 parent bc8ceed commit b376d69
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ To run **complexipy** you can use the following command:
<b>complexipy</b> path/to/directory # Use complexipy to analyze a specific directory and any subdirectories
<b>complexipy</b> path/to/file.py # Use complexipy to analyze a specific file
<b>complexipy</b> path/to/file.py -m 20 # Use the -m option to set the maximum congnitive complexity, default is 15
<b>complexipy</b> path/to/directory -m 0 # Set the maximum cognitive complexity to 0 to disable the exit with error
<b>complexipy</b> path/to/directory -o # Use the -o option to output the results to a XML file, default is False
</pre>

If the cognitive complexity of a file is greater than the maximum cognitive, then
the return code will be 1 and exit with error, otherwise it will be 0.

For example, given the following file:

```python
Expand Down
31 changes: 28 additions & 3 deletions complexipy/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from pathlib import Path
from complexipy import rust
import os
from rich.console import Console
from rich.table import Table
import time
import toml
import typer
import xml.etree.ElementTree as ET
import xml.dom.minidom

root_dir = Path(__file__).resolve().parent.parent
app = typer.Typer(name="complexipy")
Expand All @@ -16,6 +19,7 @@
def main(
path: str,
max_complexity: int = typer.Option(15, "--max-complexity", "-c", help="The maximum complexity allowed, set this value as 0 to set it as unlimited."),
output: bool = typer.Option(False, "--output", "-o", help="Output the results to a XML file."),
):
has_success = True
is_dir = Path(path).is_dir()
Expand All @@ -26,16 +30,37 @@ def main(
files = rust.main(path, is_dir, max_complexity)
execution_time = time.time() - start_time
console.print("Analysis completed! :tada:")

# Output to XML
if output:
invocation_path = os.getcwd()
xml_file = ET.Element("complexity")
for file in files:
file_xml = ET.SubElement(xml_file, "file")
name = ET.SubElement(file_xml, "name")
name.text = file.file_name
file_path = ET.SubElement(file_xml, "path")
file_path.text = file.path
file_complexity = ET.SubElement(file_xml, "complexity")
file_complexity.text = str(file.complexity)
xml_str = ET.tostring(xml_file, encoding="utf-8")
dom = xml.dom.minidom.parseString(xml_str)
pretty_xml = dom.toprettyxml(indent=" ")
with open(f"{invocation_path}/complexipy.xml", "w") as file:
file.write(pretty_xml)
console.print(f"Results saved to {invocation_path}/complexipy.xml")

# Summary
table = Table(title="Summary", show_header=True, header_style="bold magenta", show_lines=True)
table.add_column("Path")
table.add_column("File")
table.add_column("Cognitive Complexity")
table.add_column("Complexity")
for file in files:
if file.complexity > max_complexity and max_complexity != 0:
table.add_row(f"[green]{file.file_name}[/green]", f"[red]{file.complexity}[/red]")
table.add_row(f"{file.path}", f"[green]{file.file_name}[/green]", f"[red]{file.complexity}[/red]")
has_success = False
else:
table.add_row(f"[green]{file.file_name}[/green]", f"[blue]{file.complexity}[/blue]")
table.add_row(f"{file.path}", f"[green]{file.file_name}[/green]", f"[blue]{file.complexity}[/blue]")
console.print(table)
console.print(f"{len(files)} files analyzed in {execution_time:.4f} seconds")

Expand Down

0 comments on commit b376d69

Please sign in to comment.