Show mel
This commit is contained in:
parent
f910bd6add
commit
08d0776759
111
scripts/mel
111
scripts/mel
|
@ -49,6 +49,7 @@ import sys
|
|||
import subprocess
|
||||
import html
|
||||
import re
|
||||
import email.parser
|
||||
|
||||
perfstep("import")
|
||||
|
||||
|
@ -250,8 +251,8 @@ def extract_email(field):
|
|||
except ValueError:
|
||||
return field
|
||||
|
||||
msg = None
|
||||
def applyMsgs(queryStr, action, *args, showProgress=False, write=False, closeDb=True, **kwargs):
|
||||
# TODO Verify it's open in the correct mode
|
||||
if db is None:
|
||||
open_database(write=write)
|
||||
|
||||
|
@ -265,7 +266,6 @@ def applyMsgs(queryStr, action, *args, showProgress=False, write=False, closeDb=
|
|||
iterator = progressbar.progressbar(elements, max_value=nbMsgs) if showProgress else elements
|
||||
|
||||
log.info("Executing {}".format(action))
|
||||
global msg
|
||||
for msg in iterator:
|
||||
if write:
|
||||
msg.freeze()
|
||||
|
@ -281,16 +281,6 @@ def applyMsgs(queryStr, action, *args, showProgress=False, write=False, closeDb=
|
|||
|
||||
return nbMsgs
|
||||
|
||||
# applyMsgs('*', print_msg)
|
||||
# applyMsgs('tag:inbox', print_msg)
|
||||
# applyMsgs('tag:spam', print_msg)
|
||||
# applyMsgs('tag:unread', print_msg)
|
||||
# applyMsgs('tag:unprocessed', print_msg)
|
||||
# applyMsgs('from:geoffrey@frogeye.fr', print_msg)
|
||||
|
||||
# applyMsgs('tag:unprocessed', retag_msg, useProgressbar=True)
|
||||
# applyMsgs('*', retag_msg, useProgressbar=True)
|
||||
|
||||
# def update_polybar_status():
|
||||
def update_polybar_status(*args, **kwargs):
|
||||
log.info("Updating polybar status")
|
||||
|
@ -371,9 +361,78 @@ def apply_msgs_input(argmessages, action, write=False):
|
|||
|
||||
for message in messages:
|
||||
queryStr = 'tag:tuid{}'.format(message)
|
||||
nbMsgs = applyMsgs(queryStr, action, write=write)
|
||||
nbMsgs = applyMsgs(queryStr, action, write=write, closeDb=False)
|
||||
if nbMsgs < 1:
|
||||
log.error("Couldn't execute function for message {}".format(message))
|
||||
close_database()
|
||||
|
||||
def format_header_value(val):
|
||||
return val.replace('\n', '').replace('\t', '').strip()
|
||||
|
||||
# From https://stackoverflow.com/a/1094933
|
||||
def sizeof_fmt(num, suffix='B'):
|
||||
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
|
||||
if abs(num) < 1024.0:
|
||||
return "%3.1f %s%s" % (num, unit, suffix)
|
||||
num /= 1024.0
|
||||
return "%.1f %s%s" % (num, 'Yi', suffix)
|
||||
|
||||
def show_parts_tree(part, lvl=0, nb=1):
|
||||
indent = lvl * '\t'
|
||||
typ = part.get_content_type()
|
||||
if part.is_multipart():
|
||||
print(PART_MULTI_FORMAT.format(nb=nb, indent=indent, typ=typ))
|
||||
payl = part.get_payload()
|
||||
size = 1
|
||||
for obj in payl:
|
||||
size += show_parts_tree(obj, lvl=lvl+1, nb=nb+size)
|
||||
return size
|
||||
else:
|
||||
size = len(part.get_payload(decode=True))
|
||||
desc = part.get('Content-Description', '<no description>')
|
||||
print(PART_LEAF_FORMAT.format(nb=nb, indent=indent, typ=typ, desc=desc, size=sizeof_fmt(size)))
|
||||
return 1
|
||||
|
||||
INTERESTING_HEADERS = ["Date", "From", "Subject", "To", "Cc", "Message-Id"]
|
||||
HEADER_FORMAT = colorama.Fore.BLUE + colorama.Style.BRIGHT + '{}:' + colorama.Style.NORMAL + ' {}' + colorama.Style.RESET_ALL
|
||||
PART_MULTI_FORMAT = colorama.Fore.BLUE + '{nb} {indent}+ {typ}' + colorama.Style.RESET_ALL
|
||||
PART_LEAF_FORMAT = colorama.Fore.BLUE + '{nb} {indent}→ {desc} ({typ}; {size})' + colorama.Style.RESET_ALL
|
||||
def read_msg(msg):
|
||||
# Parse
|
||||
filename = msg.get_filename()
|
||||
parser = email.parser.BytesParser()
|
||||
with open(filename, 'rb') as f:
|
||||
mail = parser.parse(f)
|
||||
|
||||
# Debug
|
||||
global a
|
||||
a = mail
|
||||
|
||||
# Defects
|
||||
if len(mail.defects):
|
||||
log.warn("Defects found in the mail:")
|
||||
for defect in mail.defects:
|
||||
log.warn(mail.defects)
|
||||
|
||||
|
||||
# Headers
|
||||
for key in INTERESTING_HEADERS:
|
||||
val = mail.get(key)
|
||||
if val:
|
||||
val = format_header_value(val)
|
||||
print(HEADER_FORMAT.format(key, val))
|
||||
# TODO Show all headers
|
||||
# TODO BONUS Highlight failed verifications
|
||||
|
||||
show_parts_tree(mail)
|
||||
print()
|
||||
|
||||
# Show text/plain
|
||||
for part in mail.walk():
|
||||
if part.get_content_type() == "text/plain":
|
||||
payl = part.get_payload(decode=True)
|
||||
print(payl.decode())
|
||||
|
||||
|
||||
perfstep("definitions")
|
||||
|
||||
|
@ -413,7 +472,7 @@ if __name__ == "__main__":
|
|||
def func_flag(args):
|
||||
def flag_msg(msg):
|
||||
msg.add_tag('flagged')
|
||||
apply_msgs_input(args.message, action=flag_msg, write=True)
|
||||
apply_msgs_input(args.message, flag_msg, write=True)
|
||||
parserFlag = subparsers.add_parser("flag", help="Mark messages as flagged")
|
||||
parserFlag.add_argument('message', nargs='*', help="Messages")
|
||||
parserFlag.set_defaults(func=func_flag)
|
||||
|
@ -423,7 +482,7 @@ if __name__ == "__main__":
|
|||
def func_unflag(args):
|
||||
def unflag_msg(msg):
|
||||
msg.remove_tag('flagged')
|
||||
apply_msgs_input(args.message, action=unflag_msg, write=True)
|
||||
apply_msgs_input(args.message, unflag_msg, write=True)
|
||||
parserUnflag = subparsers.add_parser("unflag", help="Mark messages as not-flagged")
|
||||
parserUnflag.add_argument('message', nargs='*', help="Messages")
|
||||
parserUnflag.set_defaults(func=func_unflag)
|
||||
|
@ -433,7 +492,16 @@ if __name__ == "__main__":
|
|||
# spam msg...
|
||||
# move dest msg...
|
||||
## Read message
|
||||
|
||||
|
||||
# read msg [--html] [--plain] [--browser]
|
||||
def func_read(args):
|
||||
apply_msgs_input(args.message, read_msg)
|
||||
parserRead = subparsers.add_parser("read", help="Read message")
|
||||
parserRead.add_argument('message', nargs=1, help="Messages")
|
||||
parserRead.set_defaults(func=func_read)
|
||||
|
||||
|
||||
# attach msg [id] [--save] (list if no id, xdg-open else)
|
||||
## Redaction
|
||||
# new account
|
||||
|
@ -476,9 +544,15 @@ if __name__ == "__main__":
|
|||
|
||||
## Debug
|
||||
# debug (various)
|
||||
parserRetag = subparsers.add_parser("debug", help="Who know what this holds...")
|
||||
parserRetag.set_defaults(verbosity='DEBUG')
|
||||
parserRetag.set_defaults(func=notify_all)
|
||||
def func_expose(args):
|
||||
# And leave the door open
|
||||
def expose_msg(a):
|
||||
global msg
|
||||
msg = a
|
||||
applyMsgs('tag:tuidyviU45m6flff', expose_msg, closeDb=False)
|
||||
parserDebug = subparsers.add_parser("debug", help="Who know what this holds...")
|
||||
parserDebug.set_defaults(verbosity='DEBUG')
|
||||
parserDebug.set_defaults(func=func_expose)
|
||||
|
||||
# retag (all or unprocessed)
|
||||
def func_retag(args):
|
||||
|
@ -519,5 +593,6 @@ if __name__ == "__main__":
|
|||
perfstep("exec")
|
||||
|
||||
# DEBUG
|
||||
sys.exit(0)
|
||||
for kv in sorted(perf_dict.items(), key=lambda p: p[1]):
|
||||
log.debug("{1:.6f} {0}".format(*kv))
|
||||
|
|
Loading…
Reference in a new issue