Python 3: n'utilise plus six. Utilise systématiquement with avec open.

This commit is contained in:
Emmanuel Viennet 2021-10-11 22:22:42 +02:00
parent 3c5b721a3a
commit c1d13d6089
18 changed files with 33 additions and 103 deletions

View File

@ -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))
)
)

View File

@ -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)

View File

@ -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):
"""

View File

@ -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,

View File

@ -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")
with open(fname, "wb") as f:
f.write(data)
f.close()
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"""

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -830,7 +830,7 @@ 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)
with open(filename) as f:
L = [x[:-1].split(";") for x in f]
E = {}
for l in L[1:]:

View File

@ -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):

View File

@ -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 = '<span onclick="toggle_vis(this);" class="toggle_sem sem_%%s">%s</span>'
minuslink = linktmpl % scu.icontag("minus_img", border="0", alt="-")

View File

@ -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]:

View File

@ -350,7 +350,6 @@ def pdf_basic_page(
# Gestion du lock pdf
import threading, time, six.moves.queue, six.moves._thread
class PDFLock(object):

View File

@ -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 ?

View File

@ -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())

View File

@ -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 = '<a class="stdlink" href="%s">précédentes</a>' % (
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 = '<a class="stdlink" href="%s">suivantes</a>' % (
request.base_url + "?" + six.moves.urllib.parse.urlencode(webparams)
request.base_url + "?" + urllib.parse.urlencode(webparams)
)
else:
next_lnk = ""

View File

@ -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()

View File

@ -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