Skip to content

Commit

Permalink
Auxiclean exceptions into separate file + all sheet managers raise Ex…
Browse files Browse the repository at this point in the history
…celErrors only
  • Loading branch information
fgoudreault committed Aug 25, 2017
1 parent 465706e commit e422ec6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
6 changes: 6 additions & 0 deletions auxiclean/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ExcelError(Exception):
"""
Error raised if the Excel file is bad formatted or
there's a mistake in the entries.
"""
pass
24 changes: 15 additions & 9 deletions auxiclean/managers/excel_manager.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from openpyxl import load_workbook
from .sheet_managers import (CoursesSheetManager, CandidatesSheetManager,
DistributionSheetManager, ExcelError)
DistributionSheetManager)
from ..exceptions import ExcelError
import logging


class ExcelFileManager:
def __init__(self, path):
def __init__(self, path, loglevel=logging.INFO):
self.logger = logging.getLogger("auxiclean.managers.excelfilemanager")
self.logger.setLevel(loglevel)

self.path = path
self.wb = load_workbook(path)
self._courses = CoursesSheetManager(self.wb)
Expand Down Expand Up @@ -63,18 +68,19 @@ def _checkup(self):
if d is None or d.lower() == "générale":
continue
if d not in all_courses_disciplines:
raise ExcelError("La discipline '%s' de %s n'est pas dans"
" la liste de toutes les"
" disciplines des cours." %
(d, candidate.name))
self.logger.warning("La discipline '%s' de %s n'est pas dans"
" la liste de toutes les"
" disciplines des cours." %
(d, candidate.name))
for course in self.courses:
d = course.discipline.lower()
if d is None or d.lower() == "générale":
continue
if d not in all_candidates_disciplines:
raise ExcelError("La discipline '%s' du cours %s n'es pas dans"
" la liste de toutes les disciplines des"
" candidatures." % (d, course.code))
self.logger.warning("La discipline '%s' du cours %s n'est"
" pas dans"
" la liste de toutes les disciplines des"
" candidatures." % (d, course.code))

def _choice_in_courses(self, choice):
for course in self.courses:
Expand Down
17 changes: 7 additions & 10 deletions auxiclean/managers/sheet_managers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ..candidate import Candidate
from ..course import Course
from ..exceptions import ExcelError
from openpyxl.styles import Alignment, colors, PatternFill, Font
import warnings

Expand All @@ -15,12 +16,12 @@ def _find_max_columns(self, titles_row):
elif cell.value.lower() == "none":
return i
return len(titles_row)

def get_sheet(self, workbook, sheetname):
for sheet in workbook.worksheets:
if sheet.title.lower() == sheetname.lower():
return sheet
raise ValueError("%s sheet is not found inside file." % sheetname)
raise ExcelError("%s sheet is not found inside file." % sheetname)


class CoursesSheetManager(BaseSheetManager):
Expand All @@ -41,10 +42,10 @@ def load_courses_from_wb(self, wb):
max_column = self._find_max_columns(titles)
titles = titles[:max_column]
courses_data = ws[2:ws.max_row] # rest are actual courses data
courses_data = [c[:max_column] for c in courses_data]
courses = []
if ws.max_row == 2:
courses_data = (courses_data, ) # bring into a tuple if needed
courses_data = [c[:max_column] for c in courses_data]
courses = []
for course in courses_data:
d = {label.value.lower(): cell.value
for label, cell in zip(titles, course)}
Expand Down Expand Up @@ -72,9 +73,9 @@ def load_candidates_from_wb(self, wb):
max_column = self._find_max_columns(titles)
titles = titles[:max_column]
candidates_data = ws[2:ws.max_row]
candidates_data = [c[:max_column] for c in candidates_data]
if ws.max_row == 2:
candidates_data = (candidates_data, ) # bring into a tuple
candidates_data = [c[:max_column] for c in candidates_data]
candidates = []
for i, candidate in enumerate(candidates_data):
d = {label.value.lower(): cell.value
Expand Down Expand Up @@ -167,7 +168,7 @@ def get_sheet(self, workbook):
# it one already exists, create one but warn user.
try:
ws = super().get_sheet(workbook, "Distribution")
except ValueError:
except ExcelError:
# sheet does not exist
ws = workbook.create_sheet("Distribution")
else:
Expand All @@ -176,7 +177,3 @@ def get_sheet(self, workbook):
warnings.warn("A Distribution sheet already exists in destination."
" A new one will be created: %s." % ws.title)
return ws


class ExcelError(Exception):
pass

0 comments on commit e422ec6

Please sign in to comment.