Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19 add a save to file option for a report #22

Merged
merged 5 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
requirements-frozen.txt
!/tests/fixtures/files/requirements-frozen.txt
/docker-run-*
/report.html
30 changes: 30 additions & 0 deletions report_footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
</div>
</main>
<footer>
<div class="container">
<p>Made with <a href="https://github.com/wagtail-examples/dependency-checker">Dependency Checker</a></p>
</div>
</footer>
<script>
// set the radio buttons
const theme = localStorage.getItem('theme');
if (theme) {
// set the document theme
document.documentElement.setAttribute('data-theme', theme);
// set the radio button
document.querySelector(`input[data-theme="${theme}"]`).checked = true;
}
// add event listeners to the radio buttons
document.querySelectorAll('input[name="theme"]').forEach((input) => {
input.addEventListener('change', (event) => {
// get the theme from the radio button
const theme = event.target.getAttribute('data-theme');
// set the document theme
document.documentElement.setAttribute('data-theme', theme);
// set the theme in local storage
localStorage.setItem('theme', theme);
});
});
</script>
</body>
</html>
49 changes: 49 additions & 0 deletions report_header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dependency Checker</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.colors.min.css"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]" media="print">
<style>
@media print {
:root {
font-size: 60%;
}
body {
margin: 0;
padding: 0;
}
.container {
max-width: 100%;
}
#theme-switcher {
display: none !important;
}
}
</style>
</head>
<body>
<header>
<div class="container">
<div class="grid">
<div class="grid">
<h1>Dependency Checker Report</h1>
<fieldset class="grid" style="display: flex;">
<label>
<input type="radio" name="theme" data-theme="light" />
Light
</label>
<label>
<input type="radio" name="theme" data-theme="dark" />
Dark
</label>
</fieldset>
</div>
</div>
</div>
</header>
<hr>
<main>
44 changes: 43 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from src.managers.repository import RepositoryManager
from src.parsers.text import TextParser
from src.parsers.toml import TomlParser
from src.reporters.html import HTMLReporter

console = Console()
reporter = HTMLReporter()


@click.command()
Expand All @@ -21,7 +23,13 @@
prompt="Repository URL",
help="The URL of the repository to clone.",
)
def start(repo_url):
@click.option(
"--report",
"-r",
is_flag=True,
help="Generate a printable report.",
)
def start(repo_url, report):
console.clear()
console.print("Welcome to the Dependency Checker.", style="bright_white")
console.print(
Expand Down Expand Up @@ -130,6 +138,17 @@ def start(repo_url):
table.add_row("Docker Image", repository_manager.docker_image)
console.print(table)

if report:
reporter.add_info_data(
{
"Repository URL": repository_manager.repo_url,
"Branch Name": repository_manager.get_branch(),
"Dockerfile Path": repository_manager.dockerfile_path,
"Poetry Version": repository_manager.poetry_version,
"Docker Image": repository_manager.docker_image,
}
)

console.print("Analyzing the dependencies ...", style="cyan1")

# process the requirements-frozen.txt file as a FrozenParser object
Expand Down Expand Up @@ -191,6 +210,16 @@ def start(repo_url):

table.add_row(name, frozen_version, latest_version, status, style=style)

if report:
reporter.add_production_data(
{
"Package": name,
"Installed Version": frozen_version,
"Latest Version": latest_version,
"Status": status,
}
)

console.print(table)

# development dependencies
Expand Down Expand Up @@ -234,4 +263,17 @@ def start(repo_url):

table.add_row(name, frozen_version, latest_version, status, style=style)

if report:
reporter.add_development_data(
{
"Package": name,
"Installed Version": frozen_version,
"Latest Version": latest_version,
"Status": status,
}
)

console.print(table)

if report:
reporter.write_report()
88 changes: 88 additions & 0 deletions src/reporters/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from dataclasses import dataclass


@dataclass
class HTMLReporter:
report_header: str = ""
report_footer: str = ""

report_info_section: str = ""
report_production_section: str = ""
report_development_section: str = ""

def __post_init__(self):
with open("report_header.html") as f:
self.report_header = f.read()

with open("report_footer.html") as f:
self.report_footer = f.read()

self.info_data = {}
self.production_data = []
self.development_data = []

self.report = ""

def add_info_data(self, data: dict) -> None:
self.info_data = data

def add_production_data(self, data: dict) -> None:
self.production_data.append(data)

def add_development_data(self, data: dict) -> None:
self.development_data.append(data)

def write_report(self):
self.report += self.report_header

info_section = "<h2>Info</h2>\n"
info_section += "<table class='striped'>\n"
for key, value in self.info_data.items():
info_section += f"<tr><th>{key}</th><td>{value}</td></tr>"
info_section += "</table>"
self.report += "<section><div class='container'>" + info_section + "</div></section>"

self.report += "<hr>"

production_section = "<h2>Production Dependencies</h2>"
production_section += "<table class='striped'>"
production_section += (
"<thead><tr><th>Package</th><th>Installed Version</th><th>Latest Version</th><th>Status</th></tr></thead>"
)
for data in self.production_data:
status_class = "pico-color-red-500"
if data["Status"] == "Check":
status_class = "pico-color-cyan-500"
elif data["Status"] == "Outdated":
status_class = "pico-color-orange-500"
elif data["Status"] == "OK":
status_class = "pico-color-green-500"
production_section += f"<tr><td><span class='{status_class}'>{data['Package']}</span></td><td>{data['Installed Version']}</td><td>{data['Latest Version']}</td><td><span class='{status_class}'>{data['Status']}</span></td></tr>" # noqa
production_section += "</table>"
self.report += "<section><div class='container'>" + production_section + "</div></section>"

self.report += "<hr>"

development_section = "<h2>Development Dependencies</h2>"
development_section += "<table class='striped'>"
development_section += (
"<thead><tr><th>Package</th><th>Installed Version</th><th>Latest Version</th><th>Status</th></tr></thead>"
)
for data in self.development_data:
status_class = "pico-color-red-500"
if data["Status"] == "Check":
status_class = "pico-color-cyan-500"
elif data["Status"] == "Outdated":
status_class = "pico-color-orange-500"
elif data["Status"] == "OK":
status_class = "pico-color-green-500"
development_section += f"<tr><td><span class='{status_class}'>{data['Package']}</span></td><td>{data['Installed Version']}</td><td>{data['Latest Version']}</td><td><span class='{status_class}'>{data['Status']}</span></td></tr>" # noqa
development_section += "</table>"
self.report += "<section><div class='container'>" + development_section + "</div></section>"

self.report += "<hr>"

self.report += self.report_footer

with open("report.html", "w") as f:
f.write(self.report)
Loading