コード例 #1
0
ファイル: controllers.py プロジェクト: greg42/khal
def start_end_from_daterange(daterange, locale, default_timedelta=None):
    """
    convert a string description of a daterange into start and end datetime
    :param daterange: an iterable of strings that describes `daterange`
    :type daterange: tuple
    :param locale: locale settings
    :type locale: dict
    :param default_timedelta: default timedelta, if None is given and no end is
        specified, we assume start = end, in the form of '2d' for 2 days
    :type default_timedelta: str
    """
    if len(daterange) == 0:
        start = utils.datetime_fillin(end=False)
        if default_timedelta is None:
            end = utils.datetime_fillin(day=start)
        else:
            try:
                end = start + utils.guesstimedeltafstr(default_timedelta)
            except ValueError as e:
                raise InvalidDate(e)
    else:
        start, end, allday = utils.guessrangefstr(
            daterange, locale, default_timedelta=default_timedelta)
        if start is None or end is None:
            raise InvalidDate('Invalid date range: "%s"' %
                              (' '.join(daterange)))

    return start, end
コード例 #2
0
def start_end_from_daterange(daterange, locale, default_timedelta=None):
    """
    convert a string description of a daterange into start and end datetime
    :param daterange: an iterable of strings that describes `daterange`
    :type daterange: tuple
    :param locale: locale settings
    :type locale: dict
    :param default_timedelta: default timedelta, if None is given and no end is
        specified, we assume start = end, in the form of '2d' for 2 days
    :type default_timedelta: str
    """
    if len(daterange) == 0:
        start = utils.datetime_fillin(end=False)
        if default_timedelta is None:
            end = utils.datetime_fillin(day=start)
        else:
            try:
                end = start + utils.guesstimedeltafstr(default_timedelta)
            except ValueError as e:
                raise InvalidDate(e)
    else:
        start, end, allday = utils.guessrangefstr(
            daterange, locale, default_timedelta=default_timedelta)
        if start is None or end is None:
            raise InvalidDate('Invalid date range: "%s"' % (' '.join(daterange)))

    return start, end
コード例 #3
0
ファイル: controllers.py プロジェクト: greg42/khal
def get_list_from_str(collection,
                      locale,
                      start,
                      end,
                      notstarted=False,
                      format=None,
                      day_format=None,
                      once=False,
                      default_timedelta=None,
                      env=None,
                      show_all_days=False,
                      **kwargs):
    """returns a list of events scheduled in between `start` and `end`.

    :param collection:
    :type collection: khalendar.CalendarCollection
    :param locale: locale settings
    :type locale: dict
    :param notstarted: True if each event should start after start (instead of
        be active between start and end)
    :type nostarted: bool
    :param format: a format string that can be used in python string formatting
    :type format: str
    :param once: True if each event should only appear once
    :type once: bool
    :param default_timedelta: default length of datetimerange that should be
        reported on
    :type default_timedelta:
    :returns: a list to be printed as the agenda for the given datetime range
    :rtype: list(str)
    """

    event_column = []
    if once:
        kwargs["seen"] = set()
    if env is None:
        env = {}
    while start < end:
        day_end = utils.datetime_fillin(start.date())
        if start.date() == end.date():
            day_end = end
        current_events = get_list(collection,
                                  locale=locale,
                                  format=format,
                                  start=start,
                                  end=day_end,
                                  notstarted=notstarted,
                                  env=env,
                                  **kwargs)
        if day_format and (show_all_days or current_events):
            event_column.append(format_day(start.date(), day_format, locale))
        event_column.extend(current_events)
        start = utils.datetime_fillin(start.date(),
                                      end=False) + timedelta(days=1)
    if event_column == []:
        event_column = [style('No events', bold=True)]
    return event_column
コード例 #4
0
def get_list(collection, locale, start, end, format=None, notstarted=False, env=None, width=None,
             seen=None):
    """returns a list of events scheduled between start and end. Start and end
    are strings or datetimes (of some kind).

    :param collection:
    :type collection: khalendar.CalendarCollection

    :param start: the start datetime
    :param end: the end datetime
    :param format: a format string that can be used in python string formatting
    :type  format: str
    :param env: a collection of "static" values like calendar names and color
    :type env: dict
    :param nostarted: True if each event should start after start (instead of
    be active between start and end)
    :type nostarted: Boolean
    :format:
    :returns: a list to be printed as the agenda for the given days
    :rtype: list(str)

    """
    event_list = []
    if env is None:
        env = {}

    start_local = utils.datetime_fillin(start, end=False, locale=locale)
    end_local = utils.datetime_fillin(end, locale=locale)

    start = start_local.replace(tzinfo=None)
    end = end_local.replace(tzinfo=None)

    events = sorted(collection.get_localized(start_local, end_local))
    events_float = sorted(collection.get_floating(start, end))
    events = sorted(events + events_float)
    for event in events:
        event_start = utils.datetime_fillin(event.start, end=False, locale=locale)
        if not (notstarted and event_start.replace(tzinfo=None) < start):
            if seen is None or event.uid not in seen:
                try:
                    event_string = event.format(format, relative_to=(start, end), env=env)
                except KeyError as e:
                    logging.fatal(e)
                    sys.exit(1)

                if width:
                    event_list += textwrap.wrap(event_string, width)
                else:
                    event_list.append(event_string)
                if seen is not None:
                    seen.add(event.uid)

    return event_list
