Projet

Général

Profil

Demande #2795 » normalize.py

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

 
1
import re
2
import sys
3

    
4
lines = []
5
def process(line):
6
    if not line:
7
        return
8
    if (line.startswith('PRODID:') or
9
        line.startswith('UID:') or
10
        line.startswith('REV:') or
11
        line.startswith('X-EVOLUTION') or
12
        line.startswith('X-MOZILLA-HTML:') or
13
        line.startswith('FBURL:') or
14
        line.startswith('BDAY:00011130') or
15
        line.startswith('CALURI:') or
16
        line.startswith('LABEL;') or
17
        line.startswith('X-KADDRESSBOOK')):
18
        return
19
    if line.startswith('URL;') or line.startswith('ADR;'):
20
        line = line.replace(';TYPE=work', ';TYPE=WORK')
21
    if line.startswith('ADR'):
22
        line = line.replace(r'\n', ' ').replace(r'\,', '')
23
    line = line.replace('\r', ' ')
24
    line = re.sub(' +', ' ', line)
25
    lines.append(line)
26

    
27
email_count = 0
28
content = ''
29
for line in open(sys.argv[1]).readlines():
30
    if line.startswith('EMAIL'):
31
        email_count += 1
32
    if line.startswith(' '):
33
        content = content[:-1] + line[1:]
34
    else:
35
        process(content)
36
        content = line
37
process(content)
38

    
39
def process_mails(lines, email_count):
40
    if email_count == 1:
41
        new_lines = []
42
        for line in lines:
43
            if line.startswith('EMAIL'):
44
                line = re.sub('^EMAIL.*?:', 'EMAIL:', line)
45
            new_lines.append(line)
46
        return new_lines
47
    else:
48
        return lines
49

    
50
lines = process_mails(lines, email_count)
51

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