From f328e7a902a293f28a5e1dde079d16e18da5692c Mon Sep 17 00:00:00 2001 From: Mysterious-Owl <66870959+Mysterious-Owl@users.noreply.github.com> Date: Wed, 5 Oct 2022 16:17:28 +0530 Subject: [PATCH] Adding ToDo CLI program --- .gitignore | 2 + scripts/todo_cli/README.md | 14 +++ scripts/todo_cli/archive.txt | 1 + scripts/todo_cli/done.txt | 1 + scripts/todo_cli/todo.bat | 3 + scripts/todo_cli/todo.py | 180 +++++++++++++++++++++++++++++++++++ scripts/todo_cli/todo.txt | 1 + 7 files changed, 202 insertions(+) create mode 100644 scripts/todo_cli/README.md create mode 100644 scripts/todo_cli/archive.txt create mode 100644 scripts/todo_cli/done.txt create mode 100644 scripts/todo_cli/todo.bat create mode 100644 scripts/todo_cli/todo.py create mode 100644 scripts/todo_cli/todo.txt diff --git a/.gitignore b/.gitignore index b6e4761..872c981 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ + +.idea diff --git a/scripts/todo_cli/README.md b/scripts/todo_cli/README.md new file mode 100644 index 0000000..8b0667e --- /dev/null +++ b/scripts/todo_cli/README.md @@ -0,0 +1,14 @@ +## ToDo CLI program +####Main Script - +todo.py +#### Files to store ToDos - +1. todo.txt +2. done.txt +3. archive.txt + +#### Note - +todo.bat is a bash script, add its path to OS environment variables and change path in python and bash files +to absolute path to use this using CMD from anywhere. + +#### Dependency - +None \ No newline at end of file diff --git a/scripts/todo_cli/archive.txt b/scripts/todo_cli/archive.txt new file mode 100644 index 0000000..f2dae3c --- /dev/null +++ b/scripts/todo_cli/archive.txt @@ -0,0 +1 @@ +2022-10-05 hacktoberfestpython diff --git a/scripts/todo_cli/done.txt b/scripts/todo_cli/done.txt new file mode 100644 index 0000000..9a05627 --- /dev/null +++ b/scripts/todo_cli/done.txt @@ -0,0 +1 @@ +2021-10-05 hackoctober 2021 diff --git a/scripts/todo_cli/todo.bat b/scripts/todo_cli/todo.bat new file mode 100644 index 0000000..bdafc73 --- /dev/null +++ b/scripts/todo_cli/todo.bat @@ -0,0 +1,3 @@ +@echo off + +python "scripts/todo_cli/todo.py" %* diff --git a/scripts/todo_cli/todo.py b/scripts/todo_cli/todo.py new file mode 100644 index 0000000..29deb8a --- /dev/null +++ b/scripts/todo_cli/todo.py @@ -0,0 +1,180 @@ +import time +import sys +import os + +arg = sys.argv +todo_file = "todo.txt" +done_file = "done.txt" +archive_file = "archive.txt" + + +def option_help(): + sys.stdout.buffer.write(''' + Commands : + add \"todo item\"\t\t\tAdd a new todo. + ls\t\t\t\tShow remaining todos. + del NUMBER\t\t\tDelete a todo. + done NUMBER\t\t\tComplete a todo. + help\t\t\t\tShow usage. + report\t\t\t\tStatistics. + archive\t\t\t\tArchives all the completed task + + General Options + -h, --help\t\t\tShow help. + -v, --version\t\t\tShow version and exit. + -p, --print \t\tPrints the file: (t)odo, (d)one, (a)rchive + '''.encode('utf8')) + + +def option_add(todo_to_add): + todo_add = " ".join(todo_to_add) + with open(todo_file, "a") as f1: + f1.write(todo_add + '\n') + print(f"Added todo: \"{todo_add}\"") + + +def option_ls(): + todo = [] + try: + with open(todo_file, "r") as f1: + for i in f1: + todo.append(i) + for i in range(len(todo) - 1, -1, -1): + sys.stdout.buffer.write("[{}] {}\n".format(i + 1, todo[i][:-1]).encode('utf8')) + except FileNotFoundError: + print("There are no pending todos!") + + +def option_del(todo_to_delete): + try: + with open(todo_file, "r") as f1: + todo = f1.readlines() + if todo_to_delete > len(todo) or todo_to_delete == 0: + print(f"Error: todo #{todo_to_delete} does not exist. Nothing deleted.") + else: + todo.remove(todo[todo_to_delete - 1]) + with open(todo_file, "w") as f1: + for i in todo: + f1.write(i) + print("Deleted todo #{}".format(todo_to_delete)) + except FileNotFoundError: + print("There are no pending todos! Nothing deleted.") + except ValueError: + print("Error: Enter a NUMBER. Nothing deleted.") + + +def option_done(mark_done): + try: + with open(todo_file, "r") as f1: + todo = f1.readlines() + if mark_done > len(todo) or mark_done == 0: + print("Error: todo #{} does not exist.".format(mark_done)) + return + + dodo = todo[mark_done - 1] + todo.remove(dodo) + with open(todo_file, "w") as f1, open(done_file, "a") as f2: + for i in todo: + f1.write(i) + t = time.localtime() + f2.write(f"{t.tm_year}-{t.tm_mon:02}-{t.tm_mday:02} {dodo}") + print(f"Marked todo #{mark_done} as done.") + print(f"Yay! Only {len(todo)} task(s) are left.") + except FileNotFoundError: + print("There are no pending todos! Nothing marked Done.") + except ValueError: + print("Error: Enter a NUMBER. Nothing marked Done.") + + +def option_report(): + len_todo = 0 + len_done = 0 + t = time.localtime() + try: + with open(todo_file, "r") as f1: + len_todo = len(f1.readlines()) + except FileNotFoundError: + pass + try: + with open(done_file, "r") as f2: + len_done = len(f2.readlines()) + except FileNotFoundError: + pass + print(f"{t.tm_year}-{t.tm_mon:02}-{t.tm_mday:02} Pending : {len_todo} Completed : {len_done}") + + +def option_archive(): + try: + with open(done_file, 'r') as f2: + todo = f2.readlines() + os.remove(done_file) + with open(archive_file, 'a') as f3: + for i in todo: + f3.write(i) + print("All task completed are archived") + except FileNotFoundError: + print("Error: No task done to archive. Complete the pending task now!") + + +def option_print(file_code): + + if file_code == 't': + try: + with open(todo_file, 'r') as f1: + print('\nToDo List:') + for i in f1: + print(i[:-1]) + except FileNotFoundError: + print("There are no pending todos!") + elif file_code == 'd': + try: + with open(done_file, 'r') as f2: + print('\nDone List:') + for i in f2: + print(i[:10], '\t', i[10:-1]) + except FileNotFoundError: + print("Nothing has been marked as done") + elif file_code == 'a': + try: + with open(archive_file, 'r') as f3: + print('\nArchive List:') + for i in f3: + print(i[:10], '\t', i[10:-1]) + except FileNotFoundError: + print("Nothing in archives") + else: + print("Enter the correct argument") + + +if len(arg) == 1 or arg[1] in ["help", '-h', "--help"]: + option_help() +elif arg[1] == "add": + if len(arg) == 2: + print("Error: Missing todo string. Nothing added!") + else: + option_add(arg[2:]) +elif arg[1] == "ls": + option_ls() +elif arg[1] == "del": + if len(arg) < 3: + print("Error: Missing NUMBER for deleting todo.") + else: + option_del(int(arg[2])) +elif arg[1] == "done": + if len(arg) == 2: + print("Error: Missing NUMBER for marking todo as done.") + else: + option_done(int(arg[2])) +elif arg[1] == "report": + option_report() +elif arg[1] == "--version" or arg[1] == '-v': + print("ToDo 1.0") +elif arg[1] == "archive": + option_archive() +elif arg[1] == "--print" or arg[1] == '-p': + if len(arg) < 3: + print("No file name entered!") + else: + option_print(arg[2]) +else: + print("ERROR: unknown command \"{}\"".format(arg[1])) diff --git a/scripts/todo_cli/todo.txt b/scripts/todo_cli/todo.txt new file mode 100644 index 0000000..1ea3835 --- /dev/null +++ b/scripts/todo_cli/todo.txt @@ -0,0 +1 @@ +hackoctberfest 2022