File: print_page_options.py

package info (click to toggle)
python-selenium 4.8.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,348 kB
  • sloc: python: 14,352; javascript: 2,347; makefile: 118; sh: 48
file content (290 lines) | stat: -rw-r--r-- 8,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.


import sys
from typing import TYPE_CHECKING
from typing import List
from typing import Optional

# necessary to support types for Python 3.7
if TYPE_CHECKING:
    if sys.version_info >= (3, 8):
        from typing import Literal
        from typing import TypedDict
    else:
        from typing_extensions import Literal
        from typing_extensions import TypedDict

    Orientation = Literal["portrait", "landscape"]

    class _MarginOpts(TypedDict, total=False):
        left: float
        right: float
        top: float
        bottom: float

    class _PageOpts(TypedDict, total=False):
        width: float
        height: float

    class _PrintOpts(TypedDict, total=False):
        margin: _MarginOpts
        page: _PageOpts
        background: bool
        orientation: Orientation
        scale: float
        shrinkToFit: bool
        pageRanges: List[str]

else:
    from typing import Any
    from typing import Dict

    Orientation = str
    _MarginOpts = _PageOpts = _PrintOpts = Dict[str, Any]


class PrintOptions:
    ORIENTATION_VALUES = ["portrait", "landscape"]

    def __init__(self) -> None:
        self._print_options: _PrintOpts = {}
        self._page: _PageOpts = {}
        self._margin: _MarginOpts = {}

    def to_dict(self) -> _PrintOpts:
        """
        :Returns: A hash of print options configured
        """
        return self._print_options

    @property
    def orientation(self) -> Optional[Orientation]:
        """
        :Returns: Orientation that was set for the page
        """
        return self._print_options.get("orientation", None)

    @orientation.setter
    def orientation(self, value: Orientation) -> None:
        """
        Allows you to set orientation of the page
        :Args:
         - value: Either portrait or landscape
        """
        if value not in self.ORIENTATION_VALUES:
            raise ValueError(f"Orientation value must be one of {self.ORIENTATION_VALUES}")

        self._print_options["orientation"] = value

    @property
    def scale(self) -> Optional[float]:
        """
        :Returns: Scale that was set for the page
        """
        return self._print_options.get("scale", None)

    @scale.setter
    def scale(self, value: float) -> None:
        """
        Allows you to to set scale for the page
        :Args:
         - value: integer or float between 0.1 and 2
        """
        self.__validate_num_property("Scale", value)

        if value < 0.1 or value > 2:
            raise ValueError("Scale value should be between 0.1 and 2")

        self._print_options["scale"] = value

    @property
    def background(self) -> Optional[bool]:
        """
        :Returns: Background value that was set
        """
        return self._print_options.get("background", None)

    @background.setter
    def background(self, value: bool) -> None:
        """
        Allows you to set the boolean value for the background
        :Args:
         - value: Boolean
        """
        if not isinstance(value, bool):
            raise ValueError("Set background value should be a boolean")
        self._print_options["background"] = value

    @property
    def page_width(self) -> Optional[float]:
        """
        :Returns: Page width that was set
        """
        return self._page.get("width", None)

    @page_width.setter
    def page_width(self, value: float) -> None:
        """
        Allows you to set width of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Page Width", value)

        self._page["width"] = value
        self._print_options["page"] = self._page

    @property
    def page_height(self) -> Optional[float]:
        """
        :Returns: Page height that was set
        """
        return self._page.get("height", None)

    @page_height.setter
    def page_height(self, value: float) -> None:
        """
        Allows you to set height of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Page Height", value)

        self._page["height"] = value
        self._print_options["page"] = self._page

    @property
    def margin_top(self) -> Optional[float]:
        """
        :Returns: Top margin of the page
        """
        return self._margin.get("top", None)

    @margin_top.setter
    def margin_top(self, value: float) -> None:
        """
        Allows you to set top margin of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Margin top", value)

        self._margin["top"] = value
        self._print_options["margin"] = self._margin

    @property
    def margin_left(self) -> Optional[float]:
        """
        :Returns: Left margin of the page
        """
        return self._margin.get("left", None)

    @margin_left.setter
    def margin_left(self, value: float) -> None:
        """
        Allows you to set left margin of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Margin left", value)

        self._margin["left"] = value
        self._print_options["margin"] = self._margin

    @property
    def margin_bottom(self) -> Optional[float]:
        """
        :Returns: Bottom margin of the page
        """
        return self._margin.get("bottom", None)

    @margin_bottom.setter
    def margin_bottom(self, value: float) -> None:
        """
        Allows you to set bottom margin of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Margin bottom", value)

        self._margin["bottom"] = value
        self._print_options["margin"] = self._margin

    @property
    def margin_right(self) -> Optional[float]:
        """
        :Returns: Right margin of the page
        """
        return self._margin.get("right", None)

    @margin_right.setter
    def margin_right(self, value: float) -> None:
        """
        Allows you to set right margin of the page
        :Args:
         - value: A positive integer or float
        """
        self.__validate_num_property("Margin right", value)

        self._margin["right"] = value
        self._print_options["margin"] = self._margin

    @property
    def shrink_to_fit(self) -> Optional[bool]:
        """
        :Returns: Value set for shrinkToFit
        """
        return self._print_options.get("shrinkToFit", None)

    @shrink_to_fit.setter
    def shrink_to_fit(self, value: bool) -> None:
        """
        Allows you to set shrinkToFit
        :Args:
         - value: Boolean
        """
        if not isinstance(value, bool):
            raise ValueError("Set shrink to fit value should be a boolean")
        self._print_options["shrinkToFit"] = value

    @property
    def page_ranges(self) -> Optional[List[str]]:
        """
        :Returns: value set for pageRanges
        """
        return self._print_options.get("pageRanges", None)

    @page_ranges.setter
    def page_ranges(self, value: List[str]) -> None:
        """
        Allows you to set pageRanges for the print command
        :Args:
         - value: A list of page ranges. Eg: ['1-2']
        """
        if not isinstance(value, list):
            raise ValueError("Page ranges should be a list")
        self._print_options["pageRanges"] = value

    def __validate_num_property(self, property_name: str, value: float) -> None:
        """Helper function to validate some of the properties."""
        if not isinstance(value, (int, float)):
            raise ValueError(f"{property_name} should be an integer or a float")

        if value < 0:
            raise ValueError(f"{property_name} cannot be less then 0")