Skip to content

Commit

Permalink
feat(cli): create a method replace_localhost_with_hostdocker, create …
Browse files Browse the repository at this point in the history
…test for this method
  • Loading branch information
PaulPHPE committed Feb 20, 2024
1 parent 3fc2a2d commit 32c5035
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
16 changes: 6 additions & 10 deletions bases/ecoindex/cli/arguments_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from urllib.parse import urlparse, urlunparse

from ecoindex.cli.crawl import EcoindexSpider
from ecoindex.cli.helper import replace_localhost_with_hostdocker
from ecoindex.models import WindowSize
from ecoindex.config import Settings

from pydantic import AnyHttpUrl, validate_call
from pydantic.types import FilePath
Expand Down Expand Up @@ -38,11 +38,9 @@ def get_urls_from_file(urls_file: FilePath) -> Set[str]:

def get_urls_recursive(main_url: str) -> Set[str]:
parsed_url = urlparse(main_url)
netloc = parsed_url.netloc
domain = netloc
if Settings().DOCKER_CONTAINER and parsed_url.hostname == "localhost":
domain = "host.docker.internal"
netloc = netloc.replace('localhost', 'host.docker.internal')
host_infos = replace_localhost_with_hostdocker(parsed_url.netloc)
netloc = host_infos.netloc
domain = host_infos.domain
main_url = f"{parsed_url.scheme}://{netloc}"
process = CrawlerProcess()

Expand All @@ -65,10 +63,8 @@ def get_url_from_args(urls_arg: list[AnyHttpUrl]) -> set[AnyHttpUrl]:
urls_from_args = set()
for url in urls_arg:
parsed_url = urlparse(str(url))
if Settings().DOCKER_CONTAINER:
if (parsed_url.hostname == "localhost"):
replaced_netloc = parsed_url.netloc.replace('localhost', 'host.docker.internal')
url = AnyHttpUrl(urlunparse((parsed_url.scheme, replaced_netloc, parsed_url.path, parsed_url.params, parsed_url.query, parsed_url.fragment)))
host_infos = replace_localhost_with_hostdocker(parsed_url.netloc)
url = AnyHttpUrl(urlunparse((parsed_url.scheme, host_infos.netloc, parsed_url.path, parsed_url.params, parsed_url.query, parsed_url.fragment)))
urls_from_args.add(url)

return urls_from_args
Expand Down
15 changes: 14 additions & 1 deletion bases/ecoindex/cli/helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from asyncio import run
from ecoindex.config import Settings

from ecoindex.models import Result, WindowSize
from ecoindex.models import Result, WindowSize, CliHost
from ecoindex.scraper import EcoindexScraper


Expand Down Expand Up @@ -36,3 +37,15 @@ def run_page_analysis(
),
False,
)


def replace_localhost_with_hostdocker(netloc: str) -> CliHost:
if Settings().DOCKER_CONTAINER and "localhost" in netloc:
domain = "host.docker.internal"
netloc = netloc.replace("localhost", domain)
elif "localhost" in netloc :
domain = "localhost"
else :
domain = netloc

return CliHost(domain=domain, netloc=netloc)
4 changes: 4 additions & 0 deletions components/ecoindex/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from ecoindex.models.cli import (
CliHost,
)
from ecoindex.models.compute import (
Ecoindex,
PageMetrics,
Expand All @@ -19,6 +22,7 @@
from ecoindex.models.sort import Sort

__all__ = [
"CliHost",
"Ecoindex",
"example_daily_limit_response",
"example_ecoindex_not_found",
Expand Down
6 changes: 6 additions & 0 deletions components/ecoindex/models/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import BaseModel

class CliHost(BaseModel):
domain: str
netloc: str

2 changes: 1 addition & 1 deletion projects/ecoindex_cli/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN poetry export --output=requirements.txt --without-hashes

FROM python:3.12-slim

ARG wheel=ecoindex_cli-2.25.0-py3-none-any.whl
ARG wheel=ecoindex_cli-2.26.0a0-py3-none-any.whl
ENV DOCKER_CONTAINER=True

WORKDIR /code
Expand Down
19 changes: 19 additions & 0 deletions test/bases/ecoindex/cli/test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
from urllib.parse import urlparse
from ecoindex.cli.helper import replace_localhost_with_hostdocker
from ecoindex.models.cli import CliHost


def test_replace_localhost_with_hostdocker():
assert replace_localhost_with_hostdocker(
urlparse("https://test.com/page/").netloc
) == CliHost(domain="test.com", netloc="test.com")

assert replace_localhost_with_hostdocker(
urlparse("https://localhost:8000/page/").netloc
) == CliHost(domain="localhost", netloc="localhost:8000")

os.environ["DOCKER_CONTAINER"] = "true"
assert replace_localhost_with_hostdocker(
urlparse("https://localhost:8000/page/").netloc
) == CliHost(domain="host.docker.internal", netloc="host.docker.internal:8000")

0 comments on commit 32c5035

Please sign in to comment.