Пример #1
0
def parse_runtime_limit(value, now=None):
    """Parsing CLI option for runtime limit, supplied as VALUE.

    Value could be something like: Sunday 23:00-05:00, the format being
    [Wee[kday]] [hh[:mm][-hh[:mm]]].
    The function will return two valid time ranges. The first could be in the
    past, containing the present or in the future. The second is always in the
    future.
    """

    def extract_time(value):
        value = _RE_RUNTIMELIMIT_HOUR.search(value).groupdict()
        return timedelta(hours=int(value['hours']),
                         minutes=int(value['minutes']))

    def extract_weekday(value):
        key = value[:3].lower()
        try:
            return {
                'mon': 0,
                'tue': 1,
                'wed': 2,
                'thu': 3,
                'fri': 4,
                'sat': 5,
                'sun': 6,
            }[key]
        except KeyError:
            raise ValueError("%s is not a good weekday name." % value)

    if now is None:
        now = datetime.now()

    today = now.date()
    g = _RE_RUNTIMELIMIT_FULL.search(value)
    if not g:
        raise ValueError('"%s" does not seem to be correct format for '
                         'parse_runtime_limit() '
                         '[Wee[kday]] [hh[:mm][-hh[:mm]]]).' % value)
    pieces = g.groupdict()

    if pieces['weekday_begin'] is None:
        # No weekday specified. So either today or tomorrow
        first_occasion_day = timedelta(days=0)
        next_occasion_delta = timedelta(days=1)
    else:
        # If given 'Mon' then we transform it to 'Mon-Mon'
        if pieces['weekday_end'] is None:
            pieces['weekday_end'] = pieces['weekday_begin']

        # Day range
        weekday_begin = extract_weekday(pieces['weekday_begin'])
        weekday_end = extract_weekday(pieces['weekday_end'])

        if weekday_begin <= today.weekday() <= weekday_end:
            first_occasion_day = timedelta(days=0)
        else:
            days = (weekday_begin - today.weekday()) % 7
            first_occasion_day = timedelta(days=days)

        weekday = (now + first_occasion_day).weekday()
        if weekday < weekday_end:
            # Fits in the same week
            next_occasion_delta = timedelta(days=1)
        else:
            # The week after
            days = weekday_begin - weekday + 7
            next_occasion_delta = timedelta(days=days)

    if pieces['hour_begin'] is None:
        pieces['hour_begin'] = '00:00'
    if pieces['hour_end'] is None:
        pieces['hour_end'] = '00:00'

    beginning_time = extract_time(pieces['hour_begin'])
    ending_time = extract_time(pieces['hour_end'])

    if not ending_time:
        ending_time = beginning_time + timedelta(days=1)
    elif beginning_time and ending_time and beginning_time > ending_time:
        ending_time += timedelta(days=1)

    start_time = real_datetime.combine(today, real_time(hour=0, minute=0))
    current_range = (
        start_time + first_occasion_day + beginning_time,
        start_time + first_occasion_day + ending_time
    )
    if now > current_range[1]:
        current_range = tuple(t + next_occasion_delta for t in current_range)

    future_range = (
        current_range[0] + next_occasion_delta,
        current_range[1] + next_occasion_delta
    )
    return current_range, future_range
Пример #2
0
def parse_runtime_limit(value, now=None):
    """
    Parsing CLI option for runtime limit, supplied as VALUE.
    Value could be something like: Sunday 23:00-05:00, the format being
    [Wee[kday]] [hh[:mm][-hh[:mm]]].
    The function will return two valid time ranges. The first could be in the past, containing the present or in the future. The second is always in the future.
    """
    def extract_time(value):
        value = _RE_RUNTIMELIMIT_HOUR.search(value).groupdict()
        return timedelta(hours=int(value['hours']),
                         minutes=int(value['minutes']))

    def extract_weekday(value):
        key = value[:3].lower()
        try:
            return {
                'mon': 0,
                'tue': 1,
                'wed': 2,
                'thu': 3,
                'fri': 4,
                'sat': 5,
                'sun': 6,
            }[key]
        except KeyError:
            raise ValueError("%s is not a good weekday name." % value)

    if now is None:
        now = datetime.now()

    today = now.date()
    g = _RE_RUNTIMELIMIT_FULL.search(value)
    if not g:
        raise ValueError(
            '"%s" does not seem to be correct format for parse_runtime_limit() [Wee[kday]] [hh[:mm][-hh[:mm]]]).'
            % value)
    pieces = g.groupdict()

    if pieces['weekday_begin'] is None:
        # No weekday specified. So either today or tomorrow
        first_occasion_day = timedelta(days=0)
        next_occasion_delta = timedelta(days=1)
    else:
        # If given 'Mon' then we transform it to 'Mon-Mon'
        if pieces['weekday_end'] is None:
            pieces['weekday_end'] = pieces['weekday_begin']

        # Day range
        weekday_begin = extract_weekday(pieces['weekday_begin'])
        weekday_end = extract_weekday(pieces['weekday_end'])

        if weekday_begin <= today.weekday() <= weekday_end:
            first_occasion_day = timedelta(days=0)
        else:
            days = (weekday_begin - today.weekday()) % 7
            first_occasion_day = timedelta(days=days)

        weekday = (now + first_occasion_day).weekday()
        if weekday < weekday_end:
            # Fits in the same week
            next_occasion_delta = timedelta(days=1)
        else:
            # The week after
            days = weekday_begin - weekday + 7
            next_occasion_delta = timedelta(days=days)

    if pieces['hour_begin'] is None:
        pieces['hour_begin'] = '00:00'
    if pieces['hour_end'] is None:
        pieces['hour_end'] = '00:00'

    beginning_time = extract_time(pieces['hour_begin'])
    ending_time = extract_time(pieces['hour_end'])

    if not ending_time:
        ending_time = beginning_time + timedelta(days=1)
    elif beginning_time and ending_time and beginning_time > ending_time:
        ending_time += timedelta(days=1)

    start_time = real_datetime.combine(today, real_time(hour=0, minute=0))
    current_range = (start_time + first_occasion_day + beginning_time,
                     start_time + first_occasion_day + ending_time)
    if now > current_range[1]:
        current_range = tuple(t + next_occasion_delta for t in current_range)

    future_range = (current_range[0] + next_occasion_delta,
                    current_range[1] + next_occasion_delta)
    return current_range, future_range