def tag(openfile=open): """ Tag tasks from the todofile """ if len(compute_args().tag) >= 2: tagtoadd = compute_args().tag[0] if not re.findall("^[^ #]+$", tagtoadd): printerror("the tag has not a valid format - " + tagtoadd) else: # loop on the indexes for increment in range(1, len(compute_args().tag)): index = compute_args().tag[increment] # check the numeric format of the index if re.findall(REGEX_INDEX, index): tagone(index, tagtoadd, openfile) elif re.findall("^\\d+-\\d+$", index) and int( index.split("-")[0] ) < int(index.split("-")[1]): for indexlist in range( int(index.split("-")[0]), int(index.split("-")[1]) + 1, ): tagone(str(indexlist), tagtoadd, openfile) else: printwarning( "the index to tag is not in numeric format - " + index ) else: printerror( "2 or more parameters is needed for pypodo tag : the tag to add and" " the indexes of the task whose tags to add" )
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 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 listtask(openfile=open): """ Print the todofile with filters or not """ empty = True headers = ["index", "task", "tags"] data = [] with openfile(todofilefromconfig(), "r") as todofile: for line in todofile.readlines(): # without filter -> we print all display = True if compute_args().filter: for tagtofilter in compute_args().filter: # regex to search tags "#toto " or "#toto" at the end of the line if not re.findall( " #" + re.escape(tagtofilter) + REGEX_SPACE_OR_ENDLINE, line.rstrip("\n"), ): display = False if compute_args().exclude: for tagtoexclude in compute_args().exclude: # regex to search tags "#toto " or "#toto" at the end of the line if re.findall( " #" + re.escape(tagtoexclude) + REGEX_SPACE_OR_ENDLINE, line.rstrip("\n"), ): display = False if compute_args().warning: isalert = False for part in line.split(): if (part.startswith("#") and part in listalerttags() or test_date(part[1:]) == "alert" or test_date(part[1:]) == "warning"): isalert = True if isalert == False: display = False if display: data.append(printlinetodo(line)) empty = False if empty: if compute_args().filter: printwarning("the filtered todolist is empty") else: printwarning("the todolist is empty") else: if compute_args().condensate: table = columnar(data, no_borders=True, wrap_max=0) else: if (read_config_boolean("FONCTIONAL", "condensate", "False") == "True"): table = columnar(data, no_borders=True, wrap_max=0) else: table = columnar(data, headers, no_borders=False, wrap_max=0) print(table)
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