diff --git a/README.md b/README.md index 1de6ec4..832da62 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ endpoint. All the routes use the same format and general idea. Converting a single HTML file into a PDF: ```python +from gotenberg_client import GotenbergClient + with GotenbergClient("http://localhost:3000") as client: with client.chromium.html_to_pdf() as route: response = route.index("my-index.html").run() @@ -65,6 +67,8 @@ with GotenbergClient("http://localhost:3000") as client: Converting an HTML file with additional resources into a PDF: ```python +from gotenberg_client import GotenbergClient + with GotenbergClient("http://localhost:3000") as client: with client.chromium.html_to_pdf() as route: response = route.index("my-index.html").resource("image.png").resource("style.css").run() @@ -74,6 +78,7 @@ with GotenbergClient("http://localhost:3000") as client: Converting an HTML file with additional resources into a PDF/A1a format: ```python +from gotenberg_client import GotenbergClient from gotenberg_client.options import PdfAFormat with GotenbergClient("http://localhost:3000") as client: @@ -85,6 +90,7 @@ with GotenbergClient("http://localhost:3000") as client: Converting a URL into PDF, in landscape format ```python +from gotenberg_client import GotenbergClient from gotenberg_client.options import PageOrientation with GotenbergClient("http://localhost:3000") as client: @@ -98,6 +104,8 @@ used as context manager. If for some reason you cannot, you should `.close` the routes: ```python +from gotenberg_client import GotenbergClient + try: client = GotenbergClient("http://localhost:3000") try: diff --git a/src/gotenberg_client/__init__.py b/src/gotenberg_client/__init__.py index e54c6d9..015889f 100644 --- a/src/gotenberg_client/__init__.py +++ b/src/gotenberg_client/__init__.py @@ -1,3 +1,6 @@ # SPDX-FileCopyrightText: 2023-present Trenton H # # SPDX-License-Identifier: MPL-2.0 +from gotenberg_client._client import GotenbergClient + +__all__ = ["GotenbergClient"] diff --git a/src/gotenberg_client/base.py b/src/gotenberg_client/_base.py similarity index 98% rename from src/gotenberg_client/base.py rename to src/gotenberg_client/_base.py index 5061f3b..41a69e8 100644 --- a/src/gotenberg_client/base.py +++ b/src/gotenberg_client/_base.py @@ -14,8 +14,8 @@ from httpx._types import RequestFiles from gotenberg_client._types_compat import Self +from gotenberg_client._utils import guess_mime_type from gotenberg_client.options import PdfAFormat -from gotenberg_client.utils import guess_mime_type logger = logging.getLogger(__name__) diff --git a/src/gotenberg_client/client.py b/src/gotenberg_client/_client.py similarity index 87% rename from src/gotenberg_client/client.py rename to src/gotenberg_client/_client.py index 50f9bff..e384d29 100644 --- a/src/gotenberg_client/client.py +++ b/src/gotenberg_client/_client.py @@ -10,11 +10,11 @@ from httpx import Client -from gotenberg_client.convert.chromium import ChromiumApi -from gotenberg_client.convert.libre_office import LibreOfficeApi -from gotenberg_client.convert.pdfa import PdfAApi -from gotenberg_client.health import HealthCheckApi -from gotenberg_client.merge import MergeApi +from gotenberg_client._convert.chromium import ChromiumApi +from gotenberg_client._convert.libre_office import LibreOfficeApi +from gotenberg_client._convert.pdfa import PdfAApi +from gotenberg_client._health import HealthCheckApi +from gotenberg_client._merge import MergeApi class GotenbergClient: diff --git a/src/gotenberg_client/convert/__init__.py b/src/gotenberg_client/_convert/__init__.py similarity index 100% rename from src/gotenberg_client/convert/__init__.py rename to src/gotenberg_client/_convert/__init__.py diff --git a/src/gotenberg_client/convert/chromium.py b/src/gotenberg_client/_convert/chromium.py similarity index 85% rename from src/gotenberg_client/convert/chromium.py rename to src/gotenberg_client/_convert/chromium.py index 1a0a577..5ffc18f 100644 --- a/src/gotenberg_client/convert/chromium.py +++ b/src/gotenberg_client/_convert/chromium.py @@ -9,9 +9,9 @@ from typing import List from typing import Union +from gotenberg_client._base import BaseApi +from gotenberg_client._convert.common import ConvertBaseRoute from gotenberg_client._types_compat import Self -from gotenberg_client.base import BaseApi -from gotenberg_client.convert.common import ConvertBaseRoute from gotenberg_client.options import EmulatedMediaType from gotenberg_client.options import Margin from gotenberg_client.options import PageSize @@ -28,25 +28,6 @@ def __bool__(self) -> bool: FORCE_MULTIPART: Final = ForceMultipartDict() -# Define common paper sizes as shortcuts -A0: Final = PageSize(width=33.1, height=46.8) -A1: Final = PageSize(width=23.4, height=33.1) -A2: Final = PageSize(width=16.54, height=23.4) -A3: Final = PageSize(width=11.7, height=16.54) -A4: Final = PageSize(width=8.5, height=11) -A5: Final = PageSize(width=5.83, height=8.27) -A6: Final = PageSize(width=4.13, height=5.83) -Letter = A4 -Legal: Final = PageSize(width=8.5, height=14) -Tabloid: Final = PageSize(width=11, height=17) -Ledge: Final = PageSize(width=17, height=11) - - -Gotenberg_Default_Margines: Final = Margin(0.39, 0.39, 0.39, 0.39) -Word_Default_Margins: Final = Margin(top=1.0, bottom=1.0, left=1.0, right=1.0) -Word_Narrow_Margins: Final = Margin(top=0.5, bottom=0.5, left=0.5, right=0.5) - - class ChromiumBaseRoute(ConvertBaseRoute): """ https://gotenberg.dev/docs/routes#convert-with-chromium diff --git a/src/gotenberg_client/convert/common.py b/src/gotenberg_client/_convert/common.py similarity index 94% rename from src/gotenberg_client/convert/common.py rename to src/gotenberg_client/_convert/common.py index 72a6d45..afb88fb 100644 --- a/src/gotenberg_client/convert/common.py +++ b/src/gotenberg_client/_convert/common.py @@ -3,8 +3,8 @@ # SPDX-License-Identifier: MPL-2.0 import logging +from gotenberg_client._base import BaseRoute from gotenberg_client._types_compat import Self -from gotenberg_client.base import BaseRoute from gotenberg_client.options import PageOrientation logger = logging.getLogger() diff --git a/src/gotenberg_client/convert/libre_office.py b/src/gotenberg_client/_convert/libre_office.py similarity index 93% rename from src/gotenberg_client/convert/libre_office.py rename to src/gotenberg_client/_convert/libre_office.py index 3a35dfb..65654f8 100644 --- a/src/gotenberg_client/convert/libre_office.py +++ b/src/gotenberg_client/_convert/libre_office.py @@ -4,9 +4,9 @@ from pathlib import Path from typing import List +from gotenberg_client._base import BaseApi +from gotenberg_client._convert.common import ConvertBaseRoute from gotenberg_client._types_compat import Self -from gotenberg_client.base import BaseApi -from gotenberg_client.convert.common import ConvertBaseRoute class LibreOfficeConvertRoute(ConvertBaseRoute): diff --git a/src/gotenberg_client/convert/pdfa.py b/src/gotenberg_client/_convert/pdfa.py similarity index 89% rename from src/gotenberg_client/convert/pdfa.py rename to src/gotenberg_client/_convert/pdfa.py index 2f9ec90..d0ba3e2 100644 --- a/src/gotenberg_client/convert/pdfa.py +++ b/src/gotenberg_client/_convert/pdfa.py @@ -4,9 +4,9 @@ from pathlib import Path from typing import List +from gotenberg_client._base import BaseApi +from gotenberg_client._convert.common import ConvertBaseRoute from gotenberg_client._types_compat import Self -from gotenberg_client.base import BaseApi -from gotenberg_client.convert.common import ConvertBaseRoute class PdfAConvertRoute(ConvertBaseRoute): diff --git a/src/gotenberg_client/health.py b/src/gotenberg_client/_health.py similarity index 98% rename from src/gotenberg_client/health.py rename to src/gotenberg_client/_health.py index c76f36d..3f717bc 100644 --- a/src/gotenberg_client/health.py +++ b/src/gotenberg_client/_health.py @@ -9,7 +9,7 @@ from typing import TypedDict from typing import no_type_check -from gotenberg_client.base import BaseApi +from gotenberg_client._base import BaseApi _TIME_RE = re.compile( r"(?P\d{4})-" diff --git a/src/gotenberg_client/merge.py b/src/gotenberg_client/_merge.py similarity index 91% rename from src/gotenberg_client/merge.py rename to src/gotenberg_client/_merge.py index 7d48bea..52848a1 100644 --- a/src/gotenberg_client/merge.py +++ b/src/gotenberg_client/_merge.py @@ -4,8 +4,8 @@ from pathlib import Path from typing import List -from gotenberg_client.base import BaseApi -from gotenberg_client.base import BaseRoute +from gotenberg_client._base import BaseApi +from gotenberg_client._base import BaseRoute class MergeRoute(BaseRoute): diff --git a/src/gotenberg_client/metrics.py b/src/gotenberg_client/_metrics.py similarity index 100% rename from src/gotenberg_client/metrics.py rename to src/gotenberg_client/_metrics.py diff --git a/src/gotenberg_client/utils.py b/src/gotenberg_client/_utils.py similarity index 100% rename from src/gotenberg_client/utils.py rename to src/gotenberg_client/_utils.py diff --git a/src/gotenberg_client/options.py b/src/gotenberg_client/options.py index 8f58d8e..4751de7 100644 --- a/src/gotenberg_client/options.py +++ b/src/gotenberg_client/options.py @@ -4,10 +4,11 @@ import dataclasses import enum from typing import Dict +from typing import Final from typing import Optional from typing import Union -from gotenberg_client.utils import optional_to_form +from gotenberg_client._utils import optional_to_form @enum.unique @@ -52,6 +53,20 @@ def to_form(self) -> Dict[str, str]: return data +# Define common paper sizes as shortcuts +A0: Final = PageSize(width=33.1, height=46.8) +A1: Final = PageSize(width=23.4, height=33.1) +A2: Final = PageSize(width=16.54, height=23.4) +A3: Final = PageSize(width=11.7, height=16.54) +A4: Final = PageSize(width=8.5, height=11) +A5: Final = PageSize(width=5.83, height=8.27) +A6: Final = PageSize(width=4.13, height=5.83) +Letter = A4 +Legal: Final = PageSize(width=8.5, height=14) +Tabloid: Final = PageSize(width=11, height=17) +Ledge: Final = PageSize(width=17, height=11) + + @dataclasses.dataclass class Margin: top: Optional[Union[float, int]] = None @@ -67,6 +82,11 @@ def to_form(self) -> Dict[str, str]: return data +Gotenberg_Default_Margins: Final = Margin(0.39, 0.39, 0.39, 0.39) +Word_Default_Margins: Final = Margin(top=1.0, bottom=1.0, left=1.0, right=1.0) +Word_Narrow_Margins: Final = Margin(top=0.5, bottom=0.5, left=0.5, right=0.5) + + @enum.unique class EmulatedMediaType(str, enum.Enum): Print = enum.auto() diff --git a/tests/conftest.py b/tests/conftest.py index 7b2ba4a..a8ff35c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,7 @@ import pytest -from gotenberg_client.client import GotenbergClient +from gotenberg_client._client import GotenbergClient GOTENBERG_URL: Final[str] = os.getenv("GOTENBERG_URL", "http://localhost:3000") diff --git a/tests/test_convert_chromium_html.py b/tests/test_convert_chromium_html.py index 026f410..d9e790c 100644 --- a/tests/test_convert_chromium_html.py +++ b/tests/test_convert_chromium_html.py @@ -6,9 +6,9 @@ from httpx import codes from pytest_httpx import HTTPXMock -from gotenberg_client.client import GotenbergClient -from gotenberg_client.convert.chromium import A4 -from gotenberg_client.convert.chromium import Margin +from gotenberg_client._client import GotenbergClient +from gotenberg_client._convert.chromium import Margin +from gotenberg_client.options import A4 from gotenberg_client.options import PageOrientation from gotenberg_client.options import PdfAFormat from tests.conftest import SAMPLE_DIR diff --git a/tests/test_convert_chromium_markdown.py b/tests/test_convert_chromium_markdown.py index 3c45521..e300053 100644 --- a/tests/test_convert_chromium_markdown.py +++ b/tests/test_convert_chromium_markdown.py @@ -1,6 +1,6 @@ from httpx import codes -from gotenberg_client.client import GotenbergClient +from gotenberg_client._client import GotenbergClient from tests.conftest import SAMPLE_DIR diff --git a/tests/test_convert_chromium_url.py b/tests/test_convert_chromium_url.py index bee4bfd..5674ee6 100644 --- a/tests/test_convert_chromium_url.py +++ b/tests/test_convert_chromium_url.py @@ -4,8 +4,8 @@ from httpx import codes from pytest_httpx import HTTPXMock -from gotenberg_client.client import GotenbergClient -from gotenberg_client.convert.chromium import EmulatedMediaType +from gotenberg_client._client import GotenbergClient +from gotenberg_client._convert.chromium import EmulatedMediaType from tests.utils import verify_stream_contains diff --git a/tests/test_convert_libre_office.py b/tests/test_convert_libre_office.py index dfe62cd..1933742 100644 --- a/tests/test_convert_libre_office.py +++ b/tests/test_convert_libre_office.py @@ -6,9 +6,9 @@ import pytest from httpx import codes -from gotenberg_client.client import GotenbergClient +from gotenberg_client._client import GotenbergClient +from gotenberg_client._utils import guess_mime_type_stdlib from gotenberg_client.options import PdfAFormat -from gotenberg_client.utils import guess_mime_type_stdlib from tests.conftest import SAMPLE_DIR from tests.conftest import SAVE_DIR from tests.conftest import SAVE_OUTPUTS @@ -86,7 +86,7 @@ def test_libre_office_convert_multiples_format_merged(self, client: GotenbergCli (SAVE_DIR / "test_libre_office_convert_multiples_format.zip").write_bytes(resp.content) def test_libre_office_convert_std_lib_mime(self, client: GotenbergClient): - with patch("gotenberg_client.utils.guess_mime_type") as mocked_guess_mime_type: + with patch("gotenberg_client._utils.guess_mime_type") as mocked_guess_mime_type: mocked_guess_mime_type.side_effect = guess_mime_type_stdlib with client.libre_office.to_pdf() as route: resp = route.convert_files([SAMPLE_DIR / "sample.docx", SAMPLE_DIR / "sample.odt"]).no_merge().run() diff --git a/tests/test_convert_pdf_a.py b/tests/test_convert_pdf_a.py index 184b65e..8b80c25 100644 --- a/tests/test_convert_pdf_a.py +++ b/tests/test_convert_pdf_a.py @@ -5,7 +5,7 @@ import pytest from httpx import codes -from gotenberg_client.client import GotenbergClient +from gotenberg_client._client import GotenbergClient from gotenberg_client.options import PdfAFormat from tests.conftest import SAMPLE_DIR from tests.conftest import SAVE_DIR diff --git a/tests/test_health.py b/tests/test_health.py index 5f26b4a..84f415e 100644 --- a/tests/test_health.py +++ b/tests/test_health.py @@ -1,5 +1,5 @@ -from gotenberg_client.client import GotenbergClient -from gotenberg_client.health import StatusOptions +from gotenberg_client._client import GotenbergClient +from gotenberg_client._health import StatusOptions class TestHealthStatus: diff --git a/tests/test_merge.py b/tests/test_merge.py index be18b08..7ccc1f6 100644 --- a/tests/test_merge.py +++ b/tests/test_merge.py @@ -7,7 +7,7 @@ import pytest from httpx import codes -from gotenberg_client.client import GotenbergClient +from gotenberg_client._client import GotenbergClient from gotenberg_client.options import PdfAFormat from tests.conftest import SAMPLE_DIR from tests.conftest import SAVE_DIR