def add(openfile=open): """ Add a task to the todofile """ with openfile(todofilefromconfig(), "r") as todofile: lines = todofile.readlines() # index calculation if len(lines) > 0: last_line = lines[len(lines) - 1] index = int(last_line.split()[0]) + 1 else: index = 1 with openfile(todofilefromconfig(), "a") as todofile: # loop on the indexes for task in compute_args().add: # check format : words* #tag1 #tag2 : task at free format, # tags in one word prefixed by # if not re.findall("^([^#]|([^ ]#))*( #[^ #]+)*$", task): printwarning( "the task has not a valid format - " + task ) else: # adding task to the todolist todofile.write(str(index) + " " + task + "\n") printinfo( "task is added to the todolist - " + str(index) + " " + task ) index = index + 1
def backup(openfile=open): """ Backup the todofile """ dir_exists = os.path.exists(todobackupfolderfromconfig()) if not dir_exists: try: os.makedirs(todobackupfolderfromconfig()) except PermissionError: printerror( "permission error to create the backup folder : " + todobackupfolderfromconfig() ) sys.exit() printinfo("creating todolist backup folder") time_suffix = time.strftime("%Y%m%d%H%M%S") todo_backup_name = "todo" + time_suffix backup_name = os.path.join(todobackupfolderfromconfig(),todo_backup_name) try: copyfile(todofilefromconfig(), backup_name) except PermissionError: printerror( "permission error to create the backup folder : " + todobackupfolderfromconfig() ) sys.exit() printinfo("creating todolist backup - " + backup_name)
def untagone(index, tagtodel, openfile): """ Untag on task """ index_trouve = False with openfile(todofilefromconfig(), "r") as todofile: lines = todofile.readlines() with openfile(todofilefromconfig(), "w") as todofile: for line in lines: if not re.findall("^" + index + " ", line): todofile.write(line) else: if re.findall( " #" + re.escape(tagtodel) + REGEX_SPACE_OR_ENDLINE, line.rstrip("\n"), ): newline = re.sub( "#" + re.escape(tagtodel) + REGEX_SPACE_OR_ENDLINE, "", line, ).rstrip("\n") todofile.write(newline.rstrip() + "\n") printinfo("tag deleted from the task of the todolist - " + line.rstrip("\n") + " -> " + newline.rstrip()) else: todofile.write(line) printwarning( "no tags is deleted from the todolist for the task - " + line.rstrip("\n")) index_trouve = True if not index_trouve: printwarning("no task with index - " + index)
def sort(openfile=open): """ Reorder the todofile with consecutives indexes """ empty = True index = 1 with openfile(todofilefromconfig(), "r") as todofile: lines = todofile.readlines() with openfile(todofilefromconfig(), "w") as todofile: for line in lines: # we replace the existing index by the current index that we increment replaced = re.sub("^\\d+ ", str(index) + " ", line) index = index + 1 todofile.write(replaced) empty = False if empty: printwarning("the todolist is empty - nothing to do") else: printinfo("the todolist is sorted")
def tagone(index, tagtoadd, openfile): """ Tag on task """ index_trouve = False with openfile(todofilefromconfig(), "r") as todofile: lines = todofile.readlines() with openfile(todofilefromconfig(), "w") as todofile: for line in lines: if not re.findall("^" + index + " ", line): todofile.write(line) else: newline = line.rstrip("\n") + " #" + tagtoadd todofile.write(newline + "\n") printinfo( "tag added to the task of the todolist - " + line.rstrip("\n") + " -> " + newline.rstrip() ) index_trouve = True if not index_trouve: printwarning("no task with index - " + index)
def check(openfile=open): """ Check the toodofile """ file_exists = os.path.isfile(todofilefromconfig()) if file_exists: try: with openfile(todofilefromconfig(), "r") as todofile: error = False for line in todofile.readlines(): # verification regex, index + task + possible tags if not re.findall( "^\\d+ ([^#]|([^ ]#))*( #[^ #]+)*$", line.rstrip("\n"), ): printwarning( "this line has not a valid format in todo file - " + line.rstrip("\n")) error = True if error: printerror("verify the todo file.") sys.exit() return True except PermissionError: printerror("permission error to open the todofile : " + todofilefromconfig()) sys.exit() printinfo("creating todo file") try: f = openfile(todofilefromconfig(), "a") f.close except FileNotFoundError: printerror("the path " + todofilefromconfig() + " does not exist, correct it (in the config file)") sys.exit() return True