Exemple #1
0
def readFile(path):
	"""Reads a file from disk and parses it into a task list."""

	tasksFile = open(path, 'r')
	tasklist = Tasklist(os.path.splitext(os.path.basename(path))[0])
	parentStack = [] #Initialize the stack of parent tasks

	task = None

	for line in tasksFile:
		deepness = 1
		line = line.rstrip() #Remove the trailing whitespaces (newlines, mainly)

		while line.startswith("\t"): #starts with tab
			deepness += 1		#Add a deepness level to the current line
			line = line[1:] #Remove the leading tab

		if line.startswith("- ") or line.endswith(':'): #It's a task or a project (both are saved as tasks)
			if task is not None:		#There's a previous task	
				tasklist.append(task) #Commit previous task to the list

			if (getLast(parentStack) is None and deepness >= 1) or	deepness > getLast(parentStack)[DEEP]:
						parentStack.append({TASK:task, DEEP:deepness}) #This'll be parent to the next ones
			else:
				if deepness < getLast(parentStack)[DEEP]:
					parentStack.pop() #Up one level

			#Get the new task's parent from the stack
			parent = getLast(parentStack)
			if parent is not None:
				parent = parent[TASK]

			#Create the task object
			if line.startswith("- "): #It's a regular task
				taskParts = parseTask(line[2:])
				task = Task(taskParts[TITLE], '', parent)
				task.addAttributeList(taskParts[TAGS])
			else:	#It's a project
				task = Task(line[:-1], '', parent) #Remove ':' from the task title
				task.addAttribute(properties.TASKPAPER_ISPROJECT_ATTR, '') #Mark it as a project

		else:
			if task is not None: 
				task.notes += line

	#Put last task in the list
	if task is not None:
		tasklist.append(task)


	return tasklist