Projet

Général

Profil

Demande #2795 » normalize.py

Loïc Dachary, 18/10/2019 15:47

 
import re
import sys

lines = []
def process(line):
if not line:
return
if (line.startswith('PRODID:') or
line.startswith('UID:') or
line.startswith('REV:') or
line.startswith('X-EVOLUTION') or
line.startswith('X-MOZILLA-HTML:') or
line.startswith('FBURL:') or
line.startswith('BDAY:00011130') or
line.startswith('CALURI:') or
line.startswith('LABEL;') or
line.startswith('X-KADDRESSBOOK')):
return
if line.startswith('URL;') or line.startswith('ADR;'):
line = line.replace(';TYPE=work', ';TYPE=WORK')
if line.startswith('ADR'):
line = line.replace(r'\n', ' ').replace(r'\,', '')
line = line.replace('\r', ' ')
line = re.sub(' +', ' ', line)
lines.append(line)

email_count = 0
content = ''
for line in open(sys.argv[1]).readlines():
if line.startswith('EMAIL'):
email_count += 1
if line.startswith(' '):
content = content[:-1] + line[1:]
else:
process(content)
content = line
process(content)

def process_mails(lines, email_count):
if email_count == 1:
new_lines = []
for line in lines:
if line.startswith('EMAIL'):
line = re.sub('^EMAIL.*?:', 'EMAIL:', line)
new_lines.append(line)
return new_lines
else:
return lines

lines = process_mails(lines, email_count)

open(sys.argv[1], 'w').write(''.join(lines))
(2-2/2)