Skip to content

Commit 8c60c9c

Browse files
committed
pylint docstring
Webperf-se#322
1 parent 35cac32 commit 8c60c9c

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

tests/performance_sitespeed_io.py

+183
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
SITESPEED_USE_DOCKER = get_config_or_default('sitespeed_use_docker')
1313

1414
def get_result(sitespeed_use_docker, arg):
15+
"""
16+
Executes a Sitespeed command and returns the result.
17+
18+
This function runs a Sitespeed command either in a Docker container or directly via Node.js,
19+
depending on the value of `sitespeed_use_docker`.
20+
The command's output is captured and returned as a string.
21+
22+
Args:
23+
sitespeed_use_docker (bool): If True, Sitespeed is run in a Docker container.
24+
Otherwise, it's run via Node.js.
25+
arg (str): The arguments to pass to the Sitespeed command.
26+
27+
Returns:
28+
str: The output of the Sitespeed command.
29+
"""
1530
result = ''
1631
if sitespeed_use_docker:
1732
base_directory = Path(os.path.dirname(
@@ -96,6 +111,18 @@ def rate_custom_result_dict( # pylint: disable=too-many-arguments,too-many-local
96111
global_translation, result_dict, validator_result_dicts,
97112
desktop_result_dict, mobile_result_dict,
98113
desktop_rating, mobile_rating):
114+
"""
115+
Rates a custom result dictionary based on validator results and reference ratings.
116+
117+
This function iterates over validator results,
118+
rates each one, and updates the overall rating.
119+
If a validator uses a reference (mobile or desktop),
120+
it compares the validator rating with the reference rating and
121+
adds advice to the overall review if the validator rating is higher.
122+
123+
Returns:
124+
Rating: The overall rating after considering all validators.
125+
"""
99126
rating = Rating(global_translation)
100127
for validator_result_dict in validator_result_dicts:
101128
validator_name = validator_result_dict['name']
@@ -130,6 +157,12 @@ def rate_custom_result_dict( # pylint: disable=too-many-arguments,too-many-local
130157

131158

132159
def get_validators():
160+
"""
161+
Loads and returns data from the 'sitespeed-rules.json' file if it exists.
162+
163+
Returns:
164+
list or dict: Data from the JSON file, or an empty list if the file doesn't exist.
165+
"""
133166
base_directory = Path(os.path.dirname(
134167
os.path.realpath(__file__)) + os.path.sep).parent
135168
config_file = os.path.join(
@@ -142,6 +175,16 @@ def get_validators():
142175

143176

144177
def validate_on_mobile_using_validator(url, validator_config):
178+
"""
179+
Function to validate a URL on mobile using custom validators.
180+
181+
Parameters:
182+
url (str): The URL to be validated.
183+
validator_config (dict): The validator config using HTML and/or Response headers.
184+
185+
Returns:
186+
dict: The result dictionary containing the validation results.
187+
"""
145188
browertime_plugin_options = get_browsertime_plugin_options(validator_config)
146189
arg = (
147190
'--shm-size=1g '
@@ -170,6 +213,24 @@ def validate_on_mobile_using_validator(url, validator_config):
170213
return result_dict
171214

172215
def get_browsertime_plugin_options(validator_config):
216+
"""
217+
Generates a string of plugin options for Browsertime based on
218+
the given validator configuration.
219+
220+
The function iterates over 'headers' and 'htmls' in the validator_config.
221+
For each header, it appends a formatted string to the plugin options.
222+
The string contains the header name and value, with spaces and equals signs URL-encoded.
223+
Similarly for each html,
224+
it appends a formatted string with the 'replace' and 'replaceWith' values URL-encoded.
225+
226+
Args:
227+
validator_config (dict): A dictionary containing 'headers' and/or 'htmls'.
228+
Each 'header' is a dictionary with 'name' and'value' keys.
229+
Each 'html' is a dictionary with 'replace' and 'replaceWith' keys.
230+
231+
Returns:
232+
str: A string of Browsertime plugin options.
233+
"""
173234
browertime_plugin_options = ''
174235
if 'headers' in validator_config:
175236
index = 1
@@ -191,6 +252,16 @@ def get_browsertime_plugin_options(validator_config):
191252

192253

193254
def validate_on_desktop_using_validator(url, validator_config):
255+
"""
256+
Function to validate a URL on desktop using custom validators.
257+
258+
Parameters:
259+
url (str): The URL to be validated.
260+
validator_config (dict): The validator config using HTML and/or Response headers.
261+
262+
Returns:
263+
dict: The result dictionary containing the validation results.
264+
"""
194265
browertime_plugin_options = get_browsertime_plugin_options(validator_config)
195266

196267
arg = (
@@ -219,6 +290,15 @@ def validate_on_desktop_using_validator(url, validator_config):
219290

220291

221292
def validate_on_desktop(url):
293+
"""
294+
Function to validate a URL on desktop.
295+
296+
Parameters:
297+
url (str): The URL to be validated.
298+
299+
Returns:
300+
dict: The result dictionary containing the validation results.
301+
"""
222302
arg = (
223303
'--shm-size=1g '
224304
'-b chrome '
@@ -242,6 +322,15 @@ def validate_on_desktop(url):
242322

243323

244324
def validate_on_mobile(url):
325+
"""
326+
Function to validate a URL on mobile, simulating fast 3g.
327+
328+
Parameters:
329+
url (str): The URL to be validated.
330+
331+
Returns:
332+
dict: The result dictionary containing the validation results.
333+
"""
245334
arg = (
246335
'--shm-size=1g '
247336
'-b chrome '
@@ -268,6 +357,19 @@ def validate_on_mobile(url):
268357
def rate_result_dict( # pylint: disable=too-many-branches,too-many-locals
269358
result_dict, reference_result_dict,
270359
mode, global_translation):
360+
"""
361+
Function to rate a result dictionary based on a reference result dictionary and
362+
generate review texts.
363+
364+
Parameters:
365+
result_dict (dict): The result dictionary to be rated.
366+
reference_result_dict (dict): The reference result dictionary for comparison.
367+
mode (str): The mode of changes.
368+
global_translation (str): The global translation used for rating.
369+
370+
Returns:
371+
Rating: A Rating object with the overall and performance reviews.
372+
"""
271373
limit = 500
272374

273375
rating = Rating(global_translation)
@@ -332,6 +434,30 @@ def rate_result_dict( # pylint: disable=too-many-branches,too-many-locals
332434
return rating
333435

334436
def get_bumpy_advice_text(mode, limit, reference_name, key, mobile_obj, noxternal_obj): # pylint: disable=too-many-arguments
437+
"""
438+
Function to generate advice text on how to make
439+
a certain key less "bumpy" based on the comparison of range values.
440+
441+
The function compares the range value of 'mobile_obj' with the sum of 'limit' and
442+
the range value of 'noxternal_obj'.
443+
If the range value of 'mobile_obj' is greater,
444+
it calculates the difference between the two range values and generates advice text.
445+
If not, it returns None.
446+
447+
Parameters:
448+
mode (str): The mode of changes.
449+
limit (float): The limit value for comparison.
450+
reference_name (str): The reference name.
451+
key (str): The key that could be made less "bumpy".
452+
mobile_obj (dict): A dictionary containing a 'range' key,
453+
the value of which is used for comparison.
454+
noxternal_obj (dict): A dictionary containing a 'range' key,
455+
the value of which is used for comparison.
456+
457+
Returns:
458+
str or None: The advice text on how to make the key less "bumpy" with mode changes,
459+
or None if the condition is not met.
460+
"""
335461
if mobile_obj['range'] > (limit + noxternal_obj['range']):
336462
value_diff = mobile_obj['range'] - noxternal_obj['range']
337463
txt = (
@@ -342,6 +468,30 @@ def get_bumpy_advice_text(mode, limit, reference_name, key, mobile_obj, noxterna
342468
return None
343469

344470
def get_improve_advice_text(mode, limit, reference_name, key, mobile_obj, noxternal_obj): # pylint: disable=too-many-arguments
471+
"""
472+
Function to generate advice text on how to improve a
473+
certain key based on the comparison of median values.
474+
475+
The function compares the median value of 'mobile_obj' with the sum of 'limit' and
476+
the median value of 'noxternal_obj'.
477+
If the median value of 'mobile_obj' is greater,
478+
it calculates the difference between the two median values and generates advice text.
479+
If not, it returns None.
480+
481+
Parameters:
482+
mode (str): The mode of changes.
483+
limit (float): The limit value for comparison.
484+
reference_name (str): The reference name.
485+
key (str): The key that may be improved.
486+
mobile_obj (dict): A dictionary containing a 'median' key,
487+
the value of which is used for comparison.
488+
noxternal_obj (dict): A dictionary containing a 'median' key,
489+
the value of which is used for comparison.
490+
491+
Returns:
492+
str or None: The advice text on how to improve the key with mode changes,
493+
or None if the condition is not met.
494+
"""
345495
if mobile_obj['median'] > (limit + noxternal_obj['median']):
346496
value_diff = mobile_obj['median'] - noxternal_obj['median']
347497
txt = (
@@ -352,6 +502,21 @@ def get_improve_advice_text(mode, limit, reference_name, key, mobile_obj, noxter
352502

353503

354504
def get_reference_name(result_dict):
505+
"""
506+
Function to determine the reference name based on the 'name' key in the input dictionary.
507+
508+
The function checks the start of the string value of the 'name' key in the input dictionary.
509+
If it starts with 'mobile', the reference name is set to 'mobile'.
510+
If it starts with 'desktop', the reference name is set to 'desktop'.
511+
If neither condition is met, the reference name defaults to 'UNKNOWN'.
512+
513+
Parameters:
514+
result_dict (dict): A dictionary containing a 'name' key,
515+
the value of which is used to determine the reference name.
516+
517+
Returns:
518+
str: The reference name, which can be 'mobile', 'desktop', or 'UNKNOWN'.
519+
"""
355520
reference_name = 'UNKNOWN'
356521
if result_dict['name'].startswith('mobile'):
357522
reference_name = 'mobile'
@@ -360,6 +525,15 @@ def get_reference_name(result_dict):
360525
return reference_name
361526

362527
def cleanup_result_dict(result_dict):
528+
"""
529+
Remove specific keys from the result dictionary.
530+
531+
Parameters:
532+
result_dict (dict): The result dictionary to be cleaned up.
533+
534+
Returns:
535+
None: The function modifies the dictionary in-place and does not return anything.
536+
"""
363537
if 'Points' in result_dict:
364538
del result_dict['Points']
365539
if 'name' in result_dict:
@@ -460,6 +634,15 @@ def get_data_for_entry(mode, key, values):
460634
return tmp
461635

462636
def get_fullname(key):
637+
"""
638+
Converts a performance metric abbreviation to its full name.
639+
640+
Args:
641+
key (str): The abbreviation of a web performance metric.
642+
643+
Returns:
644+
str: The full name of the web performance metric, or the original key if no match is found.
645+
"""
463646
fullname = key
464647
if 'TTFB' in key:
465648
# https://web.dev/ttfb/

0 commit comments

Comments
 (0)