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 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 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 version(): """ entry point for --version """ print("version pypodo : " + pkg_resources.get_distribution("pypodo").version ) print("location of todo file : " + todofilefromconfig()) print("location of config file : " + os.path.join(get_user_config_directory_pypodo(), TODO_RC_FILE)) print("location of backup folder : " + todobackupfolderfromconfig())
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