Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] Refactored Proxy object in common/proxy.py using custom descriptor. #12286

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 183 additions & 184 deletions py/selenium/webdriver/common/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ def load(cls, value):
raise Exception(f"No proxy type is found for {value}")


class _ProxyTypeDescriptor:
def __init__(self, name, p_type):
self.name = name
self.p_type = p_type

def __get__(self, obj, cls):
return getattr(obj, self.name)

def __set__(self, obj, value):
if self.name == "autodetect" and not isinstance(value, bool):
raise ValueError("Autodetect proxy value needs to be a boolean")
getattr(obj, "_verify_proxy_type_compatibility")(self.p_type)
setattr(obj, "proxyType", self.p_type)
setattr(obj, self.name, value)


class Proxy:
"""Proxy contains information about proxy type and necessary proxy
settings."""
Expand All @@ -69,6 +85,157 @@ class Proxy:
socksPassword = ""
socksVersion = None

# create descriptor type objects
auto_detect = _ProxyTypeDescriptor("autodetect", ProxyType.AUTODETECT)
"""Gets and Sets `auto_detect`

Usage
-----
- Get
- `self.auto_detect`
- Set
- `self.auto_detect` = `value`

Parameters
----------
`value`: `str`
"""

ftp_proxy = _ProxyTypeDescriptor("ftpProxy", ProxyType.MANUAL)
"""Gets and Sets `ftp_proxy`

Usage
-----
- Get
- `self.ftp_proxy`
- Set
- `self.ftp_proxy` = `value`

Parameters
----------
`value`: `str`
"""

http_proxy = _ProxyTypeDescriptor("httpProxy", ProxyType.MANUAL)
"""Gets and Sets `http_proxy`

Usage
-----
- Get
- `self.http_proxy`
- Set
- `self.http_proxy` = `value`

Parameters
----------
`value`: `str`
"""

no_proxy = _ProxyTypeDescriptor("noProxy", ProxyType.MANUAL)
"""Gets and Sets `no_proxy`

Usage
-----
- Get
- `self.no_proxy`
- Set
- `self.no_proxy` = `value`

Parameters
----------
`value`: `str`
"""

proxy_autoconfig_url = _ProxyTypeDescriptor("proxyAutoconfigUrl", ProxyType.PAC)
"""Gets and Sets `proxy_autoconfig_url`

Usage
-----
- Get
- `self.proxy_autoconfig_url`
- Set
- `self.proxy_autoconfig_url` = `value`

Parameter
---------
`value`: `str`
"""

ssl_proxy = _ProxyTypeDescriptor("sslProxy", ProxyType.MANUAL)
"""Gets and Sets `ssl_proxy`

Usage
-----
- Get
- `self.ssl_proxy`
- Set
- `self.ssl_proxy` = `value`

Parameter
---------
`value`: `str`
"""

socks_proxy = _ProxyTypeDescriptor("socksProxy", ProxyType.MANUAL)
"""Gets and Sets `socks_proxy`

Usage
-----
- Get
- `self.sock_proxy`
- Set
- `self.socks_proxy` = `value`

Parameter
---------
`value`: `str`
"""

socks_username = _ProxyTypeDescriptor("socksUsername", ProxyType.MANUAL)
"""Gets and Sets `socks_password`

Usage
-----
- Get
- `self.socks_password`
- Set
- `self.socks_password` = `value`

Parameter
---------
`value`: `str`
"""

socks_password = _ProxyTypeDescriptor("socksPassword", ProxyType.MANUAL)
"""Gets and Sets `socks_password`

Usage
-----
- Get
- `self.socks_password`
- Set
- `self.socks_password` = `value`

Parameter
---------
`value`: `str`
"""

socks_version = _ProxyTypeDescriptor("socksVersion", ProxyType.MANUAL)
"""Gets and Sets `socks_version`

Usage
-----
- Get
- `self.socks_version`
- Set
- `self.socks_version` = `value`

Parameter
---------
`value`: `str`
"""

def __init__(self, raw=None):
"""Creates a new Proxy.

Expand Down Expand Up @@ -114,170 +281,6 @@ def proxy_type(self, value) -> None:
self._verify_proxy_type_compatibility(value)
self.proxyType = value

@property
def auto_detect(self):
"""Returns autodetect setting."""
return self.autodetect

@auto_detect.setter
def auto_detect(self, value) -> None:
"""Sets autodetect setting.

:Args:
- value: The autodetect value.
"""
if isinstance(value, bool):
if self.autodetect is not value:
self._verify_proxy_type_compatibility(ProxyType.AUTODETECT)
self.proxyType = ProxyType.AUTODETECT
self.autodetect = value
else:
raise ValueError("Autodetect proxy value needs to be a boolean")

@property
def ftp_proxy(self):
"""Returns ftp proxy setting."""
return self.ftpProxy

@ftp_proxy.setter
def ftp_proxy(self, value) -> None:
"""Sets ftp proxy setting.

