fix up generate sidebar to allow for pinning mailboxes to top or bottom

This commit is contained in:
JesseBot 2023-01-01 16:16:04 +00:00
parent 22f02f22b0
commit 6151978c40

View file

@ -1,29 +1,7 @@
#!/usr/bin/env python3.11
"""
named-mailboxes "" "+INBOX"
named-mailboxes "" "+Drafts"
named-mailboxes "" "+Sent"
named-mailboxes "" "+Labels.leveringsupdates"
named-mailboxes "" "+Labels.retourns"
named-mailboxes "" "+Labels.reviews"
named-mailboxes "" "+Labels.saxenburgerdwarsstraat"
named-mailboxes "" "+Labels.stempelkaart"
named-mailboxes "" "+Labels.viewing"
named-mailboxes "" "+All Services"
named-mailboxes "" "+aankopen"
named-mailboxes "" "+aankopen.eten"
named-mailboxes "" "+aankopen.eten.boodschappen"
named-mailboxes "" "+aankopen.eten.thuisBezorgdOfAfhalen"
named-mailboxes "" "+aankopen.general"
named-mailboxes "" "+aankopen.general.winkel specifieke bonnen"
named-mailboxes "" "+behuizing"
named-mailboxes "" "+behuizing.afwijzing"
named-mailboxes "" "+behuizing.huisBaas"
named-mailboxes "" "+importeren"
named-mailboxes "" "+promoties"
named-mailboxes "" "+Archive"
named-mailboxes "" "+Spam"
named-mailboxes "" "+Trash"
NAME: generate_neomutt_sidebar.py
DESC: generate a prettier neomutt sidebar
"""
from os import path
from xdg import xdg_config_home
@ -57,31 +35,64 @@ def fix_naming(foldername=""):
return foldername.replace("+", "").title()
def fix_mailboxes_file(mailbox_path=MAILBOXES_PATH):
def fix_mailboxes_file(mailbox_path=MAILBOXES_PATH,
pin_to_top_list=["inbox", "drafts", "sent"],
pin_to_bottom_list=["archive", "trash"]):
"""
Takes list of neomutt named-mailboxes generated in a file with this format:
named-mailboxes "" "+INBOX"
Updates the file to have prettier descriptions like:
named-mailboxes " Inbox" "+INBOX"
Also adds some tree nesting to nested directories.
Takes optional variables for pin_to_top_list and pin_to_bottom_list,
to make sure certian mailboxes are put on the top vs the bottom of the
sidebar list
"""
updated_lines_list = []
end_list = []
print(pin_to_top_list)
# open the mailboxes file to fix the naming
with open(mailbox_path, 'r') as mailbox_file:
for line in mailbox_file.readlines():
# we first split on spaces
sections = line.split()
# this should be the name of the folder on local disk
folder = sections[2]
# sometimes there's a space in the folder name; we catch it here
if not folder.endswith('"'):
if sections[2].endswith('"'):
folder = sections[2]
else:
folder = " ".join(sections[2:])
# this fixes the folder description
folder_description = fix_naming(folder)
new_line = " ".join([sections[0], folder_description, folder])
updated_lines_list.append(new_line + "\n")
# this is the new updated line for the for file
new_line = f"{sections[0]} {folder_description} {folder}\n"
stripped_folder = folder.replace("+", "").replace('"', '').lower()
print(stripped_folder)
# make sure this isn't a special folder to be pinned, such as inbox
if stripped_folder in pin_to_top_list:
new_index = pin_to_top_list.index(stripped_folder)
updated_lines_list.insert(new_index, new_line)
print("Begin: ", stripped_folder)
# pin to bottom list such as Trash
elif stripped_folder in pin_to_bottom_list:
new_index = pin_to_bottom_list.index(stripped_folder)
end_list.insert(new_index, new_line)
print("End: ", stripped_folder)
else:
updated_lines_list.append(new_line)
with open(mailbox_path, 'w') as mailbox_writing_file:
for updated_line in updated_lines_list:
mailbox_writing_file.write(updated_line)
for updated_end_line in end_list:
mailbox_writing_file.write(updated_end_line)
return True