~emersion/python-emailthreads

0a56af7fd6de16105c27b7c149eeb0282e95e587 — Drew DeVault 1 year, 11 months ago 835ec98
Establish proper Python module, add setup.py
7 files changed, 28 insertions(+), 49 deletions(-)

R __init__.py => emailthreads/__init__.py
R quotes.py => emailthreads/quotes.py
R threads.py => emailthreads/threads.py
R util.py => emailthreads/util.py
A setup.py
D test.sh
D try.py
R __init__.py => emailthreads/__init__.py +0 -0

R quotes.py => emailthreads/quotes.py +0 -0

R threads.py => emailthreads/threads.py +0 -0

R util.py => emailthreads/util.py +0 -0

A setup.py => setup.py +28 -0
@@ 0,0 1,28 @@
#!/usr/bin/env python3

from distutils.core import setup
import subprocess
import os

ver = os.environ.get("PKGVER")

if not ver:
	subp = subprocess.run(['git', 'describe', '--tags'],
		stdout=subprocess.PIPE)
	if subp.returncode != 0:
		ver = "0.0.0"
	else:
		ver = subp.stdout.decode().strip()

setup(
	name = 'emailthreads',
	packages = [
		'emailthreads',
	],
	version = ver,
	description = 'Parses email threads into conversation trees',
	author = 'Simon Ser',
	author_email = 'contact@emersion.fr',
	url = 'https://github.com/emersion/python-emailthreads',
	license = 'MIT'
)

D test.sh => test.sh +0 -3
@@ 1,3 0,0 @@
#!/bin/sh

python3 -m pytest test

D try.py => try.py +0 -46
@@ 1,46 0,0 @@
#!/usr/bin/env python3

import mailbox

from util import *
from threads import *

def get_message_references(msg):
	# TODO: handle spaces in message IDs
	refs_str = flatten_header_field(msg.get("references", ""))
	ref_ids = []
	for ref_id in refs_str.split(" "):
		ref_id = ref_id.strip()
		if ref_id == "":
			continue
		ref_ids.append(ref_id)
	return ref_ids

def print_thread(mbox, msg_id):
	messages_by_id = {}
	for msg in mbox:
		messages_by_id[msg["message-id"]] = msg

	msg = messages_by_id[msg_id]
	ref_ids = get_message_references(msg)
	refs = []
	for ref_id in ref_ids:
		ref = messages_by_id[ref_id]
		if ref is not None:
			refs.append(ref)
		else:
			print("Warning: missing reference " + ref_id)
	thread = parse(msg, refs)
	print(thread)

# TODO: don't hardcode those
# mbox_path = "/home/simon/tmp/wayland-devel/all.mbox"
mbox_path = "/home/simon/tmp/wayland-devel/2018.mbox"
# mbox_path = "/home/simon/tmp/wayland-devel/2018-May.txt"
# msg_id = "<ifhnVLylmcR__mKkQ0yb6e9VU8t-c1zYPYZFbhAUpnxZl8QJio1k4hp4Yv3LqPkhjV1389yNgIDdNwxDGyj5iW1ahatsMoCcEiR75UwnmKY=@emersion.fr>" # Direct reply
# msg_id = "<20180529171002.7a2d3706@eldfell>" # Direct reply
# msg_id = "<1527683902.2337.10.camel@nxp.com>" # Reply of reply
msg_id = "<20180531153835.6ad9e559@eldfell>" # Reply of reply of reply

mbox = mailbox.mbox(mbox_path)
print_thread(mbox, msg_id)