From 8dcdb358478d6c9fd57f19f0c7cc9a5be710d50d Mon Sep 17 00:00:00 2001 From: Miguel Barao Date: Tue, 28 Nov 2017 18:05:44 +0000 Subject: [PATCH] - some fixes in exceptions. --- initdb.py | 14 +++++++------- serve.py | 8 +++----- test.py | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/initdb.py b/initdb.py index 2429c38..5e7a163 100755 --- a/initdb.py +++ b/initdb.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3.6 # -*- coding: utf-8 -*- import csv @@ -44,8 +44,8 @@ try: # from csv file if available try: csvreader = csv.DictReader(open(args.csvfile, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) - except EnvironmentError: - print('Error: CSV file "{0}" not found.'.format(args.csvfile)) + except OSError: + print(f'Error: Can\'t open CSV file "{args.csvfile}".') session.rollback() sys.exit(1) else: @@ -72,15 +72,15 @@ except Exception: else: n = session.query(Student).count() - print('New database created: {0}\n{1} user(s) inserted:'.format(args.db, n)) + print(f'New database created: {args.db}\n{n} user(s) inserted:') users = session.query(Student).order_by(Student.id).all() - print(' {0:8} - {1} (administrator)'.format(users[0].id, users[0].name)) + print(f' {users[0].id:8} - {users[0].name} (administrator)') if n > 1: - print(' {0:8} - {1}'.format(users[1].id, users[1].name)) + print(f' {users[1].id:8} - {users[1].name}') if n > 3: print(' ... ...') if n > 2: - print(' {0:8} - {1}'.format(users[-1].id, users[-1].name)) + print(f' {users[-1].id:8} - {users[-1].name}') # --- end session --- diff --git a/serve.py b/serve.py index 7759ad7..e131ef7 100755 --- a/serve.py +++ b/serve.py @@ -168,7 +168,7 @@ class TestHandler(BaseHandler): for i, q in enumerate(t['questions']): qid = str(i) # question id if 'answered-' + qid in self.request.arguments: - ans[i] = self.get_body_arguments(qid, None) + ans[i] = self.get_body_arguments(qid) # remove list when it does not make sense... if q['type'] == 'radio': @@ -219,10 +219,8 @@ class ReviewHandler(BaseHandler): try: f = open(path.expanduser(fname)) - except FileNotFoundError: # FIXME EnvironmentError? - logging.error(f'Cannot find "{fname}" for review.') - except Exception as e: - raise e + except OSError: + logging.error(f'Cannot open "{fname}" for review.') else: with f: t = json.load(f) diff --git a/test.py b/test.py index 977601d..dbdbb21 100644 --- a/test.py +++ b/test.py @@ -71,7 +71,7 @@ class TestFactory(dict): raise TestFactoryException() try: # check if answers_dir is a writable directory f = open(path.join(path.expanduser(self['answers_dir']),'REMOVE-ME'), 'w') - except EnvironmentError: + except OSError: logger.critical(f'Cannot write answers to "{self["answers_dir"]}".') raise TestFactoryException() else: @@ -96,7 +96,7 @@ class TestFactory(dict): logger.warning('Missing "files" key. Loading all YAML files from "questions_dir"... DANGEROUS!!!') try: self['files'] = fnmatch.filter(listdir(self['questions_dir']), '*.yaml') - except EnvironmentError: + except OSError: logger.critical('Couldn\'t get list of YAML question files.') raise TestFactoryException() if isinstance(self['files'], str): -- libgit2 0.21.2