fix string.translate

This commit is contained in:
Emmanuel Viennet 2021-07-12 10:51:45 +02:00
parent 5a9eade31b
commit 2b95f6e737
6 changed files with 22 additions and 13 deletions

View File

@ -117,6 +117,9 @@ def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1):
return oid return oid
_SQL_REMOVE_BAD_CHARS = str.maketrans("", "", '%*()+=&|[]"`')
def DBSelectArgs( def DBSelectArgs(
cnx, cnx,
table, table,
@ -168,7 +171,7 @@ def DBSelectArgs(
# n'autorise pas d'expressions # n'autorise pas d'expressions
if test == "~": if test == "~":
for k in vals.keys(): for k in vals.keys():
vals[k] = vals[k].translate(string.maketrans("", ""), '%*()+=&|[]"`') vals[k] = vals[k].translate(_SQL_REMOVE_BAD_CHARS)
if vals: if vals:
if aux_tables: # paren if aux_tables: # paren

View File

@ -290,7 +290,7 @@ def do_entreprise_check_etudiant(context, etudiant):
Sinon, retourne (0, 'message explicatif') Sinon, retourne (0, 'message explicatif')
""" """
etudiant = etudiant.strip().translate( etudiant = etudiant.strip().translate(
None, "'()" str.maketrans("", "", "'()")
) # suppress parens and quote from name ) # suppress parens and quote from name
if not etudiant: if not etudiant:
return 1, None return 1, None

View File

@ -1093,8 +1093,11 @@ def do_formsemestre_clone(
pvalue = prefs[pname] pvalue = prefs[pname]
try: try:
prefs.base_prefs.set(formsemestre_id, pname, pvalue) prefs.base_prefs.set(formsemestre_id, pname, pvalue)
except: except ValueError:
log("do_formsemestre_clone: ignoring old preference %s" % pname) log(
"do_formsemestre_clone: ignoring old preference %s=%s for %s"
% (pname, pvalue, formsemestre_id)
)
# 5- Copy formules utilisateur # 5- Copy formules utilisateur
objs = sco_compute_moy.formsemestre_ue_computation_expr_list( objs = sco_compute_moy.formsemestre_ue_computation_expr_list(

View File

@ -405,7 +405,7 @@ class DisplayedGroupsInfos(object):
self.groups_titles = ", ".join(groups_titles) self.groups_titles = ", ".join(groups_titles)
self.groups_filename = "_".join(groups_titles).replace(" ", "_") self.groups_filename = "_".join(groups_titles).replace(" ", "_")
# Sanitize filename: # 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: # colonnes pour affichages nom des groupes:
# gère le cas où les étudiants appartiennent à des semestres différents # gère le cas où les étudiants appartiennent à des semestres différents

View File

@ -480,16 +480,19 @@ def sanitize_string(s):
For ids and some filenames For ids and some filenames
""" """
return ( # Table suppressing some chars:
suppress_accents(s.translate(None, "'`\"<>!&\\ ")) trans = str.maketrans("", "", "'`\"<>!&\\ ")
.replace(" ", "_") return suppress_accents(s.translate(trans)).replace(" ", "_").replace("\t", "_")
.replace("\t", "_")
)
_BAD_FILENAME_CHARS = str.maketrans("", "", ":/\\")
def make_filename(name): def make_filename(name):
"""Try to convert name to a reasonnable filename""" """Try to convert name to a reasonable filename
return suppress_accents(name).replace(" ", "_") without spaces, (back)slashes, : and without accents
"""
return suppress_accents(name.translate(_BAD_FILENAME_CHARS)).replace(" ", "_")
VALID_CARS = ( VALID_CARS = (

View File

@ -31,7 +31,7 @@ for srcfilename in sys.argv[1:]:
else: else:
s = x.s.encode("UTF-8") s = x.s.encode("UTF-8")
# remove tabs and cr # remove tabs and cr
s = s.translate(None, "\t\n") s = s.replace("\t", "").replace("\n", "")
if len(s): if len(s):
print("%s\t%s" % (srcfilename, s)) print("%s\t%s" % (srcfilename, s))