aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Runge <dave@sleepmap.de>2022-12-12 15:13:20 +0100
committerDavid Runge <dave@sleepmap.de>2022-12-12 15:13:20 +0100
commit0fb4731d7303061b051fe3f2fc94cb1975da1cbc (patch)
treee743870110f52ccc143c88d1a727fd61376a1aed
parent378cc7d1ad56bccfa7430fcf923c3ac387570277 (diff)
downloaddotfiles-0fb4731d7303061b051fe3f2fc94cb1975da1cbc.tar.gz
dotfiles-0fb4731d7303061b051fe3f2fc94cb1975da1cbc.tar.bz2
dotfiles-0fb4731d7303061b051fe3f2fc94cb1975da1cbc.tar.xz
dotfiles-0fb4731d7303061b051fe3f2fc94cb1975da1cbc.zip
Simplify mutt_mailboxes
bin/mutt_mailboxes: Simplify the command by allowing to optionally provide a directory in which mail account directories are found. Add error handling and do not generate output for account directories without mailboxes. Add documentation.
-rwxr-xr-xbin/mutt_mailboxes104
1 files changed, 85 insertions, 19 deletions
diff --git a/bin/mutt_mailboxes b/bin/mutt_mailboxes
index 71547c9..0924b89 100755
--- a/bin/mutt_mailboxes
+++ b/bin/mutt_mailboxes
@@ -2,44 +2,109 @@
from argparse import ArgumentParser
from pathlib import Path
-from sys import exit
MAILBOX_SUBDIRS = {"cur", "new", "tmp"}
def get_mailbox_from_dir(path: Path, mailboxes: list[Path]) -> None:
+ """ Add mailboxes in a directory to a list.
+
+ All directories below path are evaluated to have necessary subdirectories
+ (see MAILBOX_SUBDIRS) to be counted as a mailbox directory.
+
+ Parameters
+ ----------
+ path: Path
+ The path to search in for mailbox directories
+ mailboxes: list[Path]
+ A list of paths to add mailboxes to
+ """
for current_path in path.iterdir():
if current_path.is_dir() and current_path.name not in MAILBOX_SUBDIRS:
subdirs = list(current_path.iterdir())
- if MAILBOX_SUBDIRS & set(subdir.name for subdir in subdirs) and current_path not in mailboxes:
+ if (
+ MAILBOX_SUBDIRS & set(subdir.name for subdir in subdirs)
+ and current_path not in mailboxes
+ ):
mailboxes.append(current_path)
for subdir in subdirs:
if subdir.name not in MAILBOX_SUBDIRS:
- get_mailbox_from_dir(path=current_path, mailboxes=mailboxes)
+ get_mailbox_from_dir(
+ path=current_path,
+ mailboxes=mailboxes,
+ )
+
+
+def write_file(contents: str, output: Path) -> None:
+ """Write a string to a file
+
+ Parameters
+ ----------
+ contents: str
+ The string to write
+ output: Path
+ The file path to write to
+ """
+ with open(output, "w") as f:
+ print(contents, file=f)
-def main(output: Path, paths: list[Path]) -> None:
- file_string = ""
+def main(path: Path, output: Path) -> None:
+ """Find mailbox directories in account directories and write them to file.
+
+ The directories below path are considered to be account directories,
+ containing mailboxes.
+ The output is written to output and is compatible with mutt's mailboxes
+ directive.
+
+
+ Parameters
+ ----------
+ path: Path
+ A directory below which directories are assumed to be account
+ directories which contain mailboxes
+ output: Path
+ A file to which to write output to
+
+ Raises
+ ------
+ RuntimeError
+ If path does not exist
+ or if path is not a directory
+ or if path does not contain any directories
+ """
+ if not path.exists():
+ raise RuntimeError(f"The input path '{path}' does not exist!")
- if len(paths) == 0:
- exit(1)
+ if not path.is_dir():
+ raise RuntimeError(f"The input path '{path}' is not a directory!")
- for path in paths:
+ accounts = [subdir for subdir in path.iterdir() if subdir.is_dir()]
+ if len(accounts) == 0:
+ raise RuntimeError(
+ f"The path '{path}' does not contain any account directories!"
+ )
+
+ file_string = ""
+
+ for account_dir in accounts:
mailboxes: list[Path] = []
mailboxes_string = "mailboxes "
- if not path.exists():
- exit(1)
-
- get_mailbox_from_dir(path=path, mailboxes=mailboxes)
+ get_mailbox_from_dir(path=account_dir, mailboxes=mailboxes)
for mailbox in mailboxes:
- mailboxes_string += f" '+{(str(mailbox).replace(str(path.parent) + '/', ''))}'"
- file_string += mailboxes_string + "\n"
+ mailbox_dir = str(mailbox).replace(
+ str(account_dir.parent) + '/',
+ '',
+ )
+ mailboxes_string += f" '+{mailbox_dir}'"
- with open(output, "w") as f:
- print(file_string, file=f)
+ if mailboxes:
+ file_string += mailboxes_string + "\n"
+
+ write_file(contents=file_string, output=output)
if __name__ == "__main__":
@@ -57,12 +122,13 @@ if __name__ == "__main__":
type=Path,
)
parser.add_argument(
- "paths",
+ "path",
+ default=Path("~/.local/state/mail/"),
help="a directory to search in for mailbox directories",
- nargs="+",
+ nargs="?",
type=Path,
)
namespace = parser.parse_args()
- main(output=namespace.output, paths=namespace.paths)
+ main(output=namespace.output, path=namespace.path)