From 7adc7d824b0bb97409030a105a6f4c41379699a7 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 26 Dec 2022 20:13:34 -0300 Subject: [PATCH] add some type annotations --- app/scodoc/sco_archives.py | 32 +++++++++++++++++--------------- sco_version.py | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index 71acd8159..e4c86578b 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -28,7 +28,7 @@ """ScoDoc : gestion des archives des PV et bulletins, et des dossiers etudiants (admission) - Archives are plain files, stored in + Archives are plain files, stored in /archives/ (where is usually /opt/scodoc-data, and a departement id (int)) @@ -42,7 +42,7 @@ Les maquettes Apogée pour l'export des notes sont dans /apo_csv//-//.csv - + Un répertoire d'archive contient des fichiers quelconques, et un fichier texte nommé _description.txt qui est une description (humaine, format libre) de l'archive. @@ -105,13 +105,13 @@ class BaseArchiver(object): try: scu.GSL.acquire() if not os.path.isdir(path): - log("creating directory %s" % path) + log(f"creating directory {path}") os.mkdir(path) finally: scu.GSL.release() self.initialized = True - def get_obj_dir(self, oid): + def get_obj_dir(self, oid: int): """ :return: path to directory of archives for this object (eg formsemestre_id or etudid). If directory does not yet exist, create it. @@ -142,7 +142,7 @@ class BaseArchiver(object): dirs = glob.glob(base + "*") return [os.path.split(x)[1] for x in dirs] - def list_obj_archives(self, oid): + def list_obj_archives(self, oid: int): """Returns :return: list of archive identifiers for this object (paths to non empty dirs) """ @@ -157,7 +157,7 @@ class BaseArchiver(object): dirs.sort() return dirs - def delete_archive(self, archive_id): + def delete_archive(self, archive_id: str): """Delete (forever) this archive""" self.initialize() try: @@ -166,7 +166,7 @@ class BaseArchiver(object): finally: scu.GSL.release() - def get_archive_date(self, archive_id): + def get_archive_date(self, archive_id: str): """Returns date (as a DateTime object) of an archive""" return datetime.datetime( *[int(x) for x in os.path.split(archive_id)[1].split("-")] @@ -183,17 +183,17 @@ class BaseArchiver(object): files.sort() return [f for f in files if f and f[0] != "_"] - def get_archive_name(self, archive_id): + def get_archive_name(self, archive_id: str): """name identifying archive, to be used in web URLs""" return os.path.split(archive_id)[1] - def is_valid_archive_name(self, archive_name): + def is_valid_archive_name(self, archive_name: str): """check if name is valid.""" return re.match( "^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}$", archive_name ) - def get_id_from_name(self, oid, archive_name): + def get_id_from_name(self, oid, archive_name: str): """returns archive id (check that name is valid)""" self.initialize() if not self.is_valid_archive_name(archive_name): @@ -206,7 +206,7 @@ class BaseArchiver(object): raise ScoValueError(f"Archive {archive_name} introuvable") return archive_id - def get_archive_description(self, archive_id): + def get_archive_description(self, archive_id: str) -> str: """Return description of archive""" self.initialize() filename = os.path.join(archive_id, "_description.txt") @@ -247,7 +247,7 @@ class BaseArchiver(object): data = data.encode(scu.SCO_ENCODING) self.initialize() filename = scu.sanitize_filename(filename) - log("storing %s (%d bytes) in %s" % (filename, len(data), archive_id)) + log(f"storing {filename} ({len(data)} bytes) in {archive_id}") try: scu.GSL.acquire() fname = os.path.join(archive_id, filename) @@ -261,16 +261,18 @@ class BaseArchiver(object): """Retreive data""" self.initialize() if not scu.is_valid_filename(filename): - log('Archiver.get: invalid filename "%s"' % filename) + log(f"""Archiver.get: invalid filename '{filename}'""") raise ScoValueError("archive introuvable (déjà supprimée ?)") fname = os.path.join(archive_id, filename) - log("reading archive file %s" % fname) + log(f"reading archive file {fname}") 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""" + """Recupère les donnees du fichier indiqué et envoie au client. + Returns: Response + """ archive_id = self.get_id_from_name(oid, archive_name) data = self.get(archive_id, filename) mime = mimetypes.guess_type(filename)[0] diff --git a/sco_version.py b/sco_version.py index 98f0f59b5..91866c9f2 100644 --- a/sco_version.py +++ b/sco_version.py @@ -1,7 +1,7 @@ # -*- mode: python -*- # -*- coding: utf-8 -*- -SCOVERSION = "9.4.20" +SCOVERSION = "9.4.21" SCONAME = "ScoDoc"