Skip to content

Commit

Permalink
[py]: simplify uses of unnecessary elif throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Oct 5, 2022
1 parent 75ba99b commit 086dfe7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _get_connection_manager(self):
from urllib3.contrib.socks import SOCKSProxyManager

return SOCKSProxyManager(self._proxy_url, **pool_manager_init_args)
elif self._identify_http_proxy_auth():
if self._identify_http_proxy_auth():
self._proxy_url, self._basic_proxy_auth = self._separate_http_proxy_auth()
pool_manager_init_args["proxy_headers"] = urllib3.make_headers(proxy_basic_auth=self._basic_proxy_auth)
return urllib3.ProxyManager(self._proxy_url, **pool_manager_init_args)
Expand Down
23 changes: 10 additions & 13 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,13 @@ def _wrap_value(self, value):
for key, val in value.items():
converted[key] = self._wrap_value(val)
return converted
elif isinstance(value, self._web_element_cls):
if isinstance(value, self._web_element_cls):
return {"element-6066-11e4-a52e-4f735466cecf": value.id}
elif isinstance(value, self._shadowroot_cls):
if isinstance(value, self._shadowroot_cls):
return {"shadow-6066-11e4-a52e-4f735466cecf": value.id}
elif isinstance(value, list):
if isinstance(value, list):
return list(self._wrap_value(item) for item in value)
else:
return value
return value

def create_web_element(self, element_id: str) -> WebElement:
"""Creates a web element with the specified `element_id`."""
Expand All @@ -412,16 +411,14 @@ def _unwrap_value(self, value):
if isinstance(value, dict):
if "element-6066-11e4-a52e-4f735466cecf" in value:
return self.create_web_element(value["element-6066-11e4-a52e-4f735466cecf"])
elif "shadow-6066-11e4-a52e-4f735466cecf" in value:
if "shadow-6066-11e4-a52e-4f735466cecf" in value:
return self._shadowroot_cls(self, value["shadow-6066-11e4-a52e-4f735466cecf"])
else:
for key, val in value.items():
value[key] = self._unwrap_value(val)
return value
elif isinstance(value, list):
return list(self._unwrap_value(item) for item in value)
else:
for key, val in value.items():
value[key] = self._unwrap_value(val)
return value
if isinstance(value, list):
return list(self._unwrap_value(item) for item in value)
return value

def execute(self, driver_command: str, params: dict = None) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/safari/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Service(service.Service):
"""A Service class that is responsible for the starting and stopping
of `safaridriver` This is only supported on MAC OSX.
:param executable_path: install path of the chromedriver executable, defaults to `/usr/bin/safaridriver`.
:param executable_path: install path of the safaridriver executable, defaults to `/usr/bin/safaridriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param quiet: Suppress driver stdout & stderr, redirects to os.devnull if enabled.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
Expand Down
24 changes: 11 additions & 13 deletions py/selenium/webdriver/support/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,25 @@ def groups(self) -> Sequence[str]:

if m.match(RGB_PATTERN, str_):
return cls(*m.groups)
elif m.match(RGB_PCT_PATTERN, str_):
if m.match(RGB_PCT_PATTERN, str_):
rgb = tuple(float(each) / 100 * 255 for each in m.groups)
return cls(*rgb)
elif m.match(RGBA_PATTERN, str_):
if m.match(RGBA_PATTERN, str_):
return cls(*m.groups)
elif m.match(RGBA_PCT_PATTERN, str_):
if m.match(RGBA_PCT_PATTERN, str_):
rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]]) # type: ignore
return cls(*rgba)
elif m.match(HEX_PATTERN, str_):
if m.match(HEX_PATTERN, str_):
rgb = tuple(int(each, 16) for each in m.groups)
return cls(*rgb)
elif m.match(HEX3_PATTERN, str_):
if m.match(HEX3_PATTERN, str_):
rgb = tuple(int(each * 2, 16) for each in m.groups)
return cls(*rgb)
elif m.match(HSL_PATTERN, str_) or m.match(HSLA_PATTERN, str_):
if m.match(HSL_PATTERN, str_) or m.match(HSLA_PATTERN, str_):
return cls._from_hsl(*m.groups)
elif str_.upper() in Colors:
if str_.upper() in Colors:
return Colors[str_.upper()]
else:
raise ValueError("Could not convert %s into color" % str_)
raise ValueError("Could not convert %s into color" % str_)

@classmethod
def _from_hsl(cls, h: ParseableFloat, s: ParseableFloat, light: ParseableFloat, a: ParseableFloat = 1) -> Color:
Expand All @@ -131,12 +130,11 @@ def hue_to_rgb(lum1: float, lum2: float, hue: float) -> float:

if hue < 1.0 / 6.0:
return lum1 + (lum2 - lum1) * 6.0 * hue
elif hue < 1.0 / 2.0:
if hue < 1.0 / 2.0:
return lum2
elif hue < 2.0 / 3.0:
if hue < 2.0 / 3.0:
return lum1 + (lum2 - lum1) * ((2.0 / 3.0) - hue) * 6.0
else:
return lum1
return lum1

r = hue_to_rgb(luminocity1, luminocity2, h + 1.0 / 3.0)
g = hue_to_rgb(luminocity1, luminocity2, h)
Expand Down
15 changes: 6 additions & 9 deletions py/selenium/webdriver/support/event_firing_webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ def _wrap_elements(result, ef_driver):
# handle the case if another wrapper wraps EventFiringWebElement
if isinstance(result, EventFiringWebElement):
return result
elif isinstance(result, WebElement):
if isinstance(result, WebElement):
return EventFiringWebElement(result, ef_driver)
elif isinstance(result, list):
if isinstance(result, list):
return [_wrap_elements(item, ef_driver) for item in result]
# result is a built in type.
else:
return result
return result


class EventFiringWebDriver:
Expand Down Expand Up @@ -124,12 +122,11 @@ def _dispatch(
def _unwrap_element_args(self, args):
if isinstance(args, EventFiringWebElement):
return args.wrapped_element
elif isinstance(args, tuple):
if isinstance(args, tuple):
return tuple(self._unwrap_element_args(item) for item in args)
elif isinstance(args, list):
if isinstance(args, list):
return [self._unwrap_element_args(item) for item in args]
else:
return args
return args

def _wrap_value(self, value):
if isinstance(value, EventFiringWebElement):
Expand Down

0 comments on commit 086dfe7

Please sign in to comment.