def select_active_term(self, termid, force_refresh=False): """Set the calendar to a given term :param str termid: :ref:`4-digit term identifier <4-digit-term-identifier>` If the local db contains no terms, it will be filled with all terms from the remote db. If the local db contains no courses for this term, it will be filled with all courses for this term from the remote db. """ if self.doesnt_know_about(datatype='terms'): terms = self._fetch(datatype='terms') self._save(terms, datatype='terms') if self.doesnt_know_about(datatype='terms', term=termid): logging.critical('Unknown term <{}> at <{}>'.format( termid, self._institution)) self._term = termid if force_refresh or self.doesnt_know_about(datatype='courses', term=termid): logging.info('Fetching courses, <{}> <term={}>'.format( self._institution, self._term)) courses = self._fetch(datatype='courses', term=self._term) self._save(courses, datatype='courses')
def seed_db(args): create_db() term = '1490' if args.term: term = args.term brain.get_calendar('ualberta').select_active_term(term, force_refresh=True) logging.info('DB seeded with term {}'.format(term))
def find_schedules(schedule_params, num_requested): """ :param dict schedule_params: parameters to build the schedule with. Check :ref:`api/generate-schedules <api-generate-schedules>` for available parameters. """ logging.info('Received schedule request') if 'term' not in schedule_params: logging.error("Schedule generation call did not specify <term>") term = schedule_params.get('term', '') institution = schedule_params.get('institution', 'ualberta') cal = classtime.brain.get_calendar(institution) if 'courses' not in schedule_params: logging.error("Schedule generation call did not specify <courses>") course_ids = schedule_params.get('courses', list()) busy_times = schedule_params.get('busy-times', list()) preferences = schedule_params.get('preferences', dict()) electives_groups = schedule_params.get('electives', list()) for electives_group in electives_groups: if 'courses' not in electives_group: logging.warning('"courses" not found for electives. q={}'.format( schedule_params)) schedules = _generate_schedules_sat(cal, term, course_ids, busy_times, electives_groups, preferences) schedules = _condense_schedules(cal, schedules) schedules = sorted(schedules, reverse=True, key=lambda s: s.overall_score()) if not schedules: logging.error('No schedules found for q={}'.format( schedule_params)) else: logging.info('Returning {}/{} schedules from request q={}'.format( min(num_requested, len(schedules)), len(schedules), schedule_params)) debug_msg = 'Request q={q}\n' + \ 'Response: Returning {ret} schedules\n' + \ ' including {ret_like} more like them\n' + \ ' out of {tot} total generated\n' + \ 'Returning:\n{ret_schedules}' logging.debug(debug_msg.format( q=schedule_params, ret=min(num_requested, len(schedules)), ret_like=sum([len(s.more_like_this) for s in schedules[:num_requested]]), tot=len(schedules) + sum([len(s.more_like_this) for s in schedules]), ret_schedules=schedules[:num_requested])) return schedules[:num_requested]
def find_schedules(schedule_params, num_requested): """ :param dict schedule_params: parameters to build the schedule with. Check :ref:`api/generate-schedules <api-generate-schedules>` for available parameters. """ logging.info('Received schedule request') if 'term' not in schedule_params: logging.error("Schedule generation call did not specify <term>") term = schedule_params.get('term', '') institution = schedule_params.get('institution', 'ualberta') cal = classtime.brain.get_calendar(institution) if 'courses' not in schedule_params: logging.error("Schedule generation call did not specify <courses>") course_ids = schedule_params.get('courses', list()) busy_times = schedule_params.get('busy-times', list()) preferences = schedule_params.get('preferences', dict()) electives_groups = schedule_params.get('electives', list()) for electives_group in electives_groups: if 'courses' not in electives_group: logging.warning('"courses" not found for electives. q={}'.format( schedule_params)) schedules = _generate_schedules_sat(cal, term, course_ids, busy_times, electives_groups, preferences) schedules = _condense_schedules(cal, schedules) schedules = sorted(schedules, reverse=True, key=lambda s: s.overall_score()) if not schedules: logging.error('No schedules found for q={}'.format(schedule_params)) else: logging.info('Returning {}/{} schedules from request q={}'.format( min(num_requested, len(schedules)), len(schedules), schedule_params)) debug_msg = 'Request q={q}\n' + \ 'Response: Returning {ret} schedules\n' + \ ' including {ret_like} more like them\n' + \ ' out of {tot} total generated\n' + \ 'Returning:\n{ret_schedules}' logging.debug( debug_msg.format(q=schedule_params, ret=min(num_requested, len(schedules)), ret_like=sum([ len(s.more_like_this) for s in schedules[:num_requested] ]), tot=len(schedules) + sum([len(s.more_like_this) for s in schedules]), ret_schedules=schedules[:num_requested])) return schedules[:num_requested]
def _idly_download_courses(self, sleeptime, force_refresh): import time if self.doesnt_know_about(datatype='terms'): logging.info( '[worker] Fetching all <{}> terms'.format(institution)) terms = self._fetch(datatype='terms') self._save(terms, datatype='terms') terms = [ term.term for term in self._local_db.query(datatype='terms').all() ] terms.reverse() for termid in terms: if force_refresh or self.doesnt_know_about(datatype='courses', term=termid): logging.info( '[worker] Fetching courses - <{}> <term={}>'.format( institution, termid)) courses = self._fetch(datatype='courses', term=termid) logging.info('[worker] Fetched {} courses'.format( len(courses))) self._save(courses, datatype='courses') msg = '[worker]...Saved courses - <{}> <term={}>' logging.info(msg.format(institution, termid)) for _ in range(sleeptime): time.sleep(1)
def _idly_download_courses(self, sleeptime, force_refresh): import time if self.doesnt_know_about(datatype='terms'): logging.info('[worker] Fetching all <{}> terms'.format( institution)) terms = self._fetch(datatype='terms') self._save(terms, datatype='terms') terms = [term.term for term in self._local_db.query(datatype='terms').all()] terms.reverse() for termid in terms: if force_refresh or self.doesnt_know_about(datatype='courses', term=termid): logging.info('[worker] Fetching courses - <{}> <term={}>'.format(institution, termid)) courses = self._fetch(datatype='courses', term=termid) logging.info('[worker] Fetched {} courses'.format(len(courses))) self._save(courses, datatype='courses') msg = '[worker]...Saved courses - <{}> <term={}>' logging.info(msg.format(institution, termid)) for _ in range(sleeptime): time.sleep(1)
def create_db(): db.create_all() logging.info('DB created!')
def delete_db(): db.drop_all() logging.info('DB deleted!')
import os from classtime.logging import logging logging = logging.getLogger(__name__) SECRET_KEY = os.environ.get('SECRET_KEY', 'debug') if SECRET_KEY == 'debug': DEBUG = True else: DEBUG = False SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:////tmp/classtime.db') logging.info('Using SQLALCHEMY_DATABASE_URI {}'.format(SQLALCHEMY_DATABASE_URI))