You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below is a draft for implementing branching (control multiple lists with one script). It works similar to git branching.
"""File: todo.py------------------BETA new version of Lemnos with branching. Simple command-line to-do list manager. See README.md for getting started and how to use the program. """importsysimportosimportglobfromos.pathimportexistscurr_dir_path=os.path.dirname(os.path.realpath(__file__))
BASE_PATH=os.path.join(curr_dir_path, "lists")
LIST_PATH="grfs.txt"LIST_FILE_PATH=os.path.join(BASE_PATH, LIST_PATH)
defshow_branches():
""" Note to self: didn't do indexing yet. Function to show current branches stored in ./lists. Activated by 'branch' or 'b'. """txt_files=glob.glob(BASE_PATH+'/*.txt', recursive=True)
#txt_files.sort() #idx = 0forfile_pathintxt_files:
#idx += 1branch_name=file_path[file_path.rfind("/") +1:file_path.rfind(".")]
file="- "+branch_name#file = str(idx) + ". " + branch_nameif (branch_name+".txt") ==LIST_PATH:
file=file+" (current branch)"print(file)
defcurrent_branch():
""" Show current branch name. Activated by 'branch current' or 'bc'. """txt_files=glob.glob(BASE_PATH+'/*.txt', recursive=True)
txt_files.sort()
forfile_pathintxt_files:
branch_name=file_path[file_path.rfind("/") +1:file_path.rfind(".")]
file="- "+branch_nameif (branch_name+".txt") ==LIST_PATH:
print(branch_name)
defdelete_branch(branch_name):
""" Function to delete a branch. Activated by 'branch delete' or 'bd'. """assert (branch_name+".txt") !=LIST_PATH, "cannot delete current branch"txt_files=glob.glob(BASE_PATH+'/*.txt', recursive=True)
txt_files.sort()
forfile_pathintxt_files:
curr_branch=file_path[file_path.rfind("/") +1:file_path.rfind(".")]
ifbranch_name==curr_branch:
branch_path=os.path.join(BASE_PATH, branch_name+".txt")
ifos.path.exists(branch_path):
#os.remove(branch_path)subpath=os.path.join(BASE_PATH, "archived", branch_name+".txt")
ifos.path.exists(subpath):
subpath=os.path.join(BASE_PATH, "archived", branch_name+"1.txt")
os.rename(branch_path, subpath)
else:
print("The branch does not exist")
print(f"'{branch_name}' archived successfully")
sys.exit()
print("Error: branch does not exist")
defswitch_branches(branch_name):
""" Function to switch branches stored in ./lists. Activated by 'branch switch' or 'bs'. """txt_files=glob.glob(BASE_PATH+'/*.txt', recursive=True)
txt_files.sort()
branch_exists=Falseforfile_pathintxt_files:
file=file_path[file_path.rfind("/") +1:file_path.rfind(".")]
iffile==branch_name:
branch_exists=Trueifbranch_exists==False:
print("Error: branch does not exist")
sys.exit()
f=open(__file__, 'r')
lines=f.readlines()
list_path_idx=-1idx=0forlinlines:
l=l.strip()
template_string=f'LIST_PATH = "{LIST_PATH}"'ifl==template_string:
list_path_idx=idxbreakidx+=1new_string=branch_name+'.txt'lines[list_path_idx] =f'LIST_PATH = "{new_string}"'+'\n'withopen(__file__, "w") asf:
contents="".join(lines)
f.write(contents)
print(f"Switched to branch '{branch_name}'")
defnew_branch(branch_name):
""" Function to make a new branch. Activated by "branch new" or "bn". """path_string=branch_name+".txt"new_path=os.path.join(BASE_PATH, path_string)
assertnotexists(new_path), "branch already exists"f=open(new_path, "x")
print(f"'{branch_name}' branch created")
switch_branches(branch_name)
deflength(file_obj):
""" Function that takes in the file object (pointing to list.txt) and returns the number of items in the list. """count=0forlineinfile_obj:
count+=1returncountdefadd(item_string):
""" Function that appends the item based on what the user specified as a command line argument to list.txt. """curr_num_items=length(open(LIST_FILE_PATH))
curr_index=curr_num_items+1og_string=item_string# for printing purposes item_string=str(curr_index) +". "+item_string+'\n'file=open(LIST_FILE_PATH, "a")
file.write(item_string)
file.close()
print('Added "'+og_string+'" to the to-do list.')
defshow():
""" Function that prints out the list. It also displays the number of items in the list. """file=open(LIST_FILE_PATH)
branch_str=LIST_PATH[:LIST_PATH.rfind(".")]
forlineinfile:
print(line, end='') # use end to avoid printing \n print(f"Number of items (branch: {branch_str}):", length(open(LIST_FILE_PATH)))
defsearch(query):
""" Function that searches the list for the query. Looks for exact match in the list. If query is not found, it prints nothing. """file=open(LIST_FILE_PATH)
forlineinfile:
line_list=line.split()
foreleminline_list:
ifstr(query) ==str(elem):
print(line, end='')
defshow_priority():
""" Function that prints out prioritized items in the list. It also displays the number of prioritized items in the list. """file=open(LIST_FILE_PATH)
count=0forlineinfile:
if"*"inline:
count+=1print(line, end='') # use end to avoid printing \n print("Number of prioritized items:", count)
defmake_prioritized(idx):
""" Function that prioritizes the element at idx. """# store list of lines to use later (since we will overwrite these lines) temp_file_obj=open(LIST_FILE_PATH, "r")
line_list=temp_file_obj.readlines()
temp_file_obj.close()
updated_list=open(LIST_FILE_PATH, "w")
i=1forlineinline_list:
ifi==idx:
dot_idx=line.find(".")
line=line[:dot_idx+1] +" *"+line[dot_idx+2:]
updated_list.write(line)
i+=1updated_list.close()
defunprioritize(idx):
""" Function that un-prioritizes the element at idx. """# store list of lines to use later (since we will overwrite these lines) temp_file_obj=open(LIST_FILE_PATH, "r")
line_list=temp_file_obj.readlines()
temp_file_obj.close()
updated_list=open(LIST_FILE_PATH, "w")
i=1forlineinline_list:
ifi==idx:
line=line.replace("*", "")
updated_list.write(line)
i+=1updated_list.close()
defcomplete(idx):
""" Function that completes (deletes) the line (item) at the specified index from the list. """# store list of lines to use later (since we will overwrite these lines) temp_file_obj=open(LIST_FILE_PATH, "r")
line_list=temp_file_obj.readlines()
temp_file_obj.close()
updated_list=open(LIST_FILE_PATH, "w")
curr_idx=1line_count=0# num lines appended to new list forlineinline_list:
ifcurr_idx<idx:
updated_list.write(line)
line_count+=1ifcurr_idx>idx:
line_count+=1dot_idx=line.find(".")
line=str(line_count) +line[dot_idx:]
updated_list.write(line)
curr_idx+=1deferase():
""" Function that erases the contents of the list. """file=open(LIST_FILE_PATH, "w")
file.close()
defmain():
user_arguments=sys.argv[1:]
arg_count=len(user_arguments) # num user arguments # print out list if no argument was specified ifarg_count==0:
show() # show the list sys.exit()
# add item to list if 'add' argument is specified if (user_arguments[0] =="add"oruser_arguments[0] =="a") andarg_count>1:
add(' '.join(user_arguments[1:]))
# search the list with given argument as query if 'search' argument is specifiedif (user_arguments[0] =="search"oruser_arguments[0] =="s") andarg_count>1:
search(' '.join(user_arguments[1:]))
# print out list if 'show' argument is specifiedifuser_arguments[0] =="show":
show()
# print out prioritized items from list if (user_arguments[0] =="priority"oruser_arguments[0] =="p") andarg_count==1:
show_priority()
# prioritize element if (user_arguments[0] =="prioritize"oruser_arguments[0] =="p") andarg_count==2:
specified_idx=int(user_arguments[1])
ifspecified_idx>length(open(LIST_FILE_PATH)) orspecified_idx<=0:
print("Item index out of bounds")
sys.exit()
make_prioritized(specified_idx)
# un-prioritize element if (user_arguments[0] =="unprioritize"oruser_arguments[0] =="u") andarg_count==2:
specified_idx=int(user_arguments[1])
ifspecified_idx>length(open(LIST_FILE_PATH)) orspecified_idx<=0:
print("Item index out of bounds")
sys.exit()
unprioritize(specified_idx)
# print out num items in list if 'len' argument is specifiedifuser_arguments[0] =="len"oruser_arguments[0] =="l":
print("Number of items:", length(open(LIST_FILE_PATH)))
# complete (delete) specific item from list if (user_arguments[0] =="complete"oruser_arguments[0] =="c") andarg_count==2:
specified_idx=int(user_arguments[1])
ifspecified_idx>length(open(LIST_FILE_PATH)) orspecified_idx<=0:
print("Item index out of bounds")
sys.exit()
complete(specified_idx)
# erase items from list ifuser_arguments[0] =="erase"oruser_arguments[0] =="e":
erase()
# branch runner code # print out branches if 'branch' argument is specifiedif (user_arguments[0] =="branch"oruser_arguments[0] =="b") and (len(user_arguments) <2):
show_branches()
elif (user_arguments[0] =="branch new") or (user_arguments[0] =="bn"):
# create new branch assertlen(user_arguments) >1, "must provide branch name"new_branch(user_arguments[1])
elif (user_arguments[0] =="branch switch") or (user_arguments[0] =="bs"):
# switch branchesassertlen(user_arguments) >1, "must provide branch name"switch_branches(user_arguments[1])
elif (user_arguments[0] =="branch current") or (user_arguments[0] =="bc"):
# get current branch current_branch()
elif (user_arguments[0] =="branch delete") or (user_arguments[0] =="bd"):
# switch branchesassertlen(user_arguments) >1, "must provide branch name"delete_branch(user_arguments[1])
elif (len(user_arguments) >1):
if (user_arguments[0] =="branch") and (user_arguments[1] =="current"):
current_branch()
elif (user_arguments[0] =="branch") and (user_arguments[1] =="new"):
assertlen(user_arguments) >2, "must provide branch name"new_branch(user_arguments[2])
elif (user_arguments[0] =="branch") and (user_arguments[1] =="switch"):
assertlen(user_arguments) >2, "must provide branch name"switch_branches(user_arguments[2])
elif (user_arguments[0] =="branch") and (user_arguments[1] =="delete"):
assertlen(user_arguments) >2, "must provide branch name"delete_branch(user_arguments[2])
if__name__=='__main__':
main()
The text was updated successfully, but these errors were encountered:
Below is a draft for implementing branching (control multiple lists with one script). It works similar to git branching.
The text was updated successfully, but these errors were encountered: