Skip to content

Commit 886490e

Browse files
authored
Merge pull request #374 from Webperf-se/issue-370
pylint
2 parents d5b4697 + c6d40a3 commit 886490e

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

tests/utils.py

+10-34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import os
1414
from urllib.parse import ParseResult, urlparse, urlunparse
15+
import gettext
1516
import requests
1617
import IP2Location
1718
import dns
@@ -20,7 +21,6 @@
2021
import dns.dnssec
2122
import dns.exception
2223
import dns.name
23-
import gettext
2424

2525
def get_config_or_default(name):
2626
"""
@@ -79,10 +79,8 @@ def get_translation(module_name, lang_code):
7979
Returns:
8080
function: The gettext() function for the specified language.
8181
"""
82-
language = gettext.translation(
83-
module_name,
84-
localedir='locales',
85-
languages=[lang_code])
82+
language = gettext.translation(module_name,
83+
localedir='locales', languages=[lang_code])
8684
return language.gettext
8785

8886

@@ -110,14 +108,9 @@ def change_url_to_test_url(url, test_name):
110108
else:
111109
new_query = f'webperf-core={test_name}&' + o.query
112110
o2 = ParseResult(
113-
scheme=o.scheme,
114-
netloc=o.netloc,
115-
path=o.path,
116-
params=o.params,
117-
query=new_query,
118-
fragment=o.fragment)
119-
url2 = urlunparse(o2)
120-
return url2
111+
scheme=o.scheme, netloc=o.netloc, path=o.path,
112+
params=o.params, query=new_query, fragment=o.fragment)
113+
return urlunparse(o2)
121114

122115

123116
def is_file_older_than(file, delta):
@@ -173,8 +166,7 @@ def get_cache_path_for_rule(url, cache_key_rule):
173166
if not os.path.exists(hostname_path):
174167
os.makedirs(hostname_path)
175168

176-
cache_key = cache_key_rule.format(
177-
hashlib.sha512(url.encode()).hexdigest())
169+
cache_key = cache_key_rule.format(hashlib.sha512(url.encode()).hexdigest())
178170
cache_path = os.path.join(folder, hostname, cache_key)
179171

180172
return cache_path
@@ -191,9 +183,7 @@ def get_cache_path_for_folder(url):
191183
Returns:
192184
str: The generated cache path.
193185
"""
194-
195186
cache_key_rule = '{0}'
196-
197187
return get_cache_path_for_rule(url, cache_key_rule)
198188

199189

@@ -212,15 +202,12 @@ def get_cache_path_for_file(url, use_text_instead_of_content):
212202
Returns:
213203
str: The generated cache path.
214204
"""
215-
216205
file_ending = '.tmp'
217206
if USE_CACHE:
218207
file_ending = '.cache'
219-
220208
cache_key_rule = '{0}.txt.utf-8' + file_ending
221209
if not use_text_instead_of_content:
222210
cache_key_rule = '{0}.bytes' + file_ending
223-
224211
return get_cache_path_for_rule(url, cache_key_rule)
225212

226213

@@ -243,17 +230,15 @@ def get_cache_file(url, use_text_instead_of_content, time_delta):
243230
If the cache file does not exist or is too old, None is returned.
244231
245232
Notes:
246-
- The function uses the get_cache_path_for_file function to determine the path of the cache file.
233+
- The function uses the get_cache_path_for_file function
234+
to determine the path of the cache file.
247235
- If USE_CACHE is False, the function always returns None.
248236
"""
249237
cache_path = get_cache_path_for_file(url, use_text_instead_of_content)
250-
251238
if not os.path.exists(cache_path):
252239
return None
253-
254240
if USE_CACHE and is_file_older_than(cache_path, time_delta):
255241
return None
256-
257242
if use_text_instead_of_content:
258243
with open(cache_path, 'r', encoding='utf-8', newline='') as file:
259244
return '\n'.join(file.readlines())
@@ -277,13 +262,10 @@ def has_cache_file(url, use_text_instead_of_content, time_delta):
277262
False otherwise.
278263
"""
279264
cache_path = get_cache_path_for_file(url, use_text_instead_of_content)
280-
281265
if not os.path.exists(cache_path):
282266
return False
283-
284267
if USE_CACHE and is_file_older_than(cache_path, time_delta):
285268
return False
286-
287269
return True
288270

289271

@@ -316,18 +298,15 @@ def clean_cache_files():
316298
if os.path.exists(base_directory):
317299
shutil.rmtree(base_directory)
318300
return
319-
320301
file_ending = '.cache'
321302
folder = 'cache'
322-
323303
base_directory = os.path.join(Path(os.path.dirname(
324304
os.path.realpath(__file__)) + os.path.sep).parent, folder)
325305

326306
if not os.path.exists(base_directory):
327307
return
328308

329309
print(f'Cleaning {file_ending[1:]} files...')
330-
331310
subdirs = os.listdir(base_directory)
332311
print(len(subdirs), f'file and folders in {folder} folder.')
333312
cache_files = 0
@@ -406,9 +385,7 @@ def get_http_content(url, allow_redirects=False, use_text_instead_of_content=Tru
406385
return content
407386

408387
headers = {'user-agent': USERAGENT}
409-
410388
hostname = urlparse(url).hostname
411-
412389
if hostname == 'api.github.com' and GITHUB_APIKEY is not None:
413390
headers['authorization'] = f'Bearer {GITHUB_APIKEY}'
414391
a = requests.get(url, allow_redirects=allow_redirects,
@@ -842,8 +819,7 @@ def get_country_code_from_ip2location(ip_address):
842819
except Exception: # pylint: disable=broad-exception-caught
843820
return ''
844821
if hasattr(rec, 'country_short'):
845-
countrycode = rec.country_short
846-
return countrycode
822+
return rec.country_short
847823
return ''
848824

849825

0 commit comments

Comments
 (0)