From 427eb169aa82bd8ef3626ceb26a6a90fef919c71 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 9 Jul 2021 21:44:39 +0200 Subject: [PATCH] python-modernize --- misc/change_enseignant.py | 46 ++++++++++++------------- misc/change_etudid.py | 19 ++++++----- misc/count_inscriptions.py | 34 ++++++++++++------- misc/csv2rules.py | 9 ++--- misc/reset_sem_ens.py | 15 +++------ misc/zopelistmethods.py | 69 -------------------------------------- 6 files changed, 65 insertions(+), 127 deletions(-) delete mode 100644 misc/zopelistmethods.py diff --git a/misc/change_enseignant.py b/misc/change_enseignant.py index d8d3bc0c..438adef6 100755 --- a/misc/change_enseignant.py +++ b/misc/change_enseignant.py @@ -7,51 +7,51 @@ 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' + 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 +DBCNXSTRING = "dbname=%s" % dbname # Confirmation -ans = raw_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' +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 ) +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 } +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', - } + "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) + print("table %s: %s" % (table, cursor.statusmessage)) cnx.commit() - - - diff --git a/misc/change_etudid.py b/misc/change_etudid.py index eac00e1d..f157e3c0 100644 --- a/misc/change_etudid.py +++ b/misc/change_etudid.py @@ -21,15 +21,18 @@ et postgresql lance Emmanuel Viennet, 2007-2020 """ -import pdb, os, sys, psycopg2 +from __future__ import print_function +import pdb +import sys +import psycopg2 if len(sys.argv) != 4: - print "Usage: %s database orig_etudid dest_etudid" % sys.argv[0] - print " Fusionne l'étudiant orig dans l'étudiant dest." - print " L'identité, adresse et admission de dest restent inchangées." - print " Les inscriptions, notes, absences etc. d'orig sont associées à dest." - print " Après cette operation, orig_etudid peut être supprimé." - print "Exemple: change_etudid.py SCOGEII E1234 E87654" + print("Usage: %s database orig_etudid dest_etudid" % sys.argv[0]) + print(" Fusionne l'étudiant orig dans l'étudiant dest.") + print(" L'identité, adresse et admission de dest restent inchangées.") + print(" Les inscriptions, notes, absences etc. d'orig sont associées à dest.") + print(" Après cette operation, orig_etudid peut être supprimé.") + print("Exemple: change_etudid.py SCOGEII E1234 E87654") sys.exit(1) dbname = sys.argv[1] @@ -69,6 +72,6 @@ tables = ( for table in tables: cursor.execute(req % table, args) - print "table %s: %s" % (table, cursor.statusmessage) + print("table %s: %s" % (table, cursor.statusmessage)) cnx.commit() diff --git a/misc/count_inscriptions.py b/misc/count_inscriptions.py index 98afe99d..78cb9b36 100644 --- a/misc/count_inscriptions.py +++ b/misc/count_inscriptions.py @@ -6,14 +6,17 @@ (erreur passage GEA, fev 2007) """ -import pdb,os,sys,psycopg +from __future__ import print_function import csv +import pdb +import sys +import psycopg2 -DBCNXSTRING = 'host=localhost user=scogea dbname=SCOXXXX password=XXXXX' +DBCNXSTRING = "host=localhost user=scogea dbname=SCOXXXX password=XXXXX" -SCO_ENCODING = 'utf-8' +SCO_ENCODING = "utf-8" -cnx = psycopg.connect( DBCNXSTRING ) +cnx = psycopg2.connect(DBCNXSTRING) cursor = cnx.cursor() cursor.execute("select * from identite i order by nom") @@ -21,23 +24,28 @@ R = cursor.dictfetchall() nzero = 0 nhomonoins = 0 -print 'etudid, nom, prenom, nb_inscriptions' +print("etudid, nom, prenom, nb_inscriptions") for e in R: - cursor.execute("select count(*) from notes_formsemestre_inscription where etudid=%(etudid)s", { 'etudid' : e['etudid'] } ) + cursor.execute( + "select count(*) from notes_formsemestre_inscription where etudid=%(etudid)s", + {"etudid": e["etudid"]}, + ) nbins = cursor.fetchone()[0] if nbins == 0: nzero += 1 # recherche homonyme - cursor.execute("select * from identite i where nom=%(nom)s and prenom=%(prenom)s", e ) + cursor.execute( + "select * from identite i where nom=%(nom)s and prenom=%(prenom)s", e + ) H = cursor.dictfetchall() if len(H) == 2: - nhomonoins += 1 - print e['etudid'], e['nom'], e['prenom'], nbins + nhomonoins += 1 + print(e["etudid"], e["nom"], e["prenom"], nbins) # etudiant non inscrit ayant un homonyme exact: - # il doit etre supprimé !!! - #cursor.execute("delete from admissions where etudid=%(etudid)s", e) - #cursor.execute("delete from identite where etudid=%(etudid)s", e) + # il doit etre supprimé !!! + # cursor.execute("delete from admissions where etudid=%(etudid)s", e) + # cursor.execute("delete from identite where etudid=%(etudid)s", e) cnx.commit() -print '= %d etudiants, %d jamais inscrits, %d avec homo' % (len(R), nzero, nhomonoins) +print("= %d etudiants, %d jamais inscrits, %d avec homo" % (len(R), nzero, nhomonoins)) diff --git a/misc/csv2rules.py b/misc/csv2rules.py index 1bf8ed57..baf279ea 100755 --- a/misc/csv2rules.py +++ b/misc/csv2rules.py @@ -33,7 +33,10 @@ CSV: utf-8/Euro, tab, no text delimiter (le faire avec OpenOffice ou NeoOffice...) """ -import sys, pdb +from __future__ import print_function +import sys + +import app.scodoc.sco_utils as scu sourcefile = sys.argv[1] # fichier CSV @@ -75,8 +78,6 @@ rules_source_file='%s' sourcefile, ) -import sco_utils as scu - def _fmt(s): if not s: @@ -134,4 +135,4 @@ def genrules(csv): ) -print(genrules(open(sourcefile).readlines())) +print((genrules(open(sourcefile).readlines()))) diff --git a/misc/reset_sem_ens.py b/misc/reset_sem_ens.py index 80a943ac..6cde7d2f 100755 --- a/misc/reset_sem_ens.py +++ b/misc/reset_sem_ens.py @@ -14,27 +14,22 @@ import psycopg2 if len(sys.argv) != 4: - print( 'Usage: %s database formsemestre_id user_name' % sys.argv[0]) - print( 'Exemple: reset_sem_ens.py SCOGEII SEM34534 toto') + print("Usage: %s database formsemestre_id user_name" % sys.argv[0]) + print("Exemple: reset_sem_ens.py SCOGEII SEM34534 toto") sys.exit(1) dbname = sys.argv[1] formsemestre_id = sys.argv[2] user_name = sys.argv[3] -DBCNXSTRING = 'dbname=%s' % dbname +DBCNXSTRING = "dbname=%s" % dbname -cnx = psycopg2.connect( DBCNXSTRING ) +cnx = psycopg2.connect(DBCNXSTRING) cursor = cnx.cursor() print('affecting all modules of semestre %s to "%s"' % (formsemestre_id, user_name)) req = "update notes_moduleimpl set responsable_id=%(responsable_id)s where formsemestre_id=%(formsemestre_id)s" -cursor.execute(req, {'formsemestre_id':formsemestre_id, 'responsable_id': user_name}) +cursor.execute(req, {"formsemestre_id": formsemestre_id, "responsable_id": user_name}) cnx.commit() - - - - - diff --git a/misc/zopelistmethods.py b/misc/zopelistmethods.py deleted file mode 100644 index 565a7f56..00000000 --- a/misc/zopelistmethods.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- mode: python -*- -# -*- coding: utf-8 -*- - -"""List Zope published methods (helps redesign ScoDoc's API). - -Usage: - scotests/scointeractive.sh RT misc/zopelistmethods.py - -(replace RT by an existing departement id) - -E. Viennet 2020-01-26 -""" -from __future__ import print_function -from types import FunctionType, MethodType -from pprint import pprint as pp -import inspect - -from ZScoDoc import ZScoDoc -from ZScolar import ZScolar -from ZNotes import ZNotes -from ZAbsences import ZAbsences -from ZScoUsers import ZScoUsers -from ZEntreprises import ZEntreprises - -RESFILENAME = "publishedmethods.csv" - - -def get_methods_description(klass): - D = klass.__dict__ - M = [] - for method_name in D: - o = D[method_name] - if method_name[0] != "_" and type(o) == FunctionType and o.__doc__: - argspec = inspect.getargspec(D[method_name]) - defaults = argspec.defaults or [] - if defaults: - args = argspec.args[: -len(defaults)] - else: - args = argspec.args - for a in defaults: - args.append("%s=%s" % (argspec.args[len(args)], repr(a))) - M.append((method_name, ", ".join(args))) - M.sort() - return M - - -published_by_module = {} -lines = [] -for klass in ZScoDoc, ZScolar, ZNotes, ZAbsences, ZEntreprises, ZScoUsers: - module_name = klass.__name__ - published_by_module[module_name] = [] - M = get_methods_description(klass) - for m in M: - published_by_module[module_name].append(m) - lines.append((module_name, m[0], m[1])) - print("%s = %d published methods" % (module_name, len(M))) - -print("---\nModule \t #methods") -N = 0 -for module_name in published_by_module: - n = len(published_by_module[module_name]) - print(module_name, "\t", n) - N += n - -print("Total: \t ", N) - -print("Writing %s" % RESFILENAME) -with open(RESFILENAME, "w") as f: - f.write("\n".join(["\t".join(l) for l in lines]))