12
12
SITESPEED_USE_DOCKER = get_config_or_default ('sitespeed_use_docker' )
13
13
14
14
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
+ """
15
30
result = ''
16
31
if sitespeed_use_docker :
17
32
base_directory = Path (os .path .dirname (
@@ -96,6 +111,18 @@ def rate_custom_result_dict( # pylint: disable=too-many-arguments,too-many-local
96
111
global_translation , result_dict , validator_result_dicts ,
97
112
desktop_result_dict , mobile_result_dict ,
98
113
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
+ """
99
126
rating = Rating (global_translation )
100
127
for validator_result_dict in validator_result_dicts :
101
128
validator_name = validator_result_dict ['name' ]
@@ -130,6 +157,12 @@ def rate_custom_result_dict( # pylint: disable=too-many-arguments,too-many-local
130
157
131
158
132
159
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
+ """
133
166
base_directory = Path (os .path .dirname (
134
167
os .path .realpath (__file__ )) + os .path .sep ).parent
135
168
config_file = os .path .join (
@@ -142,6 +175,16 @@ def get_validators():
142
175
143
176
144
177
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
+ """
145
188
browertime_plugin_options = get_browsertime_plugin_options (validator_config )
146
189
arg = (
147
190
'--shm-size=1g '
@@ -170,6 +213,24 @@ def validate_on_mobile_using_validator(url, validator_config):
170
213
return result_dict
171
214
172
215
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
+ """
173
234
browertime_plugin_options = ''
174
235
if 'headers' in validator_config :
175
236
index = 1
@@ -191,6 +252,16 @@ def get_browsertime_plugin_options(validator_config):
191
252
192
253
193
254
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
+ """
194
265
browertime_plugin_options = get_browsertime_plugin_options (validator_config )
195
266
196
267
arg = (
@@ -219,6 +290,15 @@ def validate_on_desktop_using_validator(url, validator_config):
219
290
220
291
221
292
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
+ """
222
302
arg = (
223
303
'--shm-size=1g '
224
304
'-b chrome '
@@ -242,6 +322,15 @@ def validate_on_desktop(url):
242
322
243
323
244
324
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
+ """
245
334
arg = (
246
335
'--shm-size=1g '
247
336
'-b chrome '
@@ -268,6 +357,19 @@ def validate_on_mobile(url):
268
357
def rate_result_dict ( # pylint: disable=too-many-branches,too-many-locals
269
358
result_dict , reference_result_dict ,
270
359
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
+ """
271
373
limit = 500
272
374
273
375
rating = Rating (global_translation )
@@ -332,6 +434,30 @@ def rate_result_dict( # pylint: disable=too-many-branches,too-many-locals
332
434
return rating
333
435
334
436
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
+ """
335
461
if mobile_obj ['range' ] > (limit + noxternal_obj ['range' ]):
336
462
value_diff = mobile_obj ['range' ] - noxternal_obj ['range' ]
337
463
txt = (
@@ -342,6 +468,30 @@ def get_bumpy_advice_text(mode, limit, reference_name, key, mobile_obj, noxterna
342
468
return None
343
469
344
470
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
+ """
345
495
if mobile_obj ['median' ] > (limit + noxternal_obj ['median' ]):
346
496
value_diff = mobile_obj ['median' ] - noxternal_obj ['median' ]
347
497
txt = (
@@ -352,6 +502,21 @@ def get_improve_advice_text(mode, limit, reference_name, key, mobile_obj, noxter
352
502
353
503
354
504
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
+ """
355
520
reference_name = 'UNKNOWN'
356
521
if result_dict ['name' ].startswith ('mobile' ):
357
522
reference_name = 'mobile'
@@ -360,6 +525,15 @@ def get_reference_name(result_dict):
360
525
return reference_name
361
526
362
527
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
+ """
363
537
if 'Points' in result_dict :
364
538
del result_dict ['Points' ]
365
539
if 'name' in result_dict :
@@ -460,6 +634,15 @@ def get_data_for_entry(mode, key, values):
460
634
return tmp
461
635
462
636
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
+ """
463
646
fullname = key
464
647
if 'TTFB' in key :
465
648
# https://web.dev/ttfb/
0 commit comments