Esempio n. 1
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
Esempio n. 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
Esempio n. 3
0
 def test_moregarbage(self):
     with pytest.raises(ValueError):
         guesstimedeltafstr('foo10m')
Esempio n. 4
0
 def test_multi_nospace(self):
     assert timedelta(days=1, hours=-3, minutes=10) == \
         guesstimedeltafstr('1D-3hour10m')
Esempio n. 5
0
 def test_garbage(self):
     with pytest.raises(ValueError):
         guesstimedeltafstr('10mbar')
Esempio n. 6
0
 def test_negative(self):
     assert timedelta(minutes=-10) == guesstimedeltafstr('-10m')
Esempio n. 7
0
 def test_multi(self):
     assert timedelta(days=1, hours=-3, minutes=10) == \
         guesstimedeltafstr(' 1d -3H 10min ')
Esempio n. 8
0
 def test_seconds(self):
     assert timedelta(seconds=10) == guesstimedeltafstr('10s')
Esempio n. 9
0
 def test_seconds(self):
     assert timedelta(seconds=10) == guesstimedeltafstr('10s')
Esempio n. 10
0
 def test_same(self):
     assert timedelta(minutes=20) == \
         guesstimedeltafstr('10min 10minutes')
Esempio n. 11
0
def edit_event(event, collection, locale, allow_quit=False, width=80):
    options = OrderedDict()
    if allow_quit:
        options["no"] = {"short": "n"}
    else:
        options["done"] = {"short": "n"}
    options["summary"] = {"short": "s", "attr": "summary"}
    options["description"] = {"short": "d", "attr": "description", "none": True}
    options["datetime range"] = {"short": "t"}
    options["repeat"] = {"short": "p"}
    options["location"] = {"short": "l", "attr": "location", "none": True}
    options["categories"] = {"short": "c", "attr": "categories", "none": True}
    options["alarm"] = {"short": "a"}
    options["Delete"] = {"short": "D"}
    if allow_quit:
        options["quit"] = {"short":  "q"}

    now = datetime.now()

    while True:
        choice = present_options(options, prefix="Edit?", width=width)
        if choice is None:
            echo("unknown choice")
            continue
        if choice == "no":
            return True
        if choice == "quit":
            return False

        edited = False

        if choice == "Delete":
            if confirm("Delete all occurances of event?"):
                collection.delete(event.href, event.etag, event.calendar)
                return True
        elif choice == "datetime range":
            current = event.format("{start} {end}", relative_to=now)
            value = prompt("datetime range", default=current)
            try:
                start, end, allday = utils.guessrangefstr(value, locale, default_timedelta="60m")
                event.update_start_end(start, end)
                edited = True
            except:
                echo("error parsing range")
        elif choice == "repeat":
            recur = event.recurobject
            freq = recur["freq"] if "freq" in recur else ""
            until = recur["until"] if "until" in recur else ""
            if not freq:
                freq = 'None'
            freq = prompt('frequency (or "None")', freq)
            if freq == 'None':
                event.update_rrule(None)
            else:
                until = prompt('until (or "None")', until)
                if until == 'None':
                    until = None
                rrule = utils.rrulefstr(freq, until, locale)
                event.update_rrule(rrule)
            edited = True
        elif choice == "alarm":
            default_alarms = []
            for a in event.alarms:
                s = utils.timedelta2str(-1 * a[0])
                default_alarms.append(s)

            default = ', '.join(default_alarms)
            if not default:
                default = 'None'
            alarm = prompt('alarm (or "None")', default)
            if alarm == "None":
                alarm = ""
            alarm_list = []
            for a in alarm.split(","):
                alarm_trig = -1 * utils.guesstimedeltafstr(a.strip())
                new_alarm = (alarm_trig, event.description)
                alarm_list += [new_alarm]
            event.update_alarms(alarm_list)
            edited = True
        else:
            attr = options[choice]["attr"]
            default = getattr(event, attr)
            question = choice

            allow_none = False
            if "none" in options[choice] and options[choice]["none"]:
                question += ' (or "None")'
                allow_none = True
                if not default:
                    default = 'None'

            value = prompt(question, default)
            if allow_none and value == "None":
                value = ""
            getattr(event, "update_" + attr)(value)
            edited = True

        if edited:
            event.increment_sequence()
            collection.update(event)
