Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
181 lines (163 sloc) 6.34 KB
from stuff import Task, Backend, Stuff
import sys, os, re, hashlib, time
# howm backend
class HowmTask(Task):
def __init__(self,uid,filename,line):
Task.__init__(self,uid)
self.filename = filename
self.line = line
self.dueFlag = ""
class HowmStuff(Backend):
# TODO: handle different howm header characters
# TODO: handle howm task content
def __init__(self,howmDir="~/howm/"):
self.howmDir=howmDir
self.notePattern = r"^\* .*$"
self.taskPattern = r"^\*.*\[[0-9]{4}.*\]:?[~!@+-].*$"
self.pendingPattern = r"^\*.*\[[0-9]{4}.*\][~!@+-].*$"
self.finishedPattern = r"^\*.*\[[0-9]{4}.*\]:[~!@+-].*$"
self.cancelledPattern = r"^\*.*\[[0-9]{4}.*\]\. cancel .*$"
self.descPattern = r"\[[0-9]{4}.*\]:{0,1}[~!@+-][0-9]*(.*)$"
self.startPattern = r"^(\[[0-9 -:]{4,16}\]) "
self.endPattern = r"(\[[0-9 -:]{4,16}\])\."
self.duePattern = r"(\[[0-9 -:]{4,16}\]):{0,1}([!@~+-]([0-9]*))"
self.elapsedPattern = r"\[ET: ([0-9]+)\]"
self.tagPattern = r"<<<"
self.dateFormats = ["[%Y-%m-%d %H:%M]","[%Y-%m-%d]"]
def uid(self):
return "howm-stuff"
def _howmFiles(self,path):
res = []
for filename in os.listdir(path):
fullPath = path+filename
if os.path.isfile(fullPath) and os.path.splitext(filename)[1]==".howm":
res.append(fullPath)
elif os.path.isdir(fullPath):
res.extend(self._howmFiles(fullPath+'/'))
return res
def _howmFileTasks(self,filename):
res = []
index = 1
newTask = False
taskLines = []
for line in open(filename).readlines():
if newTask and re.search(self.notePattern,line): # end of task
res.append([taskLines,filename])
newTask = False
taskLines = []
if re.search(self.taskPattern,line): # new task
newTask = True
if newTask: # reading task
taskLines.append([line,index])
index += 1
if newTask:
res.append([taskLines,filename])
return res
def _retrieveHowmTasks(self):
res = []
for filename in self._howmFiles(self.howmDir):
res.extend(self._howmFileTasks(filename))
return res
def _howmTaskId(self,taskStr,filename,line):
taskHash = hashlib.md5()
taskHash.update(taskStr)
taskHash.update(filename)
taskHash.update(str(line))
return taskHash.hexdigest()
def _parseHowmDate(self,dateStr):
date = None
for fmt in self.dateFormats:
try:
date = time.strptime(dateStr,fmt)
except ValueError:
pass
return date
def _parseHowmTags(self,tagsStr):
tagStr = tagsStr.lstrip(" \t<").rstrip(" \t\n")
tags = re.split(self.tagPattern,tagStr)
return tags
def _parseHowmTask(self,taskLines,filename):
taskStr = taskLines[0][0]
line = taskLines[0][1]
t = HowmTask(self._howmTaskId(taskStr,filename,line),filename,line)
t.addNote("Infos",[filename+":"+str(line)])
if re.search(self.pendingPattern,taskStr):
t.status=Stuff.PENDING
elif re.search(self.cancelledPattern,taskStr):
t.status=Stuff.CANCELLED
elif re.search(self.finishedPattern,taskStr):
t.status=Stuff.FINISHED
else:
t.status=Stuff.UNKNOWN
taskStr=taskStr.rstrip(" \n").lstrip("* ")
descMatch = re.search(self.descPattern,taskStr)
if descMatch:
t.desc = descMatch.group(1).lstrip(" ").rstrip(" ")
startMatch = re.search(self.startPattern,taskStr)
if startMatch:
t.startDate = self._parseHowmDate(startMatch.group(1))
endMatch = re.search(self.endPattern,taskStr)
if endMatch:
t.endDate = self._parseHowmDate(endMatch.group(1))
dueMatch = re.search(self.duePattern,taskStr)
if dueMatch:
t.dueDate = self._parseHowmDate(dueMatch.group(1))
t.dueFlag = dueMatch.group(2)
elapsedMatch = re.search(self.elapsedPattern,taskStr)
if elapsedMatch:
t.timeSoFar = int(elapsedMatch.group(1))
currentTitle = ""
currentNote = []
for it in taskLines[1:]:
line = it[0].rstrip("\t\n").lstrip(" \t")
if line.startswith("<<<"):
if currentNote:
t.addNote(currentTitle,currentNote)
currentTitle=""
currentNote=[]
t.tags = self._parseHowmTags(line)
elif line.startswith("*"):
if currentNote or currentTitle:
t.addNote(currentTitle,currentNote)
currentNote=[]
currentTitle = line.lstrip("*")
else:
currentNote.append(line)
if currentNote or currentTitle:
t.addNote(currentTitle,currentNote)
currentTitle=""
currentNote=[]
return t
def allTasks(self):
res = []
for taskLines,filename in self._retrieveHowmTasks():
res.append(self._parseHowmTask(taskLines,filename))
return res
def writeTask(self,task):
dateFormat = self.dateFormats[0]
taskStr = "* "
if task.startDate:
taskStr+=time.strftime(dateFormat,task.startDate)+" "
taskStr+="[ET: "+str(task.timeSoFar)+"] "
if task.endDate:
taskStr+=time.strftime(dateFormat,task.endDate)+". "
if task.cancelDate:
taskStr+=time.strftime(dateFormat,task.cancelDate)+". cancel "
if task.dueDate:
taskStr+=time.strftime(dateFormat,task.dueDate)
if task.endDate or task.cancelDate:
taskStr+=":"
taskStr+=task.dueFlag+" "
taskStr+=task.desc
print "Writing task: ",taskStr
# TODO: better file rewriting
os.rename(task.filename,task.filename+".stuff-backup")
f = open(task.filename,"w")
index = 1
for line in open(task.filename+".stuff-backup").readlines():
if index == task.line:
f.write(taskStr+"\n")
else:
f.write(line)
index += 1
os.remove(task.filename+".stuff-backup")
Jump to Line
Something went wrong with that request. Please try again.