コード例 #5
0
ファイル: controllers.py プロジェクト: greg42/khal
def edit(collection,
         search_string,
         locale,
         format=None,
         allow_past=False,
         conf=None):
    if conf is not None:
        if format is None:
            format = conf['view']['event_format']

    term_width, _ = get_terminal_size()
    now = datetime.now()

    events = sorted(collection.search(search_string))
    for event in events:
        end = utils.datetime_fillin(event.end_local,
                                    locale).replace(tzinfo=None)
        if not allow_past and end < now:
            continue
        event_text = textwrap.wrap(event.format(format, relative_to=now),
                                   term_width)
        echo(''.join(event_text))
        if not edit_event(
                event, collection, locale, allow_quit=True, width=term_width):
            return
コード例 #6
0
def get_list_from_str(collection, locale, start, end, notstarted=False,
                      format=None, day_format=None, once=False,
                      default_timedelta=None, env=None, show_all_days=False,
                      **kwargs):
    """returns a list of events scheduled in between `start` and `end`.

    :param collection:
    :type collection: khalendar.CalendarCollection
    :param locale: locale settings
    :type locale: dict
    :param notstarted: True if each event should start after start (instead of
        be active between start and end)
    :type nostarted: bool
    :param format: a format string that can be used in python string formatting
    :type format: str
    :param once: True if each event should only appear once
    :type once: bool
    :param default_timedelta: default length of datetimerange that should be
        reported on
    :type default_timedelta:
    :returns: a list to be printed as the agenda for the given datetime range
    :rtype: list(str)
    """

    event_column = []
    if once:
        kwargs["seen"] = set()
    if env is None:
        env = {}
    while start < end:
        day_end = utils.datetime_fillin(start.date())
        if start.date() == end.date():
            day_end = end
        current_events = get_list(collection, locale=locale, format=format, start=start,
                                  end=day_end, notstarted=notstarted, env=env, **kwargs)
        if day_format and (show_all_days or current_events):
            event_column.append(format_day(start.date(), day_format, locale))
        event_column.extend(current_events)
        start = utils.datetime_fillin(start.date(), end=False) + timedelta(days=1)
    if event_column == []:
        event_column = [style('No events', bold=True)]
    return event_column
コード例 #7
0
def edit(collection, search_string, locale, format=None, allow_past=False, conf=None):
    if conf is not None:
        if format is None:
            format = conf['view']['event_format']

    term_width, _ = get_terminal_size()
    now = datetime.now()

    events = sorted(collection.search(search_string))
    for event in events:
        end = utils.datetime_fillin(event.end_local, locale).replace(tzinfo=None)
        if not allow_past and end < now:
            continue
        event_text = textwrap.wrap(event.format(format, relative_to=now), term_width)
        echo(''.join(event_text))
        if not edit_event(event, collection, locale, allow_quit=True, width=term_width):
            return
コード例 #8
0
ファイル: controllers.py プロジェクト: greg42/khal
def get_list(collection,
             locale,
             start,
             end,
             format=None,
             notstarted=False,
             env=None,
             width=None,
             seen=None):
    """returns a list of events scheduled between start and end. Start and end
    are strings or datetimes (of some kind).

    :param collection:
    :type collection: khalendar.CalendarCollection

    :param start: the start datetime
    :param end: the end datetime
    :param format: a format string that can be used in python string formatting
    :type  format: str
    :param env: a collection of "static" values like calendar names and color
    :type env: dict
    :param nostarted: True if each event should start after start (instead of
    be active between start and end)
    :type nostarted: Boolean
    :format:
    :returns: a list to be printed as the agenda for the given days
    :rtype: list(str)

    """
    event_list = []
    if env is None:
        env = {}

    start_local = utils.datetime_fillin(start, end=False, locale=locale)
    end_local = utils.datetime_fillin(end, locale=locale)

    start = start_local.replace(tzinfo=None)
    end = end_local.replace(tzinfo=None)

    events = sorted(collection.get_localized(start_local, end_local))
    events_float = sorted(collection.get_floating(start, end))
    events = sorted(events + events_float)
    for event in events:
        event_start = utils.datetime_fillin(event.start,
                                            end=False,
                                            locale=locale)
        if not (notstarted and event_start.replace(tzinfo=None) < start):
            if seen is None or event.uid not in seen:
                try:
                    event_string = event.format(format,
                                                relative_to=(start, end),
                                                env=env)
                except KeyError as e:
                    logging.fatal(e)
                    sys.exit(1)

                if width:
                    event_list += textwrap.wrap(event_string, width)
                else:
                    event_list.append(event_string)
                if seen is not None:
                    seen.add(event.uid)

    return event_list