Skip to content

Commit 669fbb3

Browse files
[py] Refactored Proxy object in common/proxy.py using custom descriptor. (#12286)
* [py] Moved all Proxytypes to custom descriptor * [py] Moved all Proxytypes to custom descriptor * fixed lynting error
1 parent 6002d14 commit 669fbb3

File tree

1 file changed

+183
-184
lines changed

1 file changed

+183
-184
lines changed

py/selenium/webdriver/common/proxy.py

Lines changed: 183 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ def load(cls, value):
5353
raise Exception(f"No proxy type is found for {value}")
5454

5555

56+
class _ProxyTypeDescriptor:
57+
def __init__(self, name, p_type):
58+
self.name = name
59+
self.p_type = p_type
60+
61+
def __get__(self, obj, cls):
62+
return getattr(obj, self.name)
63+
64+
def __set__(self, obj, value):
65+
if self.name == "autodetect" and not isinstance(value, bool):
66+
raise ValueError("Autodetect proxy value needs to be a boolean")
67+
getattr(obj, "_verify_proxy_type_compatibility")(self.p_type)
68+
setattr(obj, "proxyType", self.p_type)
69+
setattr(obj, self.name, value)
70+
71+
5672
class Proxy:
5773
"""Proxy contains information about proxy type and necessary proxy
5874
settings."""
@@ -69,6 +85,157 @@ class Proxy:
6985
socksPassword = ""
7086
socksVersion = None
7187

88+
# create descriptor type objects
89+
auto_detect = _ProxyTypeDescriptor("autodetect", ProxyType.AUTODETECT)
90+
"""Gets and Sets `auto_detect`
91+
92+
Usage
93+
-----
94+
- Get
95+
- `self.auto_detect`
96+
- Set
97+
- `self.auto_detect` = `value`
98+
99+
Parameters
100+
----------
101+
`value`: `str`
102+
"""
103+
104+
ftp_proxy = _ProxyTypeDescriptor("ftpProxy", ProxyType.MANUAL)
105+
"""Gets and Sets `ftp_proxy`
106+
107+
Usage
108+
-----
109+
- Get
110+
- `self.ftp_proxy`
111+
- Set
112+
- `self.ftp_proxy` = `value`
113+
114+
Parameters
115+
----------
116+
`value`: `str`
117+
"""
118+
119+
http_proxy = _ProxyTypeDescriptor("httpProxy", ProxyType.MANUAL)
120+
"""Gets and Sets `http_proxy`
121+
122+
Usage
123+
-----
124+
- Get
125+
- `self.http_proxy`
126+
- Set
127+
- `self.http_proxy` = `value`
128+
129+
Parameters
130+
----------
131+
`value`: `str`
132+
"""
133+
134+
no_proxy = _ProxyTypeDescriptor("noProxy", ProxyType.MANUAL)
135+
"""Gets and Sets `no_proxy`
136+
137+
Usage
138+
-----
139+
- Get
140+
- `self.no_proxy`
141+
- Set
142+
- `self.no_proxy` = `value`
143+
144+
Parameters
145+
----------
146+
`value`: `str`
147+
"""
148+
149+
proxy_autoconfig_url = _ProxyTypeDescriptor("proxyAutoconfigUrl", ProxyType.PAC)
150+
"""Gets and Sets `proxy_autoconfig_url`
151+
152+
Usage
153+
-----
154+
- Get
155+
- `self.proxy_autoconfig_url`
156+
- Set
157+
- `self.proxy_autoconfig_url` = `value`
158+
159+
Parameter
160+
---------
161+
`value`: `str`
162+
"""
163+
164+
ssl_proxy = _ProxyTypeDescriptor("sslProxy", ProxyType.MANUAL)
165+
"""Gets and Sets `ssl_proxy`
166+
167+
Usage
168+
-----
169+
- Get
170+
- `self.ssl_proxy`
171+
- Set
172+
- `self.ssl_proxy` = `value`
173+
174+
Parameter
175+
---------
176+
`value`: `str`
177+
"""
178+
179+
socks_proxy = _ProxyTypeDescriptor("socksProxy", ProxyType.MANUAL)
180+
"""Gets and Sets `socks_proxy`
181+
182+
Usage
183+
-----
184+
- Get
185+
- `self.sock_proxy`
186+
- Set
187+
- `self.socks_proxy` = `value`
188+
189+
Parameter
190+
---------
191+
`value`: `str`
192+
"""
193+
194+
socks_username = _ProxyTypeDescriptor("socksUsername", ProxyType.MANUAL)
195+
"""Gets and Sets `socks_password`
196+
197+
Usage
198+
-----
199+
- Get
200+
- `self.socks_password`
201+
- Set
202+
- `self.socks_password` = `value`
203+
204+
Parameter
205+
---------
206+
`value`: `str`
207+
"""
208+
209+
socks_password = _ProxyTypeDescriptor("socksPassword", ProxyType.MANUAL)
210+
"""Gets and Sets `socks_password`
211+
212+
Usage
213+
-----
214+
- Get
215+
- `self.socks_password`
216+
- Set
217+
- `self.socks_password` = `value`
218+
219+
Parameter
220+
---------
221+
`value`: `str`
222+
"""
223+
224+
socks_version = _ProxyTypeDescriptor("socksVersion", ProxyType.MANUAL)
225+
"""Gets and Sets `socks_version`
226+
227+
Usage
228+
-----
229+
- Get
230+
- `self.socks_version`
231+
- Set
232+
- `self.socks_version` = `value`
233+
234+
Parameter
235+
---------
236+
`value`: `str`
237+
"""
238+
72239
def __init__(self, raw=None):
73240
"""Creates a new Proxy.
74241
@@ -114,170 +281,6 @@ def proxy_type(self, value) -> None:
114281
self._verify_proxy_type_compatibility(value)
115282
self.proxyType = value
116283

117-
@property
118-
def auto_detect(self):
119-
"""Returns autodetect setting."""
120-
return self.autodetect
121-
122-
@auto_detect.setter
123-
def auto_detect(self, value) -> None:
124-
"""Sets autodetect setting.
125-
126-
:Args:
127-
- value: The autodetect value.
128-
"""
129-
if isinstance(value, bool):
130-
if self.autodetect is not value:
131-
self._verify_proxy_type_compatibility(ProxyType.AUTODETECT)
132-
self.proxyType = ProxyType.AUTODETECT
133-
self.autodetect = value
134-
else:
135-
raise ValueError("Autodetect proxy value needs to be a boolean")
136-
137-
@property
138-
def ftp_proxy(self):
139-
"""Returns ftp proxy setting."""
140-
return self.ftpProxy
141-
142-
@ftp_proxy.setter
143-
def ftp_proxy(self, value) -> None:
144-
"""Sets ftp proxy setting.
145-
146-
:Args:
147-
- value: The ftp proxy value.
148-
"""
149-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
150-
self.proxyType = ProxyType.MANUAL
151-
self.ftpProxy = value
152-
153-
@property
154-
def http_proxy(self):
155-
"""Returns http proxy setting."""
156-
return self.httpProxy
157-
158-
@http_proxy.setter
159-
def http_proxy(self, value) -> None:
160-
"""Sets http proxy setting.
161-
162-
:Args:
163-
- value: The http proxy value.
164-
"""
165-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
166-
self.proxyType = ProxyType.MANUAL
167-
self.httpProxy = value
168-
169-
@property
170-
def no_proxy(self):
171-
"""Returns noproxy setting."""
172-
return self.noProxy
173-
174-
@no_proxy.setter
175-
def no_proxy(self, value) -> None:
176-
"""Sets noproxy setting.
177-
178-
:Args:
179-
- value: The noproxy value.
180-
"""
181-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
182-
self.proxyType = ProxyType.MANUAL
183-
self.noProxy = value
184-
185-
@property
186-
def proxy_autoconfig_url(self):
187-
"""Returns proxy autoconfig url setting."""
188-
return self.proxyAutoconfigUrl
189-
190-
@proxy_autoconfig_url.setter
191-
def proxy_autoconfig_url(self, value) -> None:
192-
"""Sets proxy autoconfig url setting.
193-
194-
:Args:
195-
- value: The proxy autoconfig url value.
196-
"""
197-
self._verify_proxy_type_compatibility(ProxyType.PAC)
198-
self.proxyType = ProxyType.PAC
199-
self.proxyAutoconfigUrl = value
200-
201-
@property
202-
def ssl_proxy(self):
203-
"""Returns https proxy setting."""
204-
return self.sslProxy
205-
206-
@ssl_proxy.setter
207-
def ssl_proxy(self, value) -> None:
208-
"""Sets https proxy setting.
209-
210-
:Args:
211-
- value: The https proxy value.
212-
"""
213-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
214-
self.proxyType = ProxyType.MANUAL
215-
self.sslProxy = value
216-
217-
@property
218-
def socks_proxy(self):
219-
"""Returns socks proxy setting."""
220-
return self.socksProxy
221-
222-
@socks_proxy.setter
223-
def socks_proxy(self, value) -> None:
224-
"""Sets socks proxy setting.
225-
226-
:Args:
227-
- value: The socks proxy value.
228-
"""
229-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
230-
self.proxyType = ProxyType.MANUAL
231-
self.socksProxy = value
232-
233-
@property
234-
def socks_username(self):
235-
"""Returns socks proxy username setting."""
236-
return self.socksUsername
237-
238-
@socks_username.setter
239-
def socks_username(self, value) -> None:
240-
"""Sets socks proxy username setting.
241-
242-
:Args:
243-
- value: The socks proxy username value.
244-
"""
245-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
246-
self.proxyType = ProxyType.MANUAL
247-
self.socksUsername = value
248-
249-
@property
250-
def socks_password(self):
251-
"""Returns socks proxy password setting."""
252-
return self.socksPassword
253-
254-
@socks_password.setter
255-
def socks_password(self, value) -> None:
256-
"""Sets socks proxy password setting.
257-
258-
:Args:
259-
- value: The socks proxy password value.
260-
"""
261-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
262-
self.proxyType = ProxyType.MANUAL
263-
self.socksPassword = value
264-
265-
@property
266-
def socks_version(self):
267-
"""Returns socks proxy version setting."""
268-
return self.socksVersion
269-
270-
@socks_version.setter
271-
def socks_version(self, value) -> None:
272-
"""Sets socks proxy version setting.
273-
274-
:Args:
275-
- value: The socks proxy version value.
276-
"""
277-
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
278-
self.proxyType = ProxyType.MANUAL
279-
self.socksVersion = value
280-
281284
def _verify_proxy_type_compatibility(self, compatible_proxy):
282285
if self.proxyType not in (ProxyType.UNSPECIFIED, compatible_proxy):
283286
raise Exception(
@@ -286,24 +289,20 @@ def _verify_proxy_type_compatibility(self, compatible_proxy):
286289

287290
def to_capabilities(self):
288291
proxy_caps = {"proxyType": self.proxyType["string"].lower()}
289-
if self.autodetect:
290-
proxy_caps["autodetect"] = self.autodetect
291-
if self.ftpProxy:
292-
proxy_caps["ftpProxy"] = self.ftpProxy
293-
if self.httpProxy:
294-
proxy_caps["httpProxy"] = self.httpProxy
295-
if self.proxyAutoconfigUrl:
296-
proxy_caps["proxyAutoconfigUrl"] = self.proxyAutoconfigUrl
297-
if self.sslProxy:
298-
proxy_caps["sslProxy"] = self.sslProxy
299-
if self.noProxy:
300-
proxy_caps["noProxy"] = self.noProxy
301-
if self.socksProxy:
302-
proxy_caps["socksProxy"] = self.socksProxy
303-
if self.socksUsername:
304-
proxy_caps["socksUsername"] = self.socksUsername
305-
if self.socksPassword:
306-
proxy_caps["socksPassword"] = self.socksPassword
307-
if self.socksVersion:
308-
proxy_caps["socksVersion"] = self.socksVersion
292+
proxies = [
293+
"autodetect",
294+
"ftpProxy",
295+
"httpProxy",
296+
"proxyAutoconfigUrl",
297+
"sslProxy",
298+
"noProxy",
299+
"socksProxy",
300+
"socksUsername",
301+
"socksPassword",
302+
"socksVersion",
303+
]
304+
for proxy in proxies:
305+
attr_value = getattr(self, proxy)
306+
if attr_value:
307+
proxy_caps[proxy] = attr_value
309308
return proxy_caps

0 commit comments

Comments
 (0)