Skip to content

Instantly share code, notes, and snippets.

@daedric
Last active July 22, 2020 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daedric/103423be0b8a87f4384de86a41335d6c to your computer and use it in GitHub Desktop.
Save daedric/103423be0b8a87f4384de86a41335d6c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import pathlib
import os
import notmuch
trash_dirs = {
'[Gmail].Corbeille',
'[Gmail].Trash',
'Trash',
}
db = notmuch.Database()
accounts = [d for d in pathlib.Path(db.get_path()).iterdir() if d.name[0] != '.' and d.is_dir()]
trashes = {}
for account in accounts:
for t in trash_dirs:
trash = account / t
if trash.exists():
trashes[account] = trash
break
else:
raise Exception("Did not find a trash dir for: {}".format(account))
db.begin_atomic()
try:
query = db.create_query('tag:deleted')
for m in query.search_messages():
for f in m.get_filenames():
f = pathlib.Path(f)
if not f.exists():
continue
for prefix, trash in trashes.items():
if prefix in f.parents and trash not in f.parents:
# print("Trash for {} is: {}".format(m, trash))
os.link(f, trash / 'new' / f.name)
os.unlink(f)
finally:
db.end_atomic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment