From c8f606b0080a7dd343515c7c2ae830dfeecdf341 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 18 Oct 2022 12:59:43 +0300 Subject: Sanitise generated filenames * wqflask/wqflask/correlation/pre_computes.py: Sanitise the generated filenames to get rid of characters that have special meanings in a file path, and other characters that are just a nuisance. --- wqflask/wqflask/correlation/pre_computes.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wqflask/wqflask/correlation/pre_computes.py b/wqflask/wqflask/correlation/pre_computes.py index 85720013..37f037a7 100644 --- a/wqflask/wqflask/correlation/pre_computes.py +++ b/wqflask/wqflask/correlation/pre_computes.py @@ -209,6 +209,10 @@ def read_text_file(sample_dict, file_path): def write_db_to_textfile(db_name, conn, text_dir=TMPDIR): + def __sanitise_filename__(filename): + ttable = str.maketrans({" ": "_", "/": "_", "\\": "_"}) + return str.translate(filename, ttable) + def __generate_file_name__(db_name): # todo add expiry time and checker with conn.cursor() as cursor: @@ -216,7 +220,8 @@ def write_db_to_textfile(db_name, conn, text_dir=TMPDIR): 'SELECT Id, FullName FROM ProbeSetFreeze WHERE Name = %s', (db_name,)) results = cursor.fetchone() if (results): - return f"ProbeSetFreezeId_{results[0]}_{results[1]}" + return __sanitise_filename__( + f"ProbeSetFreezeId_{results[0]}_{results[1]}") def __parse_to_dict__(results): ids = ["ID"] @@ -232,10 +237,10 @@ def write_db_to_textfile(db_name, conn, text_dir=TMPDIR): def __write_to_file__(file_path, data, col_names): with open(file_path, 'w+', encoding='UTF8') as file_handler: - writer = csv.writer(file_handler) writer.writerow(col_names) writer.writerows(data.values()) + with conn.cursor() as cursor: cursor.execute( "SELECT ProbeSet.Name, Strain.Name, ProbeSetData.value " -- cgit v1.2.3