From 2f236a96551c6651d583d9ccdb58b4bebee00d3a Mon Sep 17 00:00:00 2001 From: PaulPHPE <93597135+PaulPHPE@users.noreply.github.com> Date: Thu, 15 Feb 2024 14:34:32 +0100 Subject: [PATCH] feat(settings): add Docker container variable to settings --- bases/ecoindex/cli/arguments_handler.py | 7 +++--- components/ecoindex/config/settings.py | 2 ++ projects/ecoindex_cli/dockerfile | 2 +- projects/ecoindex_cli/poetry.lock | 31 ++++++++++++++++++++++++- projects/ecoindex_cli/pyproject.toml | 2 ++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/bases/ecoindex/cli/arguments_handler.py b/bases/ecoindex/cli/arguments_handler.py index 9fa818e..ae5d9e1 100644 --- a/bases/ecoindex/cli/arguments_handler.py +++ b/bases/ecoindex/cli/arguments_handler.py @@ -1,10 +1,11 @@ -import os from tempfile import NamedTemporaryFile from typing import Set from urllib.parse import urlparse, urlunparse from ecoindex.cli.crawl import EcoindexSpider from ecoindex.models import WindowSize +from ecoindex.config import Settings + from pydantic import AnyHttpUrl, validate_call from pydantic.types import FilePath from scrapy.crawler import CrawlerProcess @@ -39,7 +40,7 @@ def get_urls_recursive(main_url: str) -> Set[str]: parsed_url = urlparse(main_url) netloc = parsed_url.netloc domain = netloc - if os.environ.get('DOCKER_CONTAINER') == 'true': + if Settings().DOCKER_CONTAINER: if (parsed_url.hostname == "localhost"): domain = "host.docker.internal" netloc = netloc.replace('localhost', 'host.docker.internal') @@ -65,7 +66,7 @@ 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 os.environ.get('DOCKER_CONTAINER') == 'true': + 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))) diff --git a/components/ecoindex/config/settings.py b/components/ecoindex/config/settings.py index 7fb89ad..9fdb634 100644 --- a/components/ecoindex/config/settings.py +++ b/components/ecoindex/config/settings.py @@ -1,3 +1,4 @@ +import os from pydantic_settings import BaseSettings, SettingsConfigDict @@ -8,6 +9,7 @@ class Settings(BaseSettings): CORS_ALLOWED_ORIGINS: list = ["*"] DAILY_LIMIT_PER_HOST: int = 0 DATABASE_URL: str = "sqlite+aiosqlite:///db.sqlite3" + DOCKER_CONTAINER: bool = os.environ.get('DOCKER_CONTAINER', default='False').lower() == 'true' DEBUG: bool = False ENABLE_SCREENSHOT: bool = False EXCLUDED_HOSTS: list[str] = ["localhost", "127.0.0.1"] diff --git a/projects/ecoindex_cli/dockerfile b/projects/ecoindex_cli/dockerfile index 5b2a98d..1b38686 100644 --- a/projects/ecoindex_cli/dockerfile +++ b/projects/ecoindex_cli/dockerfile @@ -10,7 +10,7 @@ RUN poetry export --output=requirements.txt --without-hashes FROM python:3.12-slim ARG wheel=ecoindex_cli-2.24.1-py3-none-any.whl -ENV DOCKER_CONTAINER=true +ENV DOCKER_CONTAINER=True WORKDIR /code diff --git a/projects/ecoindex_cli/poetry.lock b/projects/ecoindex_cli/poetry.lock index e2e1b5a..f64ce56 100644 --- a/projects/ecoindex_cli/poetry.lock +++ b/projects/ecoindex_cli/poetry.lock @@ -1509,6 +1509,21 @@ files = [ [package.dependencies] typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" +[[package]] +name = "pydantic-settings" +version = "2.1.0" +description = "Settings management using Pydantic" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pydantic_settings-2.1.0-py3-none-any.whl", hash = "sha256:7621c0cb5d90d1140d2f0ef557bdf03573aac7035948109adf2574770b77605a"}, + {file = "pydantic_settings-2.1.0.tar.gz", hash = "sha256:26b1492e0a24755626ac5e6d715e9077ab7ad4fb5f19a8b7ed7011d52f36141c"}, +] + +[package.dependencies] +pydantic = ">=2.3.0" +python-dotenv = ">=0.21.0" + [[package]] name = "pydispatcher" version = "2.0.7" @@ -1611,6 +1626,20 @@ files = [ [package.dependencies] six = ">=1.5" +[[package]] +name = "python-dotenv" +version = "1.0.1" +description = "Read key-value pairs from a .env file and set them as environment variables" +optional = false +python-versions = ">=3.8" +files = [ + {file = "python-dotenv-1.0.1.tar.gz", hash = "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca"}, + {file = "python_dotenv-1.0.1-py3-none-any.whl", hash = "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a"}, +] + +[package.extras] +cli = ["click (>=5.0)"] + [[package]] name = "pytz" version = "2024.1" @@ -2053,4 +2082,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.13" -content-hash = "a9930c3b46ccd9a6980a85220595992d701b6e8234615f3e6e12736b899ce0d4" +content-hash = "bc406663a9e8852446f94e76f2d0a3dc2eab1441b7ff99d54aed03153f952c23" diff --git a/projects/ecoindex_cli/pyproject.toml b/projects/ecoindex_cli/pyproject.toml index f3615d3..8668557 100644 --- a/projects/ecoindex_cli/pyproject.toml +++ b/projects/ecoindex_cli/pyproject.toml @@ -11,6 +11,7 @@ include = ["LICENSE"] packages = [ { include = "ecoindex/cli", from = "../../bases" }, { include = "ecoindex/compute", from = "../../components" }, + { include = "ecoindex/config", from = "../../components" }, { include = "ecoindex/data", from = "../../components" }, { include = "ecoindex/exceptions", from = "../../components" }, { include = "ecoindex/models", from = "../../components" }, @@ -28,6 +29,7 @@ pandas = "^2.1.2" playwright = "^1.39.0" playwright-stealth = "^1.0.6" pydantic = "^2.4.2" +pydantic-settings = "^2.0.3" python = ">=3.10,<3.13" pyyaml = "^6.0.1" rich = "^13.6.0"