Compare commits

..

2 Commits

Author SHA1 Message Date
Sergio Martínez Portela
6b8b4de21e Use Optional for types that might be None. 2022-06-05 23:32:32 +02:00
Sergio Martínez Portela
a67bde58d0 Fix repetition on dates with start+end times. 2022-06-05 23:32:28 +02:00
3 changed files with 32 additions and 6 deletions

View File

@ -8,7 +8,7 @@ import re
import sys import sys
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from enum import Enum from enum import Enum
from typing import Generator, List, Tuple, Union from typing import Generator, List, Optional, Tuple, Union
from . import dom from . import dom
@ -779,7 +779,7 @@ class Timestamp:
self.minute = minute self.minute = minute
self.repetition = repetition self.repetition = repetition
def to_datetime(self) -> Union[datetime, date]: def to_datetime(self) -> datetime:
if self.hour is not None: if self.hour is not None:
return datetime(self.year, self.month, self.day, self.hour, self.minute) return datetime(self.year, self.month, self.day, self.hour, self.minute)
else: else:
@ -955,11 +955,15 @@ def parse_org_time_range(start, end) -> TimeRange:
class OrgTime: class OrgTime:
def __init__(self, ts: Timestamp, end_time: Union[Timestamp, None] = None): def __init__(self, ts: Timestamp, end_time: Optional[Timestamp] = None):
assert ts is not None assert ts is not None
self.time = ts self.time = ts
self.end_time = end_time self.end_time = end_time
@property
def repetition(self):
return self.time.repetition
@property @property
def duration(self): def duration(self):
if self.end_time is None: if self.end_time is None:
@ -982,6 +986,10 @@ class OrgTime:
else: else:
return None return None
repetition = None
if m.group("repetition"):
repetition = m.group("repetition").strip()
if m.group("end_hour"): if m.group("end_hour"):
return OrgTime( return OrgTime(
Timestamp( Timestamp(
@ -992,6 +1000,7 @@ class OrgTime:
m.group("dow"), m.group("dow"),
int(m.group("start_hour")), int(m.group("start_hour")),
int(m.group("start_minute")), int(m.group("start_minute")),
repetition=repetition,
), ),
Timestamp( Timestamp(
active, active,
@ -1013,7 +1022,7 @@ class OrgTime:
m.group("dow"), m.group("dow"),
int(m.group("start_hour")) if m.group("start_hour") else None, int(m.group("start_hour")) if m.group("start_hour") else None,
int(m.group("start_minute")) if m.group("start_minute") else None, int(m.group("start_minute")) if m.group("start_minute") else None,
m.group("repetition").strip() if m.group("repetition") else None, repetition=repetition,
) )
) )
@ -1026,7 +1035,7 @@ def timerange_to_string(tr: TimeRange):
return tr.start_time.to_raw() + "--" + tr.end_time.to_raw() return tr.start_time.to_raw() + "--" + tr.end_time.to_raw()
def timestamp_to_string(ts: Timestamp, end_time: Union[Timestamp, None] = None) -> str: def timestamp_to_string(ts: Timestamp, end_time: Optional[Timestamp] = None) -> str:
date = "{year}-{month:02d}-{day:02d}".format( date = "{year}-{month:02d}-{day:02d}".format(
year=ts.year, month=ts.month, day=ts.day year=ts.year, month=ts.month, day=ts.day
) )
@ -1047,7 +1056,7 @@ def timestamp_to_string(ts: Timestamp, end_time: Union[Timestamp, None] = None)
base=base, hour=end_time.hour, minute=end_time.minute base=base, hour=end_time.hour, minute=end_time.minute
) )
if ts.repetition: if ts.repetition is not None:
base = base + " " + ts.repetition base = base + " " + ts.repetition
if ts.active: if ts.active:

View File

@ -19,3 +19,6 @@ SCHEDULED: <2020-12-12 Sáb> CLOSED: <2020-12-13 Dom> DEADLINE: <2020-12-14 Lun>
** Scheduled for time range ** Scheduled for time range
SCHEDULED: <2020-12-15 Mar 00:05-00:10> SCHEDULED: <2020-12-15 Mar 00:05-00:10>
** Scheduled periodic
SCHEDULED: <2020-12-15 Mar 00:05-00:10 ++1w>

View File

@ -418,6 +418,20 @@ class TestSerde(unittest.TestCase):
Timestamp(True, 2020, 12, 15, "Mar", 0, 10), Timestamp(True, 2020, 12, 15, "Mar", 0, 10),
) )
hl_schedule_range = hl.children[1]
self.assertEqual(
hl_schedule_range.scheduled.time,
Timestamp(True, 2020, 12, 15, "Mar", 0, 5, '++1w')
)
self.assertEqual(
hl_schedule_range.scheduled.end_time,
Timestamp(True, 2020, 12, 15, "Mar", 0, 10),
)
self.assertEqual(
hl_schedule_range.scheduled.repetition,
'++1w',
)
def test_update_info_file_05(self): def test_update_info_file_05(self):
with open(os.path.join(DIR, "05-dates.org")) as f: with open(os.path.join(DIR, "05-dates.org")) as f:
orig = f.read() orig = f.read()