Skip to content

Commit 8ffa8a0

Browse files
committed
refactor(graphics): inherit from core.graphics.Graphics to avoid repetition
1 parent 7e7c3ec commit 8ffa8a0

File tree

1 file changed

+12
-70
lines changed

1 file changed

+12
-70
lines changed

codesectools/sasts/all/graphics.py

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
"""Provides classes for generating plots and visualizations from aggregated SAST results."""
22

3-
import shutil
4-
import tempfile
5-
6-
import matplotlib
73
import matplotlib.pyplot as plt
8-
import typer
94
from matplotlib.figure import Figure
10-
from rich import print
115

126
from codesectools.sasts.all.sast import AllSAST
7+
from codesectools.sasts.core.graphics import Graphics as CoreGraphics
138
from codesectools.utils import shorten_path
149

15-
## Matplotlib config
16-
matplotlib.rcParams.update(
17-
{
18-
"font.family": "serif",
19-
"font.size": 11,
20-
}
21-
)
2210

11+
class Graphics(CoreGraphics):
12+
"""Base class for generating plots for aggregated SAST results.
13+
14+
Attributes:
15+
project_name (str): The name of the project being visualized.
16+
all_sast (AllSAST): The instance managing all SAST tools.
17+
output_dir (Path): The directory containing the aggregated results.
18+
color_mapping (dict): A dictionary mapping SAST tool names to colors.
19+
sast_names (list[str]): A list of names of the SAST tools involved in the analysis.
20+
plot_functions (list): A list of methods responsible for generating plots.
2321
24-
class Graphics:
25-
"""Base class for generating graphics from aggregated SAST results."""
22+
"""
2623

2724
def __init__(self, project_name: str) -> None:
2825
"""Initialize the Graphics object."""
@@ -38,61 +35,6 @@ def __init__(self, project_name: str) -> None:
3835
self.sast_names.append(sast.name)
3936
self.plot_functions = []
4037

41-
# Plot options
42-
self.limit = 10
43-
44-
self.has_latex = shutil.which("pdflatex")
45-
if self.has_latex:
46-
matplotlib.use("pgf")
47-
matplotlib.rcParams.update(
48-
{
49-
"pgf.texsystem": "pdflatex",
50-
"text.usetex": True,
51-
"pgf.rcfonts": False,
52-
}
53-
)
54-
else:
55-
print("pdflatex not found, pgf will not be generated")
56-
57-
def export(self, overwrite: bool, pgf: bool, show: bool) -> None:
58-
"""Generate, save, and optionally display all registered plots.
59-
60-
Args:
61-
overwrite: If True, overwrite existing figure files.
62-
pgf: If True and LaTeX is available, export figures in PGF format.
63-
show: If True, open the generated figures using the default viewer.
64-
65-
"""
66-
for plot_function in self.plot_functions:
67-
fig = plot_function()
68-
fig_name = plot_function.__name__.replace("plot_", "")
69-
fig.set_size_inches(12, 7)
70-
71-
if show:
72-
with tempfile.NamedTemporaryFile(delete=True) as temp:
73-
fig.savefig(f"{temp.name}.png", bbox_inches="tight")
74-
typer.launch(f"{temp.name}.png", wait=False)
75-
76-
figure_dir = self.output_dir / "_figures"
77-
figure_dir.mkdir(exist_ok=True, parents=True)
78-
figure_path = figure_dir / f"{fig_name}.png"
79-
if figure_path.is_file() and not overwrite:
80-
if not typer.confirm(
81-
f"Found existing figure at {figure_path}, would you like to overwrite?"
82-
):
83-
print(f"Figure {fig_name} not saved")
84-
continue
85-
86-
fig.savefig(figure_path, bbox_inches="tight")
87-
print(f"Figure {fig_name} saved at {figure_path}")
88-
89-
if pgf and self.has_latex:
90-
figure_path_pgf = figure_dir / f"{fig_name}.pgf"
91-
fig.savefig(figure_path_pgf, bbox_inches="tight")
92-
print(f"Figure {fig_name} exported to pgf")
93-
94-
plt.close(fig)
95-
9638

9739
## Single project
9840
class ProjectGraphics(Graphics):

0 commit comments

Comments
 (0)