示例#1
0
    def is_due(self, margin=timedelta(minutes=5)):
        """Returns True if the dataset's schedule demands a new parcel.

        The margin parameter will be subtracted from the scheduled interval,
        making the dataset due sooner than it otherwise would be. This is so
        that if you're running this tool via cron on, say, a daily schedule,
        and your exports are set to "1 day" intervals, they'll still be "due"
        even if the previous export actually happened slightly less than a day
        ago.

        Due date is determined using the date of the most recent extant
        complete parcel (incomplete parcels are NOT counted) and the
        "interval" property in config.toml.
        If interval is not configured, this method returns False.

        Interval should be a string such as "1 day", "3 hours", etc.
        """
        cfg = self.read_config()
        delta_str = cfg.get('interval', None)
        if not delta_str:
            return False
        delta = _interval.parse_delta(delta_str) - margin

        pas = [pa for pa in self.parcel_accessors() if pa.is_complete()]
        if not pas:
            return True
        last = pas[-1].datetime
        now = datetime.now(last.tzinfo)
        return (now - last) >= delta
def test_empty():
    with pytest.raises(ValueError):
        parse_delta('')
def test_short():
    assert (parse_delta('1w2d3h4m5s')
            == timedelta(weeks=1, days=2, hours=3, minutes=4, seconds=5))
def test_all():
    assert (parse_delta('1 week 2 days 3 hours 4 minutes 5 seconds')
            == timedelta(weeks=1, days=2, hours=3, minutes=4, seconds=5))
def test_comma():
    assert (parse_delta('3 minutes, 5 seconds')
            == timedelta(minutes=3, seconds=5))
def test_multi():
    assert (parse_delta('3 minutes 5 seconds')
            == timedelta(minutes=3, seconds=5))
def test_invalid_chars():
    with pytest.raises(ValueError):
        parse_delta('3 minutes + 5 seconds')
def test_blank():
    with pytest.raises(ValueError):
        parse_delta(' \t ')