Created
December 31, 2011 18:12
-
-
Save yashh/1544814 to your computer and use it in GitHub Desktop.
a simple command line todo which reads and write to a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
"""A simple command line todo for simplenoteapp synced via dropbox.""" | |
import os, sys, datetime | |
class TaskList(object): | |
def __init__(self, todo, done): | |
self.todo = todo | |
self.done = done | |
def _display_file(self, fp): | |
line = fp.readline() | |
# not really having first line dedicated to file. | |
# if not line == "%s\n" %filename: return "invalid %s file" %filename | |
taskno = 1 | |
while line: | |
if line: print "%02d %s" %(taskno, line.strip()) | |
taskno = taskno + 1 | |
line = fp.readline() | |
def list_tasks(self, text): | |
tasks = open(self.todo) | |
return self._display_file(tasks) | |
def add_task(self, text): | |
with open(self.todo, "a+") as myfile: myfile.write("- %s\n" %(text)) | |
def edit_task(self, text): | |
lineno_raw, new_task = text.split(" ", 1) | |
lineno = int(lineno_raw) | |
fro = open(self.todo, "rb") | |
current_line = 0 | |
while current_line < lineno: | |
fro.readline() | |
current_line += 1 | |
seekpoint = fro.tell() | |
frw = open(self.todo, "r+b") | |
frw.seek(seekpoint, 0) | |
frw.writelines("- %s" %new_task) | |
frw.close() | |
def mark_done(self, text): | |
lineno = int(text) | |
fro = open(self.todo, "rb") | |
current_line = 0 | |
while current_line < lineno: | |
fro.readline() | |
current_line += 1 | |
seekpoint = fro.tell() | |
frw = open(self.todo, "r+b") | |
frw.seek(seekpoint, 0) | |
# read the line we want to discard | |
done_task = fro.readline().strip() | |
# now move the rest of the lines in the file | |
# one line back | |
chars = fro.readline() | |
while chars: | |
frw.writelines(chars) | |
chars = fro.readline() | |
fro.close() | |
frw.truncate() | |
frw.close() | |
with open(self.done, "a") as myfile: | |
myfile.write("X %s %s" %(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), done_task)) | |
print "DONE" | |
def show_done(self, text): | |
tasks = open(self.done) | |
return self._display_file(tasks) | |
def remove_task(self, text): | |
raw_data_array = text.split(" ", 1) | |
if len(raw_data_array) == 1: raw_data_array = raw_data_array+["todo"] | |
lineno_raw, new_file = raw_data_array | |
lineno = int(lineno_raw) | |
if new_file == "done": ofile = self.done | |
else: ofile = self.todo | |
fro = open(ofile, "rb") | |
current_line = 0 | |
while current_line < lineno: | |
fro.readline() | |
current_line += 1 | |
seekpoint = fro.tell() | |
frw = open(ofile, "r+b") | |
frw.seek(seekpoint, 0) | |
# read the line we want to discard | |
done_task = fro.readline().strip() | |
# now move the rest of the lines in the file | |
# one line back | |
chars = fro.readline() | |
while chars: | |
frw.writelines(chars) | |
chars = fro.readline() | |
fro.close() | |
frw.truncate() | |
frw.close() | |
def not_found(self, text): | |
print "invalid option" | |
def get_action(action_abbr, tasklist): | |
action_directory = {"l": tasklist.list_tasks, | |
"a": tasklist.add_task, | |
"e": tasklist.edit_task, | |
"d": tasklist.mark_done, | |
"da": tasklist.show_done, | |
"r": tasklist.remove_task} | |
return action_directory.get(action_abbr, tasklist.not_found) | |
def main(): | |
args = sys.argv | |
if len(args) == 1: args = args+["l"] | |
text = ' '.join(args[2:]).strip() | |
tasklist = TaskList("/Users/yashh/Dropbox/notes/todo.txt", | |
"/users/yashh/Dropbox/notes/done.txt") | |
action_abbr = args[1] | |
return get_action(action_abbr, tasklist)(text) | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment