Skip to content

Commit

Permalink
chore: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot committed Jan 14, 2025
1 parent 1fa7dfa commit 1391103
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 125 deletions.
2 changes: 1 addition & 1 deletion bases/ecoindex/backend/routers/bff.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ async def get_latest_result_redirect(
)

return RedirectResponse(
url=f"{Settings().FRONTEND_BASE_URL}/resultat/?id={latest_result.latest_result.id}"
url=f"{Settings().FRONTEND_BASE_URL}/resultat/?id={latest_result.latest_result.id}" # type: ignore
)
5 changes: 3 additions & 2 deletions bases/ecoindex/backend/routers/ecoindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ async def get_ecoindex_analysis_list(
page=pagination.page,
size=pagination.size,
sort_params=await get_sort_parameters(
query_params=sort, model=ApiEcoindex
), # type: ignore
query_params=sort,
model=ApiEcoindex, # type: ignore
),
)
total_results = await get_count_analysis_db(
session=session,
Expand Down
22 changes: 14 additions & 8 deletions bases/ecoindex/backend/routers/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from json import loads
from typing import Annotated

import requests
from celery.result import AsyncResult
Expand Down Expand Up @@ -37,18 +38,23 @@
)
async def add_ecoindex_analysis_task(
response: Response,
web_page: WebPage = Body(
default=...,
title="Web page to analyze defined by its url and its screen resolution",
example=WebPage(url="https://www.ecoindex.fr", width=1920, height=1080),
),
web_page: Annotated[
WebPage,
Body(
default=...,
title="Web page to analyze defined by its url and its screen resolution",
example=WebPage(url="https://www.ecoindex.fr", width=1920, height=1080),
),
],
session: AsyncSession = Depends(get_session),
) -> str:
if Settings().DAILY_LIMIT_PER_HOST:
remaining_quota = await check_quota(
session=session, host=web_page.get_url_host()
)
response.headers["X-Remaining-Daily-Requests"] = str(remaining_quota - 1)

if remaining_quota:
response.headers["X-Remaining-Daily-Requests"] = str(remaining_quota - 1)

if (
Settings().EXCLUDED_HOSTS
Expand All @@ -69,7 +75,7 @@ async def add_ecoindex_analysis_task(
detail=f"The URL {web_page.url} is unreachable. Are you really sure of this url? 🤔",
)

task_result = ecoindex_task.delay(
task_result = ecoindex_task.delay( # type: ignore
url=str(web_page.url), width=web_page.width, height=web_page.height
)

Expand All @@ -93,7 +99,7 @@ async def get_ecoindex_analysis_task_by_id(
t = AsyncResult(id=str(id), app=task_app)

task_response = QueueTaskApi(
id=t.id,
id=str(t.id),
status=t.state,
)

Expand Down
9 changes: 5 additions & 4 deletions bases/ecoindex/backend/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def format_exception_response(exception: Exception) -> ExceptionResponse:
return ExceptionResponse(
exception=type(exception).__name__,
args=[arg for arg in exception.args if arg] if exception.args else [],
message=exception.msg if hasattr(exception, "msg") else None,
message=exception.msg if hasattr(exception, "msg") else None, # type: ignore
)


Expand All @@ -45,7 +45,7 @@ async def get_sort_parameters(query_params: list[str], model: BaseModel) -> list
result = []

for query_param in query_params:
pattern = re.compile("^\w+:(asc|desc)$")
pattern = re.compile("^\w+:(asc|desc)$") # type: ignore

if not re.fullmatch(pattern, query_param):
validation_error.append(
Expand All @@ -67,8 +67,9 @@ async def get_sort_parameters(query_params: list[str], model: BaseModel) -> list
"type": "value_error.sort",
}
)
continue

result.append(Sort(clause=sort_params[0], sort=sort_params[1]))
result.append(Sort(clause=sort_params[0], sort=sort_params[1])) # type: ignore

if validation_error:
raise HTTPException(
Expand All @@ -94,7 +95,7 @@ async def check_quota(
raise QuotaExceededException(
limit=Settings().DAILY_LIMIT_PER_HOST,
host=host,
latest_result=loads(latest_result.model_dump_json()),
latest_result=loads(latest_result.model_dump_json() or "{}"), # type: ignore
)

return Settings().DAILY_LIMIT_PER_HOST - count_daily_request_per_host
24 changes: 13 additions & 11 deletions bases/ecoindex/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,32 @@ def analyze(
secho(f"⏲️ Crawling root url {url[0]} -> Wait a minute!", fg=colors.MAGENTA)
with spinner():
urls = get_urls_recursive(main_url=url[0])
urls = urls if urls else url
urls = urls if urls else url # type: ignore

(
file_prefix,
input_file,
logger_file,
) = get_file_prefix_input_file_logger_file(urls=urls)
) = get_file_prefix_input_file_logger_file(urls=urls) # type: ignore

elif url:
urls = get_url_from_args(urls_arg=url)
urls = get_url_from_args(urls_arg=url) # type: ignore
(
file_prefix,
input_file,
logger_file,
) = get_file_prefix_input_file_logger_file(urls=urls, tmp_folder=tmp_folder)
) = get_file_prefix_input_file_logger_file(urls=urls, tmp_folder=tmp_folder) # type: ignore

elif urls_file:
urls = get_urls_from_file(urls_file=urls_file)
urls = get_urls_from_file(urls_file=urls_file) # type: ignore
(
file_prefix,
input_file,
logger_file,
) = get_file_prefix_input_file_logger_file(
urls=urls, urls_file=urls_file, tmp_folder=tmp_folder
urls=urls, # type: ignore
urls_file=urls_file,
tmp_folder=tmp_folder,
)
elif sitemap:
secho(
Expand All @@ -172,14 +174,14 @@ def analyze(
file_prefix,
input_file,
logger_file,
) = get_file_prefix_input_file_logger_file(urls=urls)
) = get_file_prefix_input_file_logger_file(urls=urls) # type: ignore

else:
secho("🔥 You must provide an url...", fg=colors.RED)
raise Exit(code=1)

if input_file:
write_urls_to_file(file_prefix=file_prefix, urls=urls)
write_urls_to_file(file_prefix=file_prefix, urls=urls) # type: ignore
secho(f"📁️ Urls recorded in file `{input_file}`")

if logger_file:
Expand Down Expand Up @@ -266,13 +268,13 @@ def analyze(

Path(output_folder).mkdir(parents=True, exist_ok=True)
write_results_to_file(
filename=output_filename, results=results, export_format=export_format
filename=str(output_filename), results=results, export_format=export_format
)
secho(f"🙌️ File {output_filename} written !", fg=colors.GREEN)
if html_report:
Report(
results_file=output_filename,
output_path=output_folder,
output_path=str(output_folder),
domain=file_prefix,
date=time_now,
language=html_report_language,
Expand Down Expand Up @@ -317,7 +319,7 @@ def report(
output_folder = output_folder if output_folder else dirname(results_file)

Report(
results_file=results_file,
results_file=Path(results_file),
output_path=output_folder,
domain=domain,
date=datetime.now(),
Expand Down
12 changes: 6 additions & 6 deletions bases/ecoindex/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
exception=QuotaExceededException.__name__,
status_code=429,
message=exc.message,
Expand All @@ -95,7 +95,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
exception=EcoindexHostUnreachable.__name__,
status_code=502,
message=(
Expand All @@ -110,7 +110,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
exception=EcoindexTimeout.__name__,
status_code=504,
message=(
Expand All @@ -124,7 +124,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
exception=type(exc).__name__,
status_code=500,
message=str(exc.message) if exc.message else "",
Expand All @@ -136,7 +136,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
exception=EcoindexContentTypeError.__name__,
status_code=520,
message=exc.args[0],
Expand All @@ -148,7 +148,7 @@ async def async_ecoindex_task(
return QueueTaskResult(
status=TaskStatus.FAILURE,
error=QueueTaskError(
url=url,
url=url, # type: ignore
status_code=521,
exception=EcoindexStatusError.__name__,
message=exc.message,
Expand Down
3 changes: 2 additions & 1 deletion components/ecoindex/compute/ecoindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
quantiles_size,
)
from ecoindex.models import Ecoindex
from ecoindex.models.enums import Grade
from typing_extensions import deprecated


Expand Down Expand Up @@ -38,7 +39,7 @@ async def get_ecoindex(dom: int, size: float, requests: int) -> Ecoindex:

return Ecoindex(
score=score,
grade=await get_grade(score),
grade=Grade(await get_grade(score)),
ges=await get_greenhouse_gases_emmission(score),
water=await get_water_consumption(score),
)
Expand Down
4 changes: 2 additions & 2 deletions components/ecoindex/database/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def date_filter(
date_from: date | None = None,
date_to: date | None = None,
) -> SelectOfScalar:
if date_from:
if date_from and ApiEcoindex.date:
statement = statement.where(ApiEcoindex.date >= date_from)

if date_to:
if date_to and ApiEcoindex.date:
statement = statement.where(ApiEcoindex.date <= date_to)

return statement
Expand Down
8 changes: 4 additions & 4 deletions components/ecoindex/database/repositories/ecoindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def get_ecoindex_result_list_db(
elif sort.sort == "desc":
sort_parameter = desc(sort.clause)

statement = statement.order_by(sort_parameter)
statement = statement.order_by(sort_parameter) # type: ignore

ecoindexes = await session.exec(statement)

Expand All @@ -92,7 +92,7 @@ async def get_ecoindex_result_list_db(

async def get_ecoindex_result_by_id_db(
session: AsyncSession, id: UUID, version: Version = Version.v1
) -> ApiEcoindex:
) -> ApiEcoindex | None:
statement = (
select(ApiEcoindex)
.where(ApiEcoindex.id == id)
Expand All @@ -114,11 +114,11 @@ async def get_count_daily_request_per_host(session: AsyncSession, host: str) ->
return len(results.all())


async def get_latest_result(session: AsyncSession, host: str) -> ApiEcoindex:
async def get_latest_result(session: AsyncSession, host: str) -> ApiEcoindex | None:
statement = (
select(ApiEcoindex)
.where(ApiEcoindex.host == host)
.order_by(desc(ApiEcoindex.date))
.order_by(desc(str(ApiEcoindex.date)))
.limit(1)
)

Expand Down
2 changes: 1 addition & 1 deletion components/ecoindex/database/repositories/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def get_host_list_db(
)

if host:
statement = statement.filter(ApiEcoindex.host.like(f"%{host}%"))
statement = statement.filter(ApiEcoindex.host.like(f"%{host}%")) # type: ignore

statement = date_filter(statement=statement, date_from=date_from, date_to=date_to)

Expand Down
6 changes: 3 additions & 3 deletions components/ecoindex/models/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ class WebPage(BaseModel):
@field_validator("url")
@classmethod
def url_as_http_url(cls, v: str) -> str:
url_object = AnyHttpUrl(url=v)
url_object = AnyHttpUrl(url=v) # type: ignore
assert url_object.scheme in {"http", "https"}, "scheme must be http or https"

return url_object.unicode_string()

def get_url_host(self) -> str:
url_object = AnyHttpUrl(url=self.url)
url_object = AnyHttpUrl(url=self.url) # type: ignore

return str(url_object.host)

def get_url_path(self) -> str:
url_obect = AnyHttpUrl(url=self.url)
url_obect = AnyHttpUrl(url=self.url) # type: ignore

return str(url_obect.path)

Expand Down
7 changes: 5 additions & 2 deletions components/ecoindex/scraper/scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ecoindex.models.compute import PageMetrics, Result, ScreenShot, WindowSize
from ecoindex.models.scraper import MimetypeAggregation, RequestItem, Requests
from ecoindex.utils.screenshots import convert_screenshot_to_webp, set_screenshot_rights
from playwright._impl._api_structures import SetCookieParam
from playwright._impl._api_structures import SetCookieParam, ViewportSize
from playwright.async_api import async_playwright
from typing_extensions import deprecated

Expand Down Expand Up @@ -73,7 +73,10 @@ async def scrap_page(self) -> PageMetrics:
browser = await p.chromium.launch(headless=self.headless)
self.context = await browser.new_context(
record_har_path=self.har_temp_file_path,
screen=self.window_size.model_dump(),
screen=ViewportSize(
width=self.window_size.width,
height=self.window_size.height,
),
ignore_https_errors=True,
http_credentials={
"username": self.basic_auth.split(":")[0],
Expand Down
Loading

0 comments on commit 1391103

Please sign in to comment.