Skip to content

Instantly share code, notes, and snippets.

@jeffgravitywell
Created May 1, 2015 19:29
Show Gist options
  • Save jeffgravitywell/ffff43243aca055ac8ea to your computer and use it in GitHub Desktop.
Save jeffgravitywell/ffff43243aca055ac8ea to your computer and use it in GitHub Desktop.
Today View creation script with Date math
#!/usr/bin/python
import os
import io
import sys
import shutil
import re
import string
from datetime import date, datetime, time
filelist = [
'/Users/jeffreyjh/Dropbox/Elements/tpHomeProjects.taskpaper',
'/Users/jeffreyjh/Dropbox/Elements/tpWorkProjects.taskpaper'
]
targetfile = '/Users/jeffreyjh/Dropbox/Elements/tpToday.taskpaper'
#testing block
# filelist = [
# '/Users/username/Dropbox/Elements/tpHomeProjects.taskpaper',
# '/Users/username/Dropbox/Elements/tpWorkProjects.taskpaper'
# ]
# targetfile = '/Users/username/Dropbox/Elements/tpToday.taskpaper'
d = date.today()
today = d.strftime('%Y-%m-%d')
dt = datetime.now()
todayText = 'Last Run: ' + dt.strftime('%Y-%m-%d %H:%M')
regex = '((@+\\bcritical*)|(@+\\btoday*)|(@+\\bhigh*)|(@+\\bdue\(" + today + "\)))(?!.*@done)'
isDateRegex = '@due\((.+?)\)'
isDoneRegex = '(@+\\bdone*)'
for taskfile in filelist:
# open the file for reading
currFile = open(taskfile)
todayText = todayText + '\n' + 'Filename: ' + taskfile + '\n'
currentProject = ''
project = 'None'
# loop through the lines
for line in currFile:
if line.find(':') != -1:
if line != project:
project = line
else:
pass
else:
# match the current line to the regex
match = re.search(regex, line)
dateMatch = False
# check to see if a date is present
datepresent = re.search(isDateRegex, line)
if datepresent:
#parse the date
parsedDate = re.search(isDateRegex, line).group(1)
# clean things up and get an actual date we can parse
parsedDate = parsedDate.replace('@due(', '')
parsedDate = parsedDate.replace(')', '')
parsedDate = parsedDate.replace(' ', '')
# convert it to a date object so we can do some math on it
compareDate = datetime
compareDate = datetime.strptime(parsedDate, '%Y-%m-%d')
#compare the date to today's date
if compareDate < dt:
# nice! this is a valid date but make sure it isn't @done already
isItDone = re.search(isDoneRegex, line)
if not isItDone:
dateMatch = True
if match or dateMatch:
if project == currentProject:
pass
else:
currentProject = project
todayText = todayText + '\n' + project
todayText = todayText + line
currFile.close()
os.remove(targetfile)
todayFile = open(targetfile, "w")
todayFile.write(todayText)
todayFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment