diff --git a/src/processor/org_mode/orgnode.py b/src/processor/org_mode/orgnode.py index b43cc505..7aeb0e9e 100644 --- a/src/processor/org_mode/orgnode.py +++ b/src/processor/org_mode/orgnode.py @@ -66,6 +66,7 @@ def makelist(filename): closed_date = '' sched_date = '' deadline_date = '' + logbook = list() nodelist = [] propdict = dict() in_properties_drawer = False @@ -86,6 +87,9 @@ def makelist(filename): if deadline_date: thisNode.setDeadline(deadline_date) deadline_date = '' + if logbook: + thisNode.setLogbook(logbook) + logbook = list() thisNode.setProperties(propdict) nodelist.append( thisNode ) propdict = {'LINE': f'file:{normalize_filename(filename)}::{ctr}'} @@ -121,8 +125,14 @@ def makelist(filename): in_logbook_drawer=False continue - # Ignore Clocking Lines - if re.search(r'CLOCK: \[[0-9]{4}-[0-9]{2}-[0-9]{2}', line): + # Extract Clocking Lines + clocked_re = re.search(r'CLOCK:\s*\[([0-9]{4}-[0-9]{2}-[0-9]{2} [a-zA-Z]{3} [0-9]{2}:[0-9]{2})\]--\[([0-9]{4}-[0-9]{2}-[0-9]{2} [a-zA-Z]{3} [0-9]{2}:[0-9]{2})\]', line) + if clocked_re: + # convert clock in, clock out strings to datetime objects + clocked_in = datetime.datetime.strptime(clocked_re.group(1), '%Y-%m-%d %a %H:%M') + clocked_out = datetime.datetime.strptime(clocked_re.group(2), '%Y-%m-%d %a %H:%M') + # add clocked time to the entries logbook list + logbook += [(clocked_in, clocked_out)] line = "" prop_srch = re.search(r'^\s*:([a-zA-Z0-9]+):\s*(.*?)\s*$', line) @@ -150,8 +160,8 @@ def makelist(filename): int(dd_re.group(2)), int(dd_re.group(3)) ) - # Ignore property drawer, scheduled, closed, deadline and # lines from body - if not in_properties_drawer and not cd_re and not sd_re and not dd_re and line[:1] != '#': + # Ignore property drawer, scheduled, closed, deadline, logbook entries and # lines from body + if not in_properties_drawer and not cd_re and not sd_re and not dd_re and not clocked_re and line[:1] != '#': bodytext = bodytext + line # write out last node @@ -163,6 +173,8 @@ def makelist(filename): thisNode.setDeadline(deadline_date) if closed_date: thisNode.setClosed(closed_date) + if logbook: + thisNode.setLogbook(logbook) nodelist.append( thisNode ) # using the list of TODO keywords found in the file @@ -210,6 +222,7 @@ class Orgnode(object): self.deadline = "" # Deadline date self.closed = "" # Closed date self.properties = dict() + self.logbook = list() # List of clock-in, clock-out tuples representing logbook entries # Look for priority in headline and transfer to prty field @@ -336,6 +349,18 @@ class Orgnode(object): """ return self.closed + def setLogbook(self, logbook): + """ + Set the logbook with list of clocked-in, clocked-out tuples for the entry + """ + self.logbook = logbook + + def Logbook(self): + """ + Return the logbook with all clocked-in, clocked-out date object pairs or empty list if nonexistent + """ + return self.logbook + def __repr__(self): """ Print the level, heading text and tag of a node and the body diff --git a/tests/test_orgnode.py b/tests/test_orgnode.py index 85c7fda3..a516fe7f 100644 --- a/tests/test_orgnode.py +++ b/tests/test_orgnode.py @@ -63,6 +63,7 @@ Body Line 2''' assert entries[0].Closed() == datetime.date(1984,4,1) assert entries[0].Scheduled() == datetime.date(1984,4,1) assert entries[0].Deadline() == datetime.date(1984,4,1) + assert entries[0].Logbook() == [(datetime.datetime(1984,4,1,9,0,0), datetime.datetime(1984,4,1,12,0,0))] # ---------------------------------------------------------------------------------------------------- @@ -140,7 +141,7 @@ CLOSED: [1984-04-02 Sun 12:00] SCHEDULED: <1984-04-02 Sun 09:00> DEADLINE: <1984 :ID: 123-456-789-4234-0002 :END: :LOGBOOK: -CLOCK: [1984-04-01 Sun 09:00]--[1984-04-01 Sun 12:00] => 3:00 +CLOCK: [1984-04-02 Mon 09:00]--[1984-04-02 Mon 12:00] => 3:00 - Clocked Log 2 :END: Body 2 @@ -162,6 +163,7 @@ Body 2 assert entry.Closed() == datetime.date(1984,4,index+1) assert entry.Scheduled() == datetime.date(1984,4,index+1) assert entry.Deadline() == datetime.date(1984,4,index+1) + assert entry.Logbook() == [(datetime.datetime(1984,4,index+1,9,0,0), datetime.datetime(1984,4,index+1,12,0,0))] # Helper Functions