:Args:
- value: The ftp proxy value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.ftpProxy = value

@property
def http_proxy(self):
"""Returns http proxy setting."""
return self.httpProxy

@http_proxy.setter
def http_proxy(self, value) -> None:
"""Sets http proxy setting.

:Args:
- value: The http proxy value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.httpProxy = value

@property
def no_proxy(self):
"""Returns noproxy setting."""
return self.noProxy

@no_proxy.setter
def no_proxy(self, value) -> None:
"""Sets noproxy setting.

:Args:
- value: The noproxy value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.noProxy = value

@property
def proxy_autoconfig_url(self):
"""Returns proxy autoconfig url setting."""
return self.proxyAutoconfigUrl

@proxy_autoconfig_url.setter
def proxy_autoconfig_url(self, value) -> None:
"""Sets proxy autoconfig url setting.

:Args:
- value: The proxy autoconfig url value.
"""
self._verify_proxy_type_compatibility(ProxyType.PAC)
self.proxyType = ProxyType.PAC
self.proxyAutoconfigUrl = value

@property
def ssl_proxy(self):
"""Returns https proxy setting."""
return self.sslProxy

@ssl_proxy.setter
def ssl_proxy(self, value) -> None:
"""Sets https proxy setting.

:Args:
- value: The https proxy value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.sslProxy = value

@property
def socks_proxy(self):
"""Returns socks proxy setting."""
return self.socksProxy

@socks_proxy.setter
def socks_proxy(self, value) -> None:
"""Sets socks proxy setting.

:Args:
- value: The socks proxy value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.socksProxy = value

@property
def socks_username(self):
"""Returns socks proxy username setting."""
return self.socksUsername

@socks_username.setter
def socks_username(self, value) -> None:
"""Sets socks proxy username setting.

:Args:
- value: The socks proxy username value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.socksUsername = value

@property
def socks_password(self):
"""Returns socks proxy password setting."""
return self.socksPassword

@socks_password.setter
def socks_password(self, value) -> None:
"""Sets socks proxy password setting.

:Args:
- value: The socks proxy password value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.socksPassword = value

@property
def socks_version(self):
"""Returns socks proxy version setting."""
return self.socksVersion

@socks_version.setter
def socks_version(self, value) -> None:
"""Sets socks proxy version setting.

:Args:
- value: The socks proxy version value.
"""
self._verify_proxy_type_compatibility(ProxyType.MANUAL)
self.proxyType = ProxyType.MANUAL
self.socksVersion = value

def _verify_proxy_type_compatibility(self, compatible_proxy):
if self.proxyType not in (ProxyType.UNSPECIFIED, compatible_proxy):
raise Exception(
Expand All @@ -286,24 +289,20 @@ def _verify_proxy_type_compatibility(self, compatible_proxy):

def to_capabilities(self):
proxy_caps = {"proxyType": self.proxyType["string"].lower()}
if self.autodetect:
proxy_caps["autodetect"] = self.autodetect
if self.ftpProxy:
proxy_caps["ftpProxy"] = self.ftpProxy
if self.httpProxy:
proxy_caps["httpProxy"] = self.httpProxy
if self.proxyAutoconfigUrl:
proxy_caps["proxyAutoconfigUrl"] = self.proxyAutoconfigUrl
if self.sslProxy:
proxy_caps["sslProxy"] = self.sslProxy
if self.noProxy:
proxy_caps["noProxy"] = self.noProxy
if self.socksProxy:
proxy_caps["socksProxy"] = self.socksProxy
if self.socksUsername:
proxy_caps["socksUsername"] = self.socksUsername
if self.socksPassword:
proxy_caps["socksPassword"] = self.socksPassword
if self.socksVersion:
proxy_caps["socksVersion"] = self.socksVersion
proxies = [
"autodetect",
"ftpProxy",
"httpProxy",
"proxyAutoconfigUrl",
"sslProxy",
"noProxy",
"socksProxy",
"socksUsername",
"socksPassword",
"socksVersion",
]
for proxy in proxies:
attr_value = getattr(self, proxy)
if attr_value:
proxy_caps[proxy] = attr_value
return proxy_caps
2 changes: 1 addition & 1 deletion py/selenium/webdriver/ie/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,4 @@ def to_capabilities(self) -> dict:

@property
def default_capabilities(self) -> dict:
return DesiredCapabilities.INTERNETEXPLORER.copy()
return DesiredCapabilities.INTERNETEXPLORER.copy()