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

Dates on output files #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
default_stages: [commit, push]
default_stages: [pre-commit, pre-push]
default_language_version:
python: python3.12.4
python: python3.13
repos:
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args: ["."]
- repo: https://github.com/psf/black
rev: 24.4.2
rev: 24.10.0
hooks:
- id: black
args: [
--line-length=120
]
types: ['python']
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
rev: 7.1.1
hooks:
- id: flake8
args: [
Expand All @@ -41,7 +41,7 @@ repos:
--convention=google
]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
rev: 1.7.10
hooks:
- id: bandit
args: [
Expand Down
7 changes: 6 additions & 1 deletion export_report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ def iterate_export_formats(self) -> None:
print(f"Skipping {export_format['export_type']} for {export_format['output_filename']}")
continue

print(f"Exporting {export_format['export_type']} to {export_format['output_filename']}")
print(f"Exporting {export_format['export_type']} to {self.config['output_report_name']}")

# Perform string subs for output filename (include output_report_name in all files)
export_format["output_filename"] = export_format["output_filename"].replace(
"[output_report_name]", self.config["output_report_name"]
)

# Check if export_format["input_filename"] exists
if "input_filename" in export_format:
Expand Down
24 changes: 12 additions & 12 deletions export_report_data_config.json
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
{
"input_results_folder_name": "source_folder",
"output_report_name": "09-2024",
"input_results_folder_name": "results_folder_name_here",
"output_report_name": "2024-09",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Callum, this is a minor twist in the code.
What I was considering was something along the lines of:

from datetime import date
today = str(date.today())

and plug today in to "output_report_name"

Since import json package is already initialised
all we have to do is change the value of "output_report_name" data key

data["output_report_name"] = today

This way, its all automated and won't need manual intervention on the config file whenever we run a monthly CWAC scan.

"export_formats": [
{
"enabled": true,
"export_type": "generate_axe_core_template_aware_file",
"input_filename": "axe_core_audit.csv",
"output_filename": "axe_core_audit_template_aware.csv"
"output_filename": "[output_report_name]_axe_core_audit_template_aware.csv"
},
{
"enabled": true,
"export_type": "axe_core_template_aware_leaderboard",
"input_filename": "axe_core_audit.csv",
"output_filename": "axe_core_audit_leaderboard.csv"
"output_filename": "[output_report_name]_axe_core_audit_leaderboard.csv"
},
{
"enabled": true,
"export_type": "leaderboard",
"input_filename": "focus_indicator_audit.csv",
"output_filename": "focus_indicator_audit_leaderboard.csv",
"output_filename": "[output_report_name]_focus_indicator_audit_leaderboard.csv",
"query": "SELECT organisation, base_url, SUM(count) as count FROM cwac_table GROUP BY base_url ORDER BY count DESC"
},
{
"enabled": true,
"export_type": "leaderboard",
"input_filename": "reflow_audit.csv",
"output_filename": "reflow_audit_leaderboard.csv",
"output_filename": "[output_report_name]_reflow_audit_leaderboard.csv",
"query": "SELECT organisation, base_url, AVG(overflow_amount_px) AS overflow_amount_px FROM cwac_table GROUP BY base_url ORDER BY overflow_amount_px DESC;"
},
{
"enabled": true,
"export_type": "leaderboard",
"input_filename": "language_audit.csv",
"output_filename": "language_audit_leaderboard.csv",
"output_filename": "[output_report_name]_language_audit_leaderboard.csv",
"query": "SELECT organisation, base_url, AVG(smog_gl) AS smog_grade_level FROM cwac_table GROUP BY base_url ORDER BY smog_grade_level DESC"
},
{
"enabled": true,
"export_type": "raw_data",
"input_filename": "axe_core_audit.csv",
"output_filename": "axe_core_audit.csv"
"output_filename": "[output_report_name]_axe_core_audit.csv"
},
{
"enabled": true,
"export_type": "raw_data",
"input_filename": "axe_core_audit_template_aware.csv",
"output_filename": "axe_core_audit_template_aware.csv"
"output_filename": "[output_report_name]_axe_core_audit_template_aware.csv"
},
{
"enabled": true,
"export_type": "raw_data",
"input_filename": "focus_indicator_audit.csv",
"output_filename": "focus_indicator_audit.csv"
"output_filename": "[output_report_name]_focus_indicator_audit.csv"
},
{
"enabled": true,
"export_type": "raw_data",
"input_filename": "reflow_audit.csv",
"output_filename": "reflow_auditt.csv"
"output_filename": "[output_report_name]_reflow_auditt.csv"
},
{
"enabled": true,
"export_type": "raw_data",
"input_filename": "language_audit.csv",
"output_filename": "language_audit.csv"
"output_filename": "[output_report_name]_language_audit.csv"
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"postinstall": "npx @puppeteer/browsers install chrome@122.0.6261.39"
},
"dependencies": {
"axe-core": "^4.10.0",
"axe-core": "^4.10.2",
"@mozilla/readability": "^0.5.0"
}
}
20 changes: 9 additions & 11 deletions src/audit_plugins/element_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
"""

import logging
import selenium.common.exceptions as sel_exceptions
from typing import Any, Union

from typing import Any, Union, List
from bs4 import BeautifulSoup

from config import config
from src.audit_manager import AuditManager
from src.audit_plugins.default_audit import DefaultAudit
from src.browser import Browser
from bs4 import BeautifulSoup


class ElementAudit:
"""element audit."""

# pylint: disable=too-many-instance-attributes

audit_type = "ElementAudit"

def __init__(self, browser: Browser, **kwargs: Any) -> None:
Expand All @@ -39,21 +40,20 @@ def run(self) -> Union[list[Any], bool]:
list[Any]: rows of test data
bool: False if test fails, else a list of results
"""

# Scrape the page source of the loaded browser
try:
page_source = self.browser.get_page_source()
except Exception as exc:
logging.error(f"Error getting page source: {exc}")
logging.error("Error getting page source: %s", exc)
return False

# Try to parse using BeautifulSoup
try:
soup = BeautifulSoup(page_source, "lxml")
except Exception as exc:
logging.error(f"Error parsing page source: {exc}")
logging.error("Error parsing page source: %s", exc)
return False

# Find all elements of the target type (css selector)
elements = soup.select(self.target_element)

Expand All @@ -62,9 +62,7 @@ def run(self) -> Union[list[Any], bool]:
# For each element found, create a row of data
for element in elements:
# get element outer html
element_data = {
"element_html": element.prettify()
}
element_data = {"element_html": element.prettify()}
found_elements.append(element_data)

# Get page information from DefaultAudit
Expand Down
3 changes: 2 additions & 1 deletion src/audit_plugins/reflow_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def run(self) -> Union[list[dict[Any, Any]], bool]:

# Run a ScreenshotAudit if the page overflows
if config.audit_plugins["reflow_audit"]["screenshot_failures"] and overflow_amount > 0:
from src.audit_plugins.screenshot_audit import ScreenshotAudit # pylint: disable=import-outside-toplevel
from src.audit_plugins.screenshot_audit import \
ScreenshotAudit # pylint: disable=import-outside-toplevel

screenshot_audit = ScreenshotAudit(
browser=self.browser,
Expand Down