Last active
July 22, 2020 08:39
-
-
Save daedric/103423be0b8a87f4384de86a41335d6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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