def _bad_cal():
    """Raiser function"""
    raise CAPException(
        {
            'type': 'danger',
            'msg': 'Calendar file is not a valid ICS file.'
        }, 400)
def _forbidden():
    """Raiser function"""
    raise CAPException(
        {
            'type': 'danger',
            'msg': 'You are not allowed to view this ressource.'
        }, 403)
def _planning_not_found(uuid):
    """Raiser function"""
    raise CAPException(
        {
            'type': 'danger',
            'msg': 'Planning with uuid "%s" not found' % uuid
        }, 404)
def get_interpreter_and_planning_from(uuid):
    planning = _get_planning(uuid, g.user_id)
    moodle_archive_path = planning.mbz_fullpath

    try:
        calendar = CalendarReader(planning.ics_fullpath)
        calendar_meetings = calendar.get_all_meetings()
    except CAPException as e:
        raise e
    except Exception as e:
        raise CAPException({'type': 'danger', 'msg': str(e)}, 400)

    if not moodle_archive_path:
        return Interpreter(calendar_meetings, None), planning

    # Make tmp directory for MBZ extraction
    with tempfile.TemporaryDirectory() as tmp_path:
        # Extract Moodle course to tmp folder
        try:
            with tarfile.open(moodle_archive_path) as tar_file:
                tar_file.extractall(tmp_path)
                course = MoodleCourse(tmp_path)
        except Exception:
            _bad_mbz()
    return Interpreter(calendar_meetings, course), planning
def _bad_mbz():
    """Raiser function"""
    raise CAPException({
        'type': 'danger',
        'msg': 'MBZ file could not be read.'
    }, 400)