15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
18
-
19
- class PrintOptions ():
18
+ import sys
19
+ from typing import TYPE_CHECKING , List , Optional
20
+
21
+ # necessary to support types for Python 3.7
22
+ if TYPE_CHECKING :
23
+ if sys .version_info >= (3 , 8 ):
24
+ from typing import Literal , TypedDict
25
+ else :
26
+ from typing_extensions import Literal , TypedDict
27
+
28
+ Orientation = Literal ['portrait' , 'landscape' ]
29
+
30
+ class _MarginOpts (TypedDict , total = False ):
31
+ left : float
32
+ right : float
33
+ top : float
34
+ bottom : float
35
+
36
+ class _PageOpts (TypedDict , total = False ):
37
+ width : float
38
+ height : float
39
+
40
+ class _PrintOpts (TypedDict , total = False ):
41
+ margin : _MarginOpts
42
+ page : _PageOpts
43
+ background : bool
44
+ orientation : Orientation
45
+ scale : float
46
+ shrinkToFit : bool
47
+ pageRanges : List [str ]
48
+ else :
49
+ from typing import Any , Dict
50
+
51
+ Orientation = str
52
+ _MarginOpts = _PageOpts = _PrintOpts = Dict [str , Any ]
53
+
54
+
55
+ class PrintOptions :
20
56
ORIENTATION_VALUES = ['portrait' , 'landscape' ]
21
57
22
- def __init__ (self ):
23
- self ._print_options = {}
24
- self ._page = {}
25
- self ._margin = {}
58
+ def __init__ (self ) -> None :
59
+ self ._print_options : _PrintOpts = {}
60
+ self ._page : _PageOpts = {}
61
+ self ._margin : _MarginOpts = {}
26
62
27
- def to_dict (self ):
63
+ def to_dict (self ) -> _PrintOpts :
28
64
"""
29
65
:Returns: A hash of print options configured
30
66
"""
31
67
return self ._print_options
32
68
33
69
@property
34
- def orientation (self ):
70
+ def orientation (self ) -> Optional [ Orientation ] :
35
71
"""
36
72
:Returns: Orientation that was set for the page
37
73
"""
38
74
return self ._print_options .get ('orientation' , None )
39
75
40
76
@orientation .setter
41
- def orientation (self , value ) :
77
+ def orientation (self , value : Orientation ) -> None :
42
78
"""
43
79
Allows you to set orientation of the page
44
80
:Args:
@@ -50,14 +86,14 @@ def orientation(self, value):
50
86
self ._print_options ['orientation' ] = value
51
87
52
88
@property
53
- def scale (self ):
89
+ def scale (self ) -> Optional [ float ] :
54
90
"""
55
91
:Returns: Scale that was set for the page
56
92
"""
57
93
return self ._print_options .get ('scale' , None )
58
94
59
95
@scale .setter
60
- def scale (self , value ) :
96
+ def scale (self , value : float ) -> None :
61
97
"""
62
98
Allows you to to set scale for the page
63
99
:Args:
@@ -71,14 +107,14 @@ def scale(self, value):
71
107
self ._print_options ['scale' ] = value
72
108
73
109
@property
74
- def background (self ):
110
+ def background (self ) -> Optional [ bool ] :
75
111
"""
76
112
:Returns: Background value that was set
77
113
"""
78
114
return self ._print_options .get ('background' , None )
79
115
80
116
@background .setter
81
- def background (self , value ) :
117
+ def background (self , value : bool ) -> None :
82
118
"""
83
119
Allows you to set the boolean value for the background
84
120
:Args:
@@ -89,14 +125,14 @@ def background(self, value):
89
125
self ._print_options ['background' ] = value
90
126
91
127
@property
92
- def page_width (self ):
128
+ def page_width (self ) -> Optional [ float ] :
93
129
"""
94
130
:Returns: Page width that was set
95
131
"""
96
132
return self ._page .get ('width' , None )
97
133
98
134
@page_width .setter
99
- def page_width (self , value ) :
135
+ def page_width (self , value : float ) -> None :
100
136
"""
101
137
Allows you to set width of the page
102
138
:Args:
@@ -108,14 +144,14 @@ def page_width(self, value):
108
144
self ._print_options ['page' ] = self ._page
109
145
110
146
@property
111
- def page_height (self ):
147
+ def page_height (self ) -> Optional [ float ] :
112
148
"""
113
149
:Returns: Page height that was set
114
150
"""
115
151
return self ._page .get ('height' , None )
116
152
117
153
@page_height .setter
118
- def page_height (self , value ) :
154
+ def page_height (self , value : float ) -> None :
119
155
"""
120
156
Allows you to set height of the page
121
157
:Args:
@@ -127,14 +163,14 @@ def page_height(self, value):
127
163
self ._print_options ['page' ] = self ._page
128
164
129
165
@property
130
- def margin_top (self ):
166
+ def margin_top (self ) -> Optional [ float ] :
131
167
"""
132
168
:Returns: Top margin of the page
133
169
"""
134
170
return self ._margin .get ('top' , None )
135
171
136
172
@margin_top .setter
137
- def margin_top (self , value ) :
173
+ def margin_top (self , value : float ) -> None :
138
174
"""
139
175
Allows you to set top margin of the page
140
176
:Args:
@@ -146,14 +182,14 @@ def margin_top(self, value):
146
182
self ._print_options ['margin' ] = self ._margin
147
183
148
184
@property
149
- def margin_left (self ):
185
+ def margin_left (self ) -> Optional [ float ] :
150
186
"""
151
187
:Returns: Left margin of the page
152
188
"""
153
189
return self ._margin .get ('left' , None )
154
190
155
191
@margin_left .setter
156
- def margin_left (self , value ) :
192
+ def margin_left (self , value : float ) -> None :
157
193
"""
158
194
Allows you to set left margin of the page
159
195
:Args:
@@ -165,14 +201,14 @@ def margin_left(self, value):
165
201
self ._print_options ['margin' ] = self ._margin
166
202
167
203
@property
168
- def margin_bottom (self ):
204
+ def margin_bottom (self ) -> Optional [ float ] :
169
205
"""
170
206
:Returns: Bottom margin of the page
171
207
"""
172
208
return self ._margin .get ('bottom' , None )
173
209
174
210
@margin_bottom .setter
175
- def margin_bottom (self , value ) :
211
+ def margin_bottom (self , value : float ) -> None :
176
212
"""
177
213
Allows you to set bottom margin of the page
178
214
:Args:
@@ -184,14 +220,14 @@ def margin_bottom(self, value):
184
220
self ._print_options ['margin' ] = self ._margin
185
221
186
222
@property
187
- def margin_right (self ):
223
+ def margin_right (self ) -> Optional [ float ] :
188
224
"""
189
225
:Returns: Right margin of the page
190
226
"""
191
227
return self ._margin .get ('right' , None )
192
228
193
229
@margin_right .setter
194
- def margin_right (self , value ) :
230
+ def margin_right (self , value : float ) -> None :
195
231
"""
196
232
Allows you to set right margin of the page
197
233
:Args:
@@ -203,14 +239,14 @@ def margin_right(self, value):
203
239
self ._print_options ['margin' ] = self ._margin
204
240
205
241
@property
206
- def shrink_to_fit (self ):
242
+ def shrink_to_fit (self ) -> Optional [ bool ] :
207
243
"""
208
244
:Returns: Value set for shrinkToFit
209
245
"""
210
246
return self ._print_options .get ('shrinkToFit' , None )
211
247
212
248
@shrink_to_fit .setter
213
- def shrink_to_fit (self , value ) :
249
+ def shrink_to_fit (self , value : bool ) -> None :
214
250
"""
215
251
Allows you to set shrinkToFit
216
252
:Args:
@@ -221,14 +257,14 @@ def shrink_to_fit(self, value):
221
257
self ._print_options ['shrinkToFit' ] = value
222
258
223
259
@property
224
- def page_ranges (self ):
260
+ def page_ranges (self ) -> Optional [ List [ str ]] :
225
261
"""
226
262
:Returns: value set for pageRanges
227
263
"""
228
264
return self ._print_options .get ('pageRanges' , None )
229
265
230
266
@page_ranges .setter
231
- def page_ranges (self , value ) :
267
+ def page_ranges (self , value : List [ str ]) -> None :
232
268
"""
233
269
Allows you to set pageRanges for the print command
234
270
:Args:
@@ -238,11 +274,11 @@ def page_ranges(self, value):
238
274
raise ValueError ('Page ranges should be a list' )
239
275
self ._print_options ['pageRanges' ] = value
240
276
241
- def __validate_num_property (self , property_name , value ) :
277
+ def __validate_num_property (self , property_name : str , value : float ) -> None :
242
278
"""
243
279
Helper function to validate some of the properties
244
280
"""
245
- if not isinstance (value , float ) and not isinstance ( value , int ):
281
+ if not isinstance (value , ( int , float ) ):
246
282
raise ValueError (f'{ property_name } should be an integer or a float' )
247
283
248
284
if value < 0 :
0 commit comments