From 2b95f6e737cb583459fce7f6cb7a5c12a19bc22e Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 12 Jul 2021 10:51:45 +0200 Subject: [PATCH] fix string.translate --- app/scodoc/notesdb.py | 5 ++++- app/scodoc/sco_entreprises.py | 2 +- app/scodoc/sco_formsemestre_edit.py | 7 +++++-- app/scodoc/sco_groups_view.py | 2 +- app/scodoc/sco_utils.py | 17 ++++++++++------- misc/extract_code_strings.py | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/scodoc/notesdb.py b/app/scodoc/notesdb.py index 76792126..8226c1e2 100644 --- a/app/scodoc/notesdb.py +++ b/app/scodoc/notesdb.py @@ -117,6 +117,9 @@ def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1): return oid +_SQL_REMOVE_BAD_CHARS = str.maketrans("", "", '%*()+=&|[]"`') + + def DBSelectArgs( cnx, table, @@ -168,7 +171,7 @@ def DBSelectArgs( # n'autorise pas d'expressions if test == "~": for k in vals.keys(): - vals[k] = vals[k].translate(string.maketrans("", ""), '%*()+=&|[]"`') + vals[k] = vals[k].translate(_SQL_REMOVE_BAD_CHARS) if vals: if aux_tables: # paren diff --git a/app/scodoc/sco_entreprises.py b/app/scodoc/sco_entreprises.py index 00c19a4f..8fb076a7 100644 --- a/app/scodoc/sco_entreprises.py +++ b/app/scodoc/sco_entreprises.py @@ -290,7 +290,7 @@ def do_entreprise_check_etudiant(context, etudiant): Sinon, retourne (0, 'message explicatif') """ etudiant = etudiant.strip().translate( - None, "'()" + str.maketrans("", "", "'()") ) # suppress parens and quote from name if not etudiant: return 1, None diff --git a/app/scodoc/sco_formsemestre_edit.py b/app/scodoc/sco_formsemestre_edit.py index a35b1631..d4e2b65f 100644 --- a/app/scodoc/sco_formsemestre_edit.py +++ b/app/scodoc/sco_formsemestre_edit.py @@ -1093,8 +1093,11 @@ def do_formsemestre_clone( pvalue = prefs[pname] try: prefs.base_prefs.set(formsemestre_id, pname, pvalue) - except: - log("do_formsemestre_clone: ignoring old preference %s" % pname) + except ValueError: + log( + "do_formsemestre_clone: ignoring old preference %s=%s for %s" + % (pname, pvalue, formsemestre_id) + ) # 5- Copy formules utilisateur objs = sco_compute_moy.formsemestre_ue_computation_expr_list( diff --git a/app/scodoc/sco_groups_view.py b/app/scodoc/sco_groups_view.py index 3c218fb1..08c4c527 100644 --- a/app/scodoc/sco_groups_view.py +++ b/app/scodoc/sco_groups_view.py @@ -405,7 +405,7 @@ class DisplayedGroupsInfos(object): self.groups_titles = ", ".join(groups_titles) self.groups_filename = "_".join(groups_titles).replace(" ", "_") # Sanitize filename: - self.groups_filename = self.groups_filename.translate(None, ":/\\") + self.groups_filename = scu.make_filename(self.groups_filename) # colonnes pour affichages nom des groupes: # gère le cas où les étudiants appartiennent à des semestres différents diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 637bbf75..819a37e9 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -480,16 +480,19 @@ def sanitize_string(s): For ids and some filenames """ - return ( - suppress_accents(s.translate(None, "'`\"<>!&\\ ")) - .replace(" ", "_") - .replace("\t", "_") - ) + # Table suppressing some chars: + trans = str.maketrans("", "", "'`\"<>!&\\ ") + return suppress_accents(s.translate(trans)).replace(" ", "_").replace("\t", "_") + + +_BAD_FILENAME_CHARS = str.maketrans("", "", ":/\\") def make_filename(name): - """Try to convert name to a reasonnable filename""" - return suppress_accents(name).replace(" ", "_") + """Try to convert name to a reasonable filename + without spaces, (back)slashes, : and without accents + """ + return suppress_accents(name.translate(_BAD_FILENAME_CHARS)).replace(" ", "_") VALID_CARS = ( diff --git a/misc/extract_code_strings.py b/misc/extract_code_strings.py index ad16304d..651900d7 100755 --- a/misc/extract_code_strings.py +++ b/misc/extract_code_strings.py @@ -31,7 +31,7 @@ for srcfilename in sys.argv[1:]: else: s = x.s.encode("UTF-8") # remove tabs and cr - s = s.translate(None, "\t\n") + s = s.replace("\t", "").replace("\n", "") if len(s): print("%s\t%s" % (srcfilename, s))