Skip to content

Commit ed638af

Browse files
committed
[py]: use Zipfile in a context; remove duplicate imports, use compiled re pattern for searching
1 parent 2d8f51d commit ed638af

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

py/selenium/webdriver/firefox/firefox_profile.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,19 @@ def assume_untrusted_cert_issuer(self, value) -> None:
154154
self.set_preference("webdriver_assume_untrusted_issuer", value)
155155

156156
@property
157-
def encoded(self):
157+
def encoded(self) -> str:
158158
"""
159159
A zipped, base64 encoded string of profile directory
160160
for use with remote WebDriver JSON wire protocol
161161
"""
162162
self.update_preferences()
163163
fp = BytesIO()
164-
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
165-
path_root = len(self.path) + 1 # account for trailing slash
166-
for base, dirs, files in os.walk(self.path):
167-
for fyle in files:
168-
filename = os.path.join(base, fyle)
169-
zipped.write(filename, filename[path_root:])
170-
zipped.close()
164+
with zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED) as zipped:
165+
path_root = len(self.path) + 1 # account for trailing slash
166+
for base, _, files in os.walk(self.path):
167+
for fyle in files:
168+
filename = os.path.join(base, fyle)
169+
zipped.write(filename, filename[path_root:])
171170
return base64.b64encode(fp.getvalue()).decode('UTF-8')
172171

173172
def _create_tempfolder(self):
@@ -185,13 +184,12 @@ def _write_user_prefs(self, user_prefs):
185184
f.write(f'user_pref("{key}", {json.dumps(value)});\n')
186185

187186
def _read_existing_userjs(self, userjs):
188-
import warnings
189187

190-
PREF_RE = re.compile(r'user_pref\("(.*)",\s(.*)\)')
188+
pref_pattern = re.compile(r'user_pref\("(.*)",\s(.*)\)')
191189
try:
192190
with open(userjs, encoding='utf-8') as f:
193191
for usr in f:
194-
matches = re.search(PREF_RE, usr)
192+
matches = pref_pattern.search(usr)
195193
try:
196194
self.default_preferences[matches.group(1)] = json.loads(matches.group(2))
197195
except Exception:

py/selenium/webdriver/remote/remote_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def get_remote_connection_headers(cls, parsed_url, keep_alive=False):
127127
def _get_proxy_url(self):
128128
if self._url.startswith('https://'):
129129
return os.environ.get('https_proxy', os.environ.get('HTTPS_PROXY'))
130-
elif self._url.startswith('http://'):
130+
if self._url.startswith('http://'):
131131
return os.environ.get('http_proxy', os.environ.get('HTTP_PROXY'))
132132

133133
def _identify_http_proxy_auth(self):

0 commit comments

Comments
 (0)