-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-mailbox-names.py
More file actions
executable file
·146 lines (120 loc) · 3.32 KB
/
check-mailbox-names.py
File metadata and controls
executable file
·146 lines (120 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# ]
# ///
import sys, locale, argparse, pathlib, collections, json, subprocess
CONFIG_DIR = pathlib.Path.home() / ".config" / pathlib.Path(__file__).stem
def main(
*,
# Example Options
show_all_mailboxes: bool
) -> None:
locale.setlocale(locale.LC_ALL, "")
if show_all_mailboxes:
for label, entries in list_accounts():
print(label)
for entry in sorted(entries, key=mailbox_name_sort_order_key):
print("\t", "/".join(entry["path"]), sep="")
print()
else:
folders = collections.defaultdict(list)
ignored_paths = set(tuple(path) for path in read_config("ignore"))
for label, entries in list_accounts():
for entry in entries:
path = entry["path"]
if tuple(path) in ignored_paths:
continue
path.insert(0, label)
for p in path:
assert "/" not in p
folders[path[-1]].append("/".join(path))
folders = {name: paths for name, paths in folders.items() if len(paths) != 1}
print("\n\n".join(
"\n".join(paths) for paths in folders.values()
))
the_mailbox_name_order_first = ["INBOX", "Drafts", "Sent", "Junk", "Spam", "Trash"]
the_mailbox_name_order_last = ["Archive"]
emojis = ["⏱", "📥", "🗃️"]
def mailbox_name_sort_order_key(entry):
path = entry["path"]
if path[0] == "[Gmail]":
path = path[1:]
for i in emojis:
if path and path[0].startswith(i):
order = (2, path)
break
else:
order = (3, path)
try:
order = (1, the_mailbox_name_order_first.index(path[0]), *path)
except:
try:
order = (4, the_mailbox_name_order_last.index(path[0]), *path)
except:
pass
return order
def list_accounts():
for label, url in read_config("accounts"):
p = subprocess.run(
["imap.py", "-a", url, "--json"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False
)
assert p.returncode == 0, p
assert not p.stderr
try:
out = p.stdout.decode()
except:
print(p.stdout)
raise
try:
out = json.loads(out)
except:
print(out)
raise
yield (label, out)
def read_config(name):
with (CONFIG_DIR / (name + ".json")).open("r") as fo:
return json.load(fo)
def parse_args(*, args, prog):
parser = argparse.ArgumentParser(
prog=prog,
usage="%(prog)s [OPTIONS]...",
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
fromfile_prefix_chars="@",
add_help=False,
)
the_default = "\ndefault: %(default)s"
options_generic = parser.add_argument_group("Generic Options")
options_generic.add_argument(
"--help", "-h",
action="help",
help="show help message and exit",
)
options_example = parser.add_argument_group("Example Options")
options_example.add_argument(
"--show-all-mailboxes",
action="store_true", dest="show_all_mailboxes", default=False, required=False,
help="instead of finding mailboxes with same name, simply list all mailboxes in all accounts" + the_default
)
opts = parser.parse_args(args)
return vars(opts)
def configure_logging(opts):
pass
def smain(argv=None):
if argv is None:
argv = sys.argv
try:
opts = parse_args(args=argv[1:], prog=argv[0])
configure_logging(opts)
return main(**opts)
except KeyboardInterrupt:
print(file=sys.stderr)
if __name__ == "__main__":
sys.exit(smain())