-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves where the options are located, centralizing them
- Loading branch information
Showing
11 changed files
with
121 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,30 @@ | ||
# SPDX-FileCopyrightText: 2023-present Trenton H <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
import dataclasses | ||
import enum | ||
import json | ||
import logging | ||
from pathlib import Path | ||
from typing import Dict | ||
from typing import Final | ||
from typing import List | ||
from typing import Optional | ||
from typing import Union | ||
|
||
from gotenberg_client.base import BaseApi | ||
from gotenberg_client.convert.common import FORCE_MULTIPART | ||
from gotenberg_client.convert.common import ConvertBaseRoute | ||
from gotenberg_client.convert.common import ForceMultipartDict | ||
from gotenberg_client.convert.common import optional_to_form | ||
from gotenberg_client.options import EmulatedMediaType | ||
from gotenberg_client.options import Margin | ||
from gotenberg_client.options import PageSize | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
@dataclasses.dataclass | ||
class PageSize: | ||
width: Optional[Union[float, int]] = None | ||
height: Optional[Union[float, int]] = None | ||
# See https://github.com/psf/requests/issues/1081#issuecomment-428504128 | ||
class ForceMultipartDict(Dict): | ||
def __bool__(self) -> bool: | ||
return True | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
data = optional_to_form(self.width, "paperWidth") | ||
data.update(optional_to_form(self.height, "paperHeight")) | ||
return data | ||
|
||
FORCE_MULTIPART: Final = ForceMultipartDict() | ||
|
||
|
||
# Define common paper sizes as shortcuts | ||
|
@@ -46,40 +41,11 @@ def to_form(self) -> Dict[str, str]: | |
Ledge: Final = PageSize(width=17, height=11) | ||
|
||
|
||
@dataclasses.dataclass | ||
class Margin: | ||
top: Optional[Union[float, int]] = None | ||
bottom: Optional[Union[float, int]] = None | ||
left: Optional[Union[float, int]] = None | ||
right: Optional[Union[float, int]] = None | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
data = optional_to_form(self.top, "marginTop") | ||
data.update(optional_to_form(self.bottom, "marginBottom")) | ||
data.update(optional_to_form(self.left, "marginLeft")) | ||
data.update(optional_to_form(self.right, "marginRight")) | ||
return data | ||
|
||
|
||
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) | ||
|
||
|
||
@enum.unique | ||
class EmulatedMediaTypeChoices(str, enum.Enum): | ||
Print = enum.auto() | ||
Screen = enum.auto() | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
if self.value == EmulatedMediaTypeChoices.Print.value: | ||
return {"emulatedMediaType": "print"} | ||
elif self.value == EmulatedMediaTypeChoices.Screen.value: | ||
return {"emulatedMediaType": "screen"} | ||
else: # pragma: no cover | ||
raise NotImplementedError(self.value) | ||
|
||
|
||
class ChromiumBaseRoute(ConvertBaseRoute): | ||
def header(self, header: Path) -> "ChromiumBaseRoute": | ||
self._add_file_map(header, "header.html") | ||
|
@@ -144,7 +110,7 @@ def render_expr(self, expr: str) -> "ChromiumBaseRoute": | |
self._form_data.update({"waitForExpression": expr}) | ||
return self | ||
|
||
def media_type(self, media_type: EmulatedMediaTypeChoices) -> "ChromiumBaseRoute": | ||
def media_type(self, media_type: EmulatedMediaType) -> "ChromiumBaseRoute": | ||
self._form_data.update(media_type.to_form()) | ||
return self | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,20 @@ | ||
# SPDX-FileCopyrightText: 2023-present Trenton H <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
import enum | ||
import logging | ||
from functools import lru_cache | ||
from typing import Dict | ||
from typing import Final | ||
from typing import Optional | ||
from typing import Union | ||
|
||
from gotenberg_client.base import BaseRoute | ||
from gotenberg_client.options import PageOrientation | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
# See https://github.com/psf/requests/issues/1081#issuecomment-428504128 | ||
class ForceMultipartDict(Dict): | ||
def __bool__(self) -> bool: | ||
return True | ||
|
||
|
||
FORCE_MULTIPART: Final = ForceMultipartDict() | ||
|
||
|
||
@enum.unique | ||
class PageOrientationOptions(enum.Enum): | ||
Landscape = enum.auto() | ||
Potrait = enum.auto() | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
if self.value == PageOrientationOptions.Landscape.value: | ||
return {"landscape": "true"} | ||
elif self.value == PageOrientationOptions.Potrait.value: | ||
return {"landscape": "false"} | ||
else: # pragma: no cover | ||
raise NotImplementedError(self.value) | ||
|
||
|
||
class ConvertBaseRoute(BaseRoute): | ||
def orient(self, orient: PageOrientationOptions) -> "BaseRoute": | ||
def orient(self, orient: PageOrientation) -> "BaseRoute": | ||
self._form_data.update(orient.to_form()) | ||
return self | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# SPDX-FileCopyrightText: 2023-present Trenton H <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
import dataclasses | ||
import enum | ||
from typing import Dict | ||
from typing import Optional | ||
from typing import Union | ||
|
||
from gotenberg_client.convert.common import optional_to_form | ||
|
||
|
||
@enum.unique | ||
class PdfAFormat(enum.Enum): | ||
A1a = enum.auto() | ||
A2b = enum.auto() | ||
A3b = enum.auto() | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
if self.value == PdfAFormat.A1a.value: | ||
return {"pdfFormat": "PDF/A-1a"} | ||
elif self.value == PdfAFormat.A2b.value: | ||
return {"pdfFormat": "PDF/A-2b"} | ||
elif self.value == PdfAFormat.A3b.value: | ||
return {"pdfFormat": "PDF/A-3b"} | ||
else: # pragma: no cover | ||
raise NotImplementedError(self.value) | ||
|
||
|
||
@enum.unique | ||
class PageOrientation(enum.Enum): | ||
Landscape = enum.auto() | ||
Potrait = enum.auto() | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
if self.value == PageOrientation.Landscape.value: | ||
return {"landscape": "true"} | ||
elif self.value == PageOrientation.Potrait.value: | ||
return {"landscape": "false"} | ||
else: # pragma: no cover | ||
raise NotImplementedError(self.value) | ||
|
||
|
||
@dataclasses.dataclass | ||
class PageSize: | ||
width: Optional[Union[float, int]] = None | ||
height: Optional[Union[float, int]] = None | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
data = optional_to_form(self.width, "paperWidth") | ||
data.update(optional_to_form(self.height, "paperHeight")) | ||
return data | ||
|
||
|
||
@dataclasses.dataclass | ||
class Margin: | ||
top: Optional[Union[float, int]] = None | ||
bottom: Optional[Union[float, int]] = None | ||
left: Optional[Union[float, int]] = None | ||
right: Optional[Union[float, int]] = None | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
data = optional_to_form(self.top, "marginTop") | ||
data.update(optional_to_form(self.bottom, "marginBottom")) | ||
data.update(optional_to_form(self.left, "marginLeft")) | ||
data.update(optional_to_form(self.right, "marginRight")) | ||
return data | ||
|
||
|
||
@enum.unique | ||
class EmulatedMediaType(str, enum.Enum): | ||
Print = enum.auto() | ||
Screen = enum.auto() | ||
|
||
def to_form(self) -> Dict[str, str]: | ||
if self.value == EmulatedMediaType.Print.value: | ||
return {"emulatedMediaType": "print"} | ||
elif self.value == EmulatedMediaType.Screen.value: | ||
return {"emulatedMediaType": "screen"} | ||
else: # pragma: no cover | ||
raise NotImplementedError(self.value) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.