Created
September 18, 2017 12:31
-
-
Save xcombelle/5a898abec71d5d199e79af1a6260dc8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import re | |
import sys | |
def e(arg): | |
return re.escape(arg) | |
def any(arg): | |
return "("+"|".join(map(re.escape,arg.split()))+")" | |
def non_space(): | |
return r"\S+" | |
def spaced(*args): | |
return "\s+".join(args) | |
def digits(n,n2=None): | |
if n2 is None: | |
return r"\d{"+str(n)+"}" | |
else: | |
return r"\d{"+str(n)+","+str(n2)+"}" | |
def lines(*arg): | |
return ("^"+r"\n".join(arg)+"$") | |
def free(): | |
return(".*?") | |
def o(arg): | |
return("("+arg+")?") | |
def join(*arg): | |
return "".join(arg) | |
def nl(): | |
return r"\n" | |
def mailman_time(): | |
return ":".join((digits(2) for i in range(3))) | |
def mailman_date(): | |
return spaced(any("Mon Tue Wed Thu Fri Sat Sun"), | |
any("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"), | |
digits(0,2), | |
mailman_time(), | |
digits(4)) | |
def mailman_mail(): | |
return spaced(non_space(), | |
e("at"), | |
non_space()) | |
def mail_header(name): | |
return spaced(e(name)+":", | |
free()+"$" | |
+r"(\n\t\S"+free()+"$)*" | |
) | |
def mail_header_nl(name): | |
return join(mail_header(name),nl()) | |
def mailman_header(): | |
return join(lines(spaced(e("From"), | |
mailman_mail(), | |
mailman_date()), | |
spaced(e("From:"), | |
mailman_mail(), | |
r".*?"), | |
mail_header("Date"), | |
mail_header("Subject")), | |
nl(), | |
o(mail_header_nl("In-Reply-To")), | |
o(mail_header_nl("References")), | |
o(mail_header_nl("Message-ID")), | |
"^$" | |
) | |
if __name__ == "__main__": | |
header = re.compile(mailman_header(),re.MULTILINE | |
) | |
print(mailman_header()) | |
for line in header.finditer(sys.stdin.read()): | |
print(line.group()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment