From c1d13d60891a17c46b5560a51df42e0c131b87a6 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 11 Oct 2021 22:22:42 +0200 Subject: [PATCH] =?UTF-8?q?Python=203:=20n'utilise=20plus=20six.=20Utilise?= =?UTF-8?q?=20syst=C3=A9matiquement=20with=20avec=20open.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/pe/pe_tools.py | 3 +- app/scodoc/gen_tables.py | 6 ++- app/scodoc/intervals.py | 3 -- app/scodoc/notes_table.py | 13 ++---- app/scodoc/sco_archives.py | 13 ++++-- app/scodoc/sco_codes_parcours.py | 1 - app/scodoc/sco_dump_db.py | 3 +- app/scodoc/sco_edt_cal.py | 6 +-- app/scodoc/sco_etud.py | 4 +- app/scodoc/sco_formsemestre_edit.py | 1 - app/scodoc/sco_formsemestre_validation.py | 5 +- app/scodoc/sco_groups.py | 6 --- app/scodoc/sco_pdf.py | 1 - app/scodoc/sco_portal_apogee.py | 4 +- app/scodoc/sco_report.py | 4 +- app/views/entreprises.py | 5 +- misc/change_enseignant.py | 57 ----------------------- requirements-3.9.txt | 1 - 18 files changed, 33 insertions(+), 103 deletions(-) delete mode 100755 misc/change_enseignant.py diff --git a/app/pe/pe_tools.py b/app/pe/pe_tools.py index aef083982..46e706eec 100644 --- a/app/pe/pe_tools.py +++ b/app/pe/pe_tools.py @@ -44,7 +44,6 @@ import unicodedata import app.scodoc.sco_utils as scu from app import log -import six PE_DEBUG = 0 @@ -145,7 +144,7 @@ def escape_for_latex(s): } exp = re.compile( "|".join( - re.escape(six.text_type(key)) + re.escape(key) for key in sorted(list(conv.keys()), key=lambda item: -len(item)) ) ) diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index f6ac6c343..a986494ed 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -752,6 +752,8 @@ if __name__ == "__main__": ) document.build(objects) data = doc.getvalue() - open("/tmp/gen_table.pdf", "wb").write(data) + with open("/tmp/gen_table.pdf", "wb") as f: + f.write(data) p = T.make_page(format="pdf") - open("toto.pdf", "wb").write(p) + with open("toto.pdf", "wb") as f: + f.write(p) diff --git a/app/scodoc/intervals.py b/app/scodoc/intervals.py index 9c9b59cc5..f4159bea7 100644 --- a/app/scodoc/intervals.py +++ b/app/scodoc/intervals.py @@ -4,11 +4,8 @@ # Code from http://code.activestate.com/recipes/457411/ -from __future__ import print_function from bisect import bisect_left, bisect_right -from six.moves import zip - class intervalmap(object): """ diff --git a/app/scodoc/notes_table.py b/app/scodoc/notes_table.py index 3ded58683..a06c2ccd5 100644 --- a/app/scodoc/notes_table.py +++ b/app/scodoc/notes_table.py @@ -27,10 +27,7 @@ """Calculs sur les notes et cache des resultats """ -import inspect -import os -import pdb -import time + from operator import itemgetter from flask import g, url_for @@ -40,12 +37,8 @@ import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb from app import log from app.scodoc.sco_formulas import NoteVector -from app.scodoc.sco_exceptions import ( - AccessDenied, - NoteProcessError, - ScoException, - ScoValueError, -) +from app.scodoc.sco_exceptions import ScoValueError + from app.scodoc.sco_formsemestre import ( formsemestre_uecoef_list, formsemestre_uecoef_create, diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index 8ebab00aa..f747d9127 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -203,7 +203,9 @@ class BaseArchiver(object): def get_archive_description(self, archive_id): """Return description of archive""" self.initialize() - return open(os.path.join(archive_id, "_description.txt")).read() + with open(os.path.join(archive_id, "_description.txt")) as f: + descr = f.read() + return descr def create_obj_archive(self, oid: int, description: str): """Creates a new archive for this object and returns its id.""" @@ -232,9 +234,8 @@ class BaseArchiver(object): try: scu.GSL.acquire() fname = os.path.join(archive_id, filename) - f = open(fname, "wb") - f.write(data) - f.close() + with open(fname, "wb") as f: + f.write(data) finally: scu.GSL.release() return filename @@ -247,7 +248,9 @@ class BaseArchiver(object): raise ValueError("invalid filename") fname = os.path.join(archive_id, filename) log("reading archive file %s" % fname) - return open(fname, "rb").read() + with open(fname, "rb") as f: + data = f.read() + return data def get_archived_file(self, oid, archive_name, filename): """Recupere donnees du fichier indiqué et envoie au client""" diff --git a/app/scodoc/sco_codes_parcours.py b/app/scodoc/sco_codes_parcours.py index 251a7adaf..ce0ab664b 100644 --- a/app/scodoc/sco_codes_parcours.py +++ b/app/scodoc/sco_codes_parcours.py @@ -28,7 +28,6 @@ """Semestres: Codes gestion parcours (constantes) """ import collections -from six.moves import range NOTES_TOLERANCE = 0.00499999999999 # si note >= (BARRE-TOLERANCE), considere ok # (permet d'eviter d'afficher 10.00 sous barre alors que la moyenne vaut 9.999) diff --git a/app/scodoc/sco_dump_db.py b/app/scodoc/sco_dump_db.py index 126d27832..8fa2e209e 100644 --- a/app/scodoc/sco_dump_db.py +++ b/app/scodoc/sco_dump_db.py @@ -167,7 +167,8 @@ def _anonymize_db(ano_db_name): def _get_scodoc_serial(): try: - return int(open(os.path.join(scu.SCODOC_VERSION_DIR, "scodoc.sn")).read()) + with open(os.path.join(scu.SCODOC_VERSION_DIR, "scodoc.sn")) as f: + return int(f.read()) except: return 0 diff --git a/app/scodoc/sco_edt_cal.py b/app/scodoc/sco_edt_cal.py index 312a6a4fc..f526c092f 100644 --- a/app/scodoc/sco_edt_cal.py +++ b/app/scodoc/sco_edt_cal.py @@ -33,10 +33,10 @@ XXX incompatible avec les ics HyperPlanning Paris 13 (était pour GPU). """ -import six.moves.urllib.request, six.moves.urllib.error, six.moves.urllib.parse -import traceback import icalendar import pprint +import traceback +import urllib import app.scodoc.sco_utils as scu from app import log @@ -80,7 +80,7 @@ def formsemestre_load_ics(sem): ics_data = "" else: log("Loading edt from %s" % ics_url) - f = six.moves.urllib.request.urlopen( + f = urllib.request.urlopen( ics_url, timeout=5 ) # 5s TODO: add config parameter, eg for slow networks ics_data = f.read() diff --git a/app/scodoc/sco_etud.py b/app/scodoc/sco_etud.py index 53d68f963..a2f2aebc9 100644 --- a/app/scodoc/sco_etud.py +++ b/app/scodoc/sco_etud.py @@ -830,8 +830,8 @@ appreciations_edit = _appreciationsEditor.edit def read_etablissements(): filename = os.path.join(scu.SCO_TOOLS_DIR, scu.CONFIG.ETABL_FILENAME) log("reading %s" % filename) - f = open(filename) - L = [x[:-1].split(";") for x in f] + with open(filename) as f: + L = [x[:-1].split(";") for x in f] E = {} for l in L[1:]: E[l[0]] = { diff --git a/app/scodoc/sco_formsemestre_edit.py b/app/scodoc/sco_formsemestre_edit.py index 2e238fe57..e66ca28d6 100644 --- a/app/scodoc/sco_formsemestre_edit.py +++ b/app/scodoc/sco_formsemestre_edit.py @@ -58,7 +58,6 @@ from app.scodoc import sco_permissions_check from app.scodoc import sco_portal_apogee from app.scodoc import sco_preferences from app.scodoc import sco_users -import six def _default_sem_title(F): diff --git a/app/scodoc/sco_formsemestre_validation.py b/app/scodoc/sco_formsemestre_validation.py index 7c49c792a..5319ee2a4 100644 --- a/app/scodoc/sco_formsemestre_validation.py +++ b/app/scodoc/sco_formsemestre_validation.py @@ -27,7 +27,7 @@ """Semestres: validation semestre et UE dans parcours """ -import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error, time, datetime +import time import flask from flask import url_for, g, request @@ -494,7 +494,7 @@ def formsemestre_recap_parcours_table( with_links=False, with_all_columns=True, a_url="", - sem_info={}, + sem_info=None, show_details=False, ): """Tableau HTML recap parcours @@ -502,6 +502,7 @@ def formsemestre_recap_parcours_table( sem_info = { formsemestre_id : txt } permet d'ajouter des informations associées à chaque semestre with_all_columns: si faux, pas de colonne "assiduité". """ + sem_info = sem_info or {} H = [] linktmpl = '%s' minuslink = linktmpl % scu.icontag("minus_img", border="0", alt="-") diff --git a/app/scodoc/sco_groups.py b/app/scodoc/sco_groups.py index 0a776813c..dac989248 100644 --- a/app/scodoc/sco_groups.py +++ b/app/scodoc/sco_groups.py @@ -59,8 +59,6 @@ from app.scodoc.sco_exceptions import ScoException, AccessDenied, ScoValueError from app.scodoc.sco_permissions import Permission from app.scodoc.TrivialFormulator import TrivialFormulator -import six - def checkGroupName( groupName, @@ -732,10 +730,6 @@ def setGroups( group_name = fs[0].strip() if not group_name: continue - # ajax arguments are encoded in utf-8: - # group_name = six.text_type(group_name, "utf-8").encode( - # scu.SCO_ENCODING - # ) # #py3 #sco8 group_id = createGroup(partition_id, group_name) # Place dans ce groupe les etudiants indiqués: for etudid in fs[1:-1]: diff --git a/app/scodoc/sco_pdf.py b/app/scodoc/sco_pdf.py index c504ae176..77e6f4e1b 100755 --- a/app/scodoc/sco_pdf.py +++ b/app/scodoc/sco_pdf.py @@ -350,7 +350,6 @@ def pdf_basic_page( # Gestion du lock pdf -import threading, time, six.moves.queue, six.moves._thread class PDFLock(object): diff --git a/app/scodoc/sco_portal_apogee.py b/app/scodoc/sco_portal_apogee.py index e79b35312..07006affd 100644 --- a/app/scodoc/sco_portal_apogee.py +++ b/app/scodoc/sco_portal_apogee.py @@ -39,7 +39,6 @@ import app.scodoc.sco_utils as scu from app import log from app.scodoc.sco_exceptions import ScoValueError from app.scodoc import sco_preferences -import six SCO_CACHE_ETAPE_FILENAME = os.path.join(scu.SCO_TMP_DIR, "last_etapes.xml") @@ -386,7 +385,8 @@ def get_etapes_apogee(): # cache le resultat (utile si le portail repond de façon intermitente) if infos: log("get_etapes_apogee: caching result") - open(SCO_CACHE_ETAPE_FILENAME, "w").write(doc) + with open(SCO_CACHE_ETAPE_FILENAME, "w") as f: + f.write(doc) except: log("invalid XML response from getEtapes Web Service\n%s" % etapes_url) # Avons nous la copie d'une réponse récente ? diff --git a/app/scodoc/sco_report.py b/app/scodoc/sco_report.py index 7d9a80821..078da6697 100644 --- a/app/scodoc/sco_report.py +++ b/app/scodoc/sco_report.py @@ -31,7 +31,6 @@ """ import os import tempfile -import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error import re import time import datetime @@ -1399,7 +1398,8 @@ def graph_parcours( # Genere graphe _, path = tempfile.mkstemp(".gr") g.write(path=path, format=format) - data = open(path, "rb").read() + with open(path, "rb") as f: + data = f.read() log("dot generated %d bytes in %s format" % (len(data), format)) if not data: log("graph.to_string=%s" % g.to_string()) diff --git a/app/views/entreprises.py b/app/views/entreprises.py index 9d1e76a06..5b8cec66a 100644 --- a/app/views/entreprises.py +++ b/app/views/entreprises.py @@ -35,6 +35,7 @@ Note: Code très ancien, porté de Zope/DTML, peu utilisable import time +import urllib from flask import request from flask_login import current_user @@ -166,14 +167,14 @@ def index_html(etud_nom=None, limit=50, offset="", format="html"): if offset: webparams["offset"] = max((offset or 0) - limit, 0) prev_lnk = 'précédentes' % ( - request.base_url + "?" + six.moves.urllib.parse.urlencode(webparams) + request.base_url + "?" + urllib.parse.urlencode(webparams) ) else: prev_lnk = "" if len(entreprises) >= limit: webparams["offset"] = (offset or 0) + limit next_lnk = 'suivantes' % ( - request.base_url + "?" + six.moves.urllib.parse.urlencode(webparams) + request.base_url + "?" + urllib.parse.urlencode(webparams) ) else: next_lnk = "" diff --git a/misc/change_enseignant.py b/misc/change_enseignant.py deleted file mode 100755 index 438adef6b..000000000 --- a/misc/change_enseignant.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -"""Change un identifiant d'enseignant (pour corriger une erreur, typiquement un doublon) - -(à lancer en tant qu'utilisateur postgres) -Emmanuel Viennet, 2007 - 2014 -""" - -from __future__ import print_function -import pdb, os, sys -import psycopg2 -from six.moves import input - -if len(sys.argv) != 4: - print("Usage: %s database ancien_utilisateur nouvel_utilisateur" % sys.argv[0]) - print("Exemple: change_enseignant.py SCOGEII toto tata") - sys.exit(1) - -dbname = sys.argv[1] -OLD_ID = sys.argv[2] -NEW_ID = sys.argv[3] - -DBCNXSTRING = "dbname=%s" % dbname - -# Confirmation -ans = input( - "Remplacer le l'utilisateur %s par %s dans toute la base du departement %s ?" - % (OLD_ID, NEW_ID, dbname) -).strip() -if not ans or ans[0].lower() not in "oOyY": - print("annulation") - sys.exit(-1) - - -cnx = psycopg2.connect(DBCNXSTRING) - -cursor = cnx.cursor() -req = "update %s set %s=%%(new_id)s where %s=%%(old_id)s" -args = {"old_id": OLD_ID, "new_id": NEW_ID} - -tables_attr = { - "notes_formsemestre": "responsable_id", - "entreprise_contact": "enseignant", - "admissions": "rapporteur", - "notes_moduleimpl": "responsable_id", - "notes_modules_enseignants": "ens_id", - "notes_notes": "uid", - "notes_notes_log": "uid", - "notes_appreciations": "author", -} - -for (table, attr) in tables_attr.items(): - cursor.execute(req % (table, attr, attr), args) - print("table %s: %s" % (table, cursor.statusmessage)) - -cnx.commit() diff --git a/requirements-3.9.txt b/requirements-3.9.txt index 682b05053..4c83a175a 100755 --- a/requirements-3.9.txt +++ b/requirements-3.9.txt @@ -58,7 +58,6 @@ redis==3.5.3 reportlab==3.6.1 requests==2.26.0 rq==1.9.0 -six==1.16.0 SQLAlchemy==1.4.22 toml==0.10.2 urllib3==1.26.6