Esempio n. 12
0
 def test_garbage(self):
     with pytest.raises(ValueError):
             guesstimedeltafstr('10mbar')
Esempio n. 13
0
 def test_moregarbage(self):
     with pytest.raises(ValueError):
             guesstimedeltafstr('foo10m')
Esempio n. 14
0
 def test_multi_nospace(self):
     assert timedelta(days=1, hours=-3, minutes=10) == \
         guesstimedeltafstr('1D-3hour10m')
Esempio n. 15
0
 def test_multi(self):
     assert timedelta(days=1, hours=-3, minutes=10) == \
         guesstimedeltafstr(' 1d -3H 10min ')
Esempio n. 16
0
 def test_negative(self):
     assert timedelta(minutes=-10) == guesstimedeltafstr('-10m')
Esempio n. 17
0
 def test_same(self):
     assert timedelta(minutes=20) == \
         guesstimedeltafstr('10min 10minutes')
Esempio n. 18
0
 def test_single(self):
     assert timedelta(minutes=10) == guesstimedeltafstr('10m')
Esempio n. 19
0
def edit_event(event, collection, locale, allow_quit=False, width=80):
    options = OrderedDict()
    if allow_quit:
        options["no"] = {"short": "n"}
    else:
        options["done"] = {"short": "n"}
    options["summary"] = {"short": "s", "attr": "summary"}
    options["description"] = {
        "short": "d",
        "attr": "description",
        "none": True
    }
    options["datetime range"] = {"short": "t"}
    options["repeat"] = {"short": "p"}
    options["location"] = {"short": "l", "attr": "location", "none": True}
    options["categories"] = {"short": "c", "attr": "categories", "none": True}
    options["alarm"] = {"short": "a"}
    options["Delete"] = {"short": "D"}
    if allow_quit:
        options["quit"] = {"short": "q"}

    now = datetime.now()

    while True:
        choice = present_options(options, prefix="Edit?", width=width)
        if choice is None:
            echo("unknown choice")
            continue
        if choice == "no":
            return True
        if choice == "quit":
            return False

        edited = False

        if choice == "Delete":
            if confirm("Delete all occurences of event?"):
                collection.delete(event.href, event.etag, event.calendar)
                return True
        elif choice == "datetime range":
            current = event.format("{start} {end}", relative_to=now)
            value = prompt("datetime range", default=current)
            try:
                start, end, allday = utils.guessrangefstr(value, locale)
                event.update_start_end(start, end)
                edited = True
            except:
                echo("error parsing range")
        elif choice == "repeat":
            recur = event.recurobject
            freq = recur["freq"] if "freq" in recur else ""
            until = recur["until"] if "until" in recur else ""
            if not freq:
                freq = 'None'
            freq = prompt('frequency (or "None")', freq)
            if freq == 'None':
                event.update_rrule(None)
            else:
                until = prompt('until (or "None")', until)
                if until == 'None':
                    until = None
                rrule = utils.rrulefstr(freq, until, locale)
                event.update_rrule(rrule)
            edited = True
        elif choice == "alarm":
            default_alarms = []
            for a in event.alarms:
                s = utils.timedelta2str(-1 * a[0])
                default_alarms.append(s)

            default = ', '.join(default_alarms)
            if not default:
                default = 'None'
            alarm = prompt('alarm (or "None")', default)
            if alarm == "None":
                alarm = ""
            alarm_list = []
            for a in alarm.split(","):
                alarm_trig = -1 * utils.guesstimedeltafstr(a.strip())
                new_alarm = (alarm_trig, event.description)
                alarm_list += [new_alarm]
            event.update_alarms(alarm_list)
            edited = True
        else:
            attr = options[choice]["attr"]
            default = getattr(event, attr)
            question = choice

            allow_none = False
            if "none" in options[choice] and options[choice]["none"]:
                question += ' (or "None")'
                allow_none = True
                if not default:
                    default = 'None'

            value = prompt(question, default)
            if allow_none and value == "None":
                value = ""
            getattr(event, "update_" + attr)(value)
            edited = True

        if edited:
            event.increment_sequence()
            collection.update(event)
Esempio n. 20
0
 def test_single(self):
     assert timedelta(minutes=10) == guesstimedeltafstr('10m')