Skip to content

Instantly share code, notes, and snippets.

@cmaureir
Created February 28, 2018 15:45
Show Gist options
  • Save cmaureir/92b3da22825e6491f44b8d8cc1eaaaab to your computer and use it in GitHub Desktop.
Save cmaureir/92b3da22825e6491f44b8d8cc1eaaaab to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import re
import sys
import logging
def get_emails(fname):
print("Reading {}".format(fname))
data = []
original = []
with open(fname) as f:
lines = f.readlines()
l = len(lines)
for i, line in enumerate(lines):
#line = line.encode('utf-8').strip()
#line = line.decode('utf-8')
if re.match('^From\ ', line) and re.match('.*pau\@ice\.cat.*', line):
data.append(line)
j = 1
line = lines[i+j]
while not re.match('^From\ +', line):
j += 1
data.append(line)
if i + j < l:
line = lines[i+j]
else:
break
else:
original.append(line)
return data, original
def append_emails(fname, data=[]):
if data and len(data) > 0:
with open(fname.replace('rebuts','enviats'), 'a') as f:
for line in data:
f.write(line)
else:
logging.warning("No messages from {}".format(fname))
def write_original(fname, data=[]):
if data and len(data) > 0:
with open(fname, 'w') as f:
for line in data:
f.write(line)
else:
logging.warning("No messages from {}".format(fname))
if __name__ == "__main__":
regex = "rebuts_[a-zA-Z][a-zA-Z][a-zA-Z]_[0-9][0-9][0-9][0-9]"
rebuts = [i for i in os.listdir(".") if re.match(regex,i) and "bak" not in i]
for i in rebuts:
# Copy to be safe
os.system("/bin/cp -f {} {}".format(i,i+".bak"))
data, orig = get_emails(i)
append_emails(i, data)
write_original(i, orig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment