1. Thomas Noe
  2. Todo-py

Commits

Thomas Noe  committed 42be0bb

init commit

  • Participants
  • Branches default

Comments (0)

Files changed (2)

File README

View file
+Python todo script
+
+TODO:
+	argument parsing
+	project support
+	and more.

File todo.py

View file
+#!/usr/bin/python2
+
+import UserDict
+import logging
+import cPickle as pick
+
+LOG = logging.getLogger('todo')
+handle = logging.FileHandler(".todo.log")
+LOG.addHandler(handle)
+
+class Entry(UserDict.UserDict):
+	
+	def __init__(self, eid=0, content='', status='i'):
+		UserDict.UserDict.__init__(self)
+		self['eid'] = eid 
+		self['content'] = content
+		if not status in ['i', 'c']:
+			self['status'] = 'i'
+		else:
+			self['status']  = status
+
+	def __str__(self):
+		return 'ID (%d) Task: %s   STATUS: [ %s ]' % (self['eid'], self['content'], self['status'])
+
+	def __cmp__(self, other):
+		if self['eid'] > other['eid']:
+			return True
+		else:
+			return False
+
+class Db(object):
+
+	def __init__(self, tasks=[]):
+		self.tasks = tasks
+
+	def __str__(self):
+		if not len(self.tasks):
+			return ""
+		temp = ""
+		for task in sorted(self.tasks):
+			temp += str(task) + '\n'
+		return temp
+	
+	def __len__(self):
+		return len(self.tasks)
+
+	def add(self, task, status=0):
+		if not isinstance(task, Entry):
+			raise Exception, "Cannot add type %s" % (type(task))
+		self.tasks.append(task)
+
+	def rem(self, index):
+		try:
+			self.tasks.pop(index)
+		except IndexError:
+			LOG.warning("IndexError @ index %d" % index)
+
+def ghetto_len(fn):
+	with open(fn, 'rb') as f:
+		
+		length = 0
+		for line in f.readlines():
+			length += len(str(line))
+	
+	return length
+
+def main():
+	
+	
+	todo_file = '.todo.pkl'
+	
+	prompt =  """
+	Todo:
+	# # # # # # # # # #
+	
+	p) print tasklist 
+	a) add entry
+	r) remove task
+	q) quit
+
+	) == >  """
+	
+		
+	try:
+		if ghetto_len(todo_file) > 1:
+			with open(todo_file, 'rb') as f:
+				db = pick.load(f)	
+		else:
+			raise Exception
+	except:
+		db = Db()
+
+	choice = raw_input(prompt).lower()
+	while choice != 'q':
+
+		if choice == 'p':
+			print '\t' + str(db)
+		
+		elif choice == 'a':
+			foo = raw_input("\tTask > ")
+			stat = raw_input("\tStatus of task %s (c for complete, i for incomplete\n\t > " % foo)
+			db.add(Entry(len(db), foo, stat))
+
+		elif choice == 'r':
+			try:
+				foo = input("\tIndex to remove > ")
+				db.rem(foo)
+			
+			except:
+				LOG.warning("Exception: Invalid input (remove)")
+
+		else:
+			print "\tError: %s is not a valid option" % choice
+
+		choice = raw_input(prompt).lower()
+
+	with open(todo_file, 'wb') as f:
+		pick.dump(db, f)
+
+	print "\tExiting"
+if __name__ == '__main__':
+	main()