def _run_validator(self, data): is_valid = False full_skip = False logger = logging.getLogger('parsing.schools.' + self.school) try: self.validator.validate(data) self.tracker.stats = dict(kind=data['kind'], status='valid') is_valid = True except ValidationError as e: if self.break_on_error: raise ValidationError(*e.args) else: logger.warning('Ingestion failed', exc_info=True) logger.debug('Ingestor dump', self) except ValidationWarning as e: if (isinstance(e, MultipleDefinitionsWarning) and self.skip_duplicates): full_skip = True else: is_valid = True if self.break_on_warning: raise ValidationWarning(*e.args) else: logger.warning('Validation warning', exc_info=True) logger.debug('Ingestor dump', self) return is_valid, full_skip
def is_short_course(date_start, date_end, short_course_weeks_limit): """ Checks whether a course's duration is longer than a short term course week limit or not. Limit is defined in the config file for the corresponding school. Arguments: date_start {str} -- Any reasonable date value for start date date_end {str} -- Any reasonable date value for end date short_course_weeks_limit {int} -- Number of weeks a course can be defined as "short term". Raises: ValidationError: Invalid date input ValidationError: Invalid date input Returns: bool -- Defines whether the course is short term or not. """ from parsing.library.validator import ValidationError is_short = False if short_course_weeks_limit is not None: if isinstance(date_start, basestring): date_start = dateparser.parse(date_start) if isinstance(date_end, basestring): date_end = dateparser.parse(date_end) if not isinstance(date_start, datetime): raise ValidationError('invalid date input {}'.format(date_start)) if not isinstance(date_end, datetime): raise ValidationError('invalid date input {}'.format(date_end)) date_diff = date_end - date_start is_short = date_diff.days <= short_course_weeks_limit * 7 return is_short
def time24(time): """Convert time to 24hr format. Args: time (str): time in reasonable format Returns: str: 24hr time in format hh:mm Raises: ParseError: Unparseable time input. """ from parsing.library.validator import ValidationError if isinstance(time, basestring): time = dateparser.parse(time) if not isinstance(time, datetime): raise ValidationError('invalid time input {}'.format(time)) return time.strftime('%H:%M')
def short_date(date): """Convert input to %m-%d-%y format. Returns None if input is None. Args: date (str): date in reasonable format Returns: str: Date in format %m-%d-%y if the input is not None. Raises: ParseError: Unparseable time input. """ from parsing.library.validator import ValidationError if date is not None: if isinstance(date, basestring): date = dateparser.parse(date) if not isinstance(date, datetime): raise ValidationError('invalid date input {}'.format(date)) return date.strftime('%m-%d-%y') else: return None