From 6e583cb3d69fe8ead021d56ceba380bdcfaf54d5 Mon Sep 17 00:00:00 2001 From: Rafa Faura Date: Fri, 24 Jan 2025 19:13:47 +0100 Subject: [PATCH] Minor: Code optimizations --- README.md | 2 +- humble.py | 40 ++++++++++++++++++++++------------------ l10n/details.txt | 14 +++++++------- l10n/details_es.txt | 14 +++++++------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 32020e9..e977f12 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - +
diff --git a/humble.py b/humble.py index 968a27d..e251be0 100644 --- a/humble.py +++ b/humble.py @@ -136,7 +136,7 @@ XML_STRING = ('Ref: ', 'Value: ', 'Valor: ') current_time = datetime.now().strftime("%Y/%m/%d - %H:%M:%S") -local_version = datetime.strptime('2025-01-18', '%Y-%m-%d').date() +local_version = datetime.strptime('2025-01-24', '%Y-%m-%d').date() class SSLContextAdapter(requests.adapters.HTTPAdapter): @@ -295,16 +295,15 @@ def get_l10n_content(): def get_analysis_results(): - print_detail_l('[analysis_time]') - print(round(end - start, 2), end="") - print_detail_l('[analysis_time_sec]') + analysis_t = str(round(end - start, 2)).rstrip() + print(f"{get_detail('[analysis_time]', replace=True)} {analysis_t}\ +{get_detail('[analysis_time_sec]', replace=True)}") t_cnt = sum([m_cnt, f_cnt, i_cnt[0], e_cnt]) analysis_totals = save_analysis_results(t_cnt) analysis_diff = compare_analysis_results(*analysis_totals, en_cnt=en_cnt, m_cnt=m_cnt, f_cnt=f_cnt, i_cnt=i_cnt, e_cnt=e_cnt, t_cnt=t_cnt) - print("") print_analysis_results(*analysis_diff, t_cnt=t_cnt) analysis_grade = grade_analysis(en_cnt, m_cnt, f_cnt, i_cnt, e_cnt) print(f"{get_detail(analysis_grade)}") @@ -775,9 +774,7 @@ def delete_lines(reliable=True): def print_export_path(filename, reliable): delete_lines(reliable=False) if reliable else delete_lines() if '-c' not in sys.argv: - print("") - print_detail_l('[report]') - print(path.abspath(filename)) + print(f"{print_detail_s('[report]')} {path.abspath(filename)}") def print_nowarnings(): @@ -816,8 +813,8 @@ def print_basic_info(export_filename): print(f" {current_time}") print(f'{URL_STRING[1]}{URL}') if export_filename: - print_detail_l('[export_filename]') - print(f"{export_filename}") + print(f"{get_detail('[export_filename]', replace=True)} \ +{export_filename}") def print_extended_info(args, reliable, status_code): @@ -889,6 +886,12 @@ def print_detail_r(id_mode, is_red=False): print("") +def print_detail_s(id_mode): + for i, line in enumerate(l10n_main): + if line.startswith(id_mode): + return f"\n{l10n_main[i+1].rstrip()}" + + def get_detail(id_mode, replace=False): for i, line in enumerate(l10n_main): if line.startswith(id_mode): @@ -1050,7 +1053,8 @@ def check_path_permissions(output_path): try: open(path.join(output_path, HUMBLE_FILES[1]), 'w') except PermissionError: - print(f"\n{get_detail('[args_nowr]', replace=True)}'{output_path}'") + print(f"\n {get_detail('[args_nowr]', replace=True)} \ +('{output_path}')") sys.exit() else: remove(path.join(output_path, HUMBLE_FILES[1])) @@ -1063,8 +1067,8 @@ def check_output_path(args, output_path): elif path.exists(output_path): check_path_permissions(output_path) else: - print(f"\n{get_detail('[args_noexportpath]', replace=True)}\ -('{output_path}').") + print(f"\n {get_detail('[args_noexportpath]', replace=True)} \ +('{output_path}')") sys.exit() @@ -1137,9 +1141,8 @@ def print_skipped_headers(args): def print_unsupported_headers(unsupported_headers): - print("") - print_detail_l('[args_skipped_unknown]') - print(f"{', '.join(f'{header}' for header in unsupported_headers)}") + print(f"\n {get_detail('[args_skipped_unknown]', replace=True)} \ +({', '.join(f'\'{header}\'' for header in unsupported_headers)})") sys.exit() @@ -2808,8 +2811,9 @@ def footer(self): self.set_y(-15) self.set_font('Helvetica', 'I', 8) self.set_text_color(0, 0, 0) - self.cell(0, 10, get_detail('[pdf_footer]') + str(self.page_no()) + - get_detail('[pdf_footer2]') + ' {nb}', align='C') + self.cell(0, 10, print_detail_s('[pdf_footer]') + ' ' + + str(self.page_no()) + get_detail('[pdf_footer2]') + + ' {nb}', align='C') pdf = PDF() pdf_links = (URL_STRING[1], REF_LINKS[2], REF_LINKS[3], URL_LIST[0], REF_LINKS[4]) diff --git a/l10n/details.txt b/l10n/details.txt index e1197e5..3cc9081 100644 --- a/l10n/details.txt +++ b/l10n/details.txt @@ -764,7 +764,7 @@ 'humble' (HTTP Headers Analyzer) [pdf_footer] -Page +Page [pdf_footer2] of @@ -836,7 +836,7 @@ HTTP Response Headers Error: The URL scheme provided is either invalid or unsupported. [report] - Report saved to + Report saved to [0section] [0. Info] @@ -1315,7 +1315,7 @@ HTTP Response Headers Findings to review: [analysis_time] - Done in + Done in [analysis_time_sec] seconds! (changes with respect to the last analysis in parentheses) @@ -1597,13 +1597,13 @@ Error: The parameter '-op' requires the parameter '-o'. Error: The parameter '-s' requires at least the name of one HTTP response header. [args_skipped_unknown] -Error: Remove these HTTP headers from the '-s' parameter as 'humble' does not check them: +Error: Remove these HTTP headers from the '-s' parameter as they are not analyzed [args_noexportpath] -Error: The indicated PATH does not exist +Error: The indicated PATH does not exist [args_nowr] -Error: This user does not have write permissions on the path +Error: This user does not have write permissions on the indicated PATH [args_input_traversal] Error: The indicated filename, or absolute path, seem wrong @@ -1636,7 +1636,7 @@ want to contribute?: Value: [export_filename] - File : + File : [e_grade] Analysis Grade: E (Review 'Enabled headers') diff --git a/l10n/details_es.txt b/l10n/details_es.txt index f5c3d66..9ed0821 100644 --- a/l10n/details_es.txt +++ b/l10n/details_es.txt @@ -766,7 +766,7 @@ 'humble' (Analizador de cabeceras HTTP) [pdf_footer] -Página +Página [pdf_footer2] de @@ -838,7 +838,7 @@ Cabeceras de respuesta HTTP Error: El esquema de la URL no es válido o no es compatible. [report] - Informe guardado en + Informe guardado en [0section] [0. Información] @@ -1320,7 +1320,7 @@ Cabeceras de respuesta HTTP Advertencias a revisar: [analysis_time] - Realizado en + Realizado en [analysis_time_sec] segundos! (cambios con respecto al ultimo análisis entre paréntesis) @@ -1599,16 +1599,16 @@ Error: la ruta indicada de 'testssl.sh' es incorrecta. Error: El parámetro '-s' requiere, al menos, el nombre de una cabecera HTTP de respuesta. [args_skipped_unknown] -Error: Elimina estas cabeceras HTTP del parámetro '-s', ya que 'humble' no las comprueba: +Error: Elimine estas cabeceras HTTP del parámetro '-s' ya que no se analizan [args_nooutputfmt] Error: El parámetro '-op' requiere el parámetro '-o'. [args_noexportpath] -Error: No existe la ruta indicada +Error: No existe la ruta indicada [args_nowr] -Erorr: Este usuario no tiene permisos de escritura en la ruta +Erorr: Este usuario no tiene permisos de escritura en la ruta indicada [args_input_traversal] Error: El nombre del fichero, o la ruta absoluta, indicados parecen erróneos @@ -1623,7 +1623,7 @@ Valores Valor: [export_filename] - Fich.: + Fich.: [e_grade] Nota del análisis: E (Revisa 'Cabeceras habilitadas')