Skip to content

Commit

Permalink
Some defensive coding around date parse requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhydlewis committed Mar 8, 2020
1 parent 8a68c68 commit 77854f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
44 changes: 28 additions & 16 deletions factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
STATUS_DROPPED = 'dropped'
STATUS_INACTIVE = 'inactive'
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
DATETIME_OFFSET = 978307200

FOLDER_PREFIX = 'en.lproj/OmniFocus Help/art/'

Expand Down Expand Up @@ -96,20 +97,23 @@ def create_task(self, row):
icon = self.deferred_icon
if inbox:
icon = self.inbox_icon

if row[DUE_DATE]:
due_date = offset_date(row[DUE_DATE])
now = datetime.now()
due_date_label = due_date.strftime("%c")

if now > due_date:
name = name + " (overdue: {0})".format(due_date_label)
icon = ICON_WARNING
if row[DUE_DATE]:
due_date = parse_datetime(row[DUE_DATE])
if due_date is not None:
now = datetime.now()
due_date_label = due_date.strftime("%c")

if now > due_date:
name = name + " (overdue: {0})".format(due_date_label)
icon = ICON_WARNING
else:
name = name + " (due: {0})".format(due_date_label)
else:
name = name + " (due: {0})".format(due_date_label)
name = name + " (due date unknown)"

return Item(item_type='Task', persistent_id=pid, name=name, icon=icon, subtitle=project)

def create_context(self, raw_data):
pid = raw_data[ID]
name = raw_data[NAME]
Expand Down Expand Up @@ -149,8 +153,14 @@ def create_recent_item(self, raw_data):
else:
task.name = task.name + " (Task)"

modified_date = offset_date(raw_data[MODIFIED_DATE]).strftime("%c")
task.subtitle = modified_date
mod_date = raw_data[MODIFIED_DATE]

try:
modified_date = datetime.fromtimestamp(mod_date + DATETIME_OFFSET).strftime("%c")
task.subtitle = modified_date
except:
task.subtitle = "Unable to determine a modified date for this task {0}".format(mod_date)

return task


Expand All @@ -162,12 +172,14 @@ def deferred_date(datetostart, effectivedatetostart):
def is_deferred(datetostart):
deferred = False
if datetostart is not None:
dts = offset_date(datetostart)
dts = parse_datetime(datetostart)
if dts > datetime.now():
deferred = True

return deferred


def offset_date(value):
return datetime.strptime(value, DATETIME_FORMAT)
def parse_datetime(value):
try:
return datetime.strptime(value, DATETIME_FORMAT)
except:
return None
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ Well, I want it because I can't quickly search for, say, a task within OmniFocus
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>2.1.0</string>
<string>2.1.1</string>
<key>webaddress</key>
<string>rhydlewis.net</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.1.1

0 comments on commit 77854f6

Please sign in to comment.