|
#!/usr/bin/env python3
|
|
|
|
import fileinput, sys, re
|
|
|
|
try:
|
|
import vobject
|
|
except:
|
|
print('apt install python3-vobject')
|
|
exit(1)
|
|
|
|
|
|
def is_ascii(s):
|
|
try:
|
|
s.encode('ascii')
|
|
return True
|
|
except UnicodeEncodeError:
|
|
return False
|
|
|
|
match_line = re.compile(r'^- (?P<prenom>[^\s]+)\s+(?P<nom>[^,]*)\s*(,\s+(?P<note>.*)\s+)?<(?P<email>[^>]+)>\s*$')
|
|
|
|
vcards = {}
|
|
|
|
for line in fileinput.input():
|
|
matched = match_line.match(line)
|
|
if not matched:
|
|
print("Donnée mal formée: ", line, file=sys.stderr)
|
|
else:
|
|
card = matched.groupdict()
|
|
already_seen = vcards.get((card["nom"], card["prenom"]), False)
|
|
if already_seen:
|
|
if hasattr(already_seen, 'note') and card['note']:
|
|
v.add('note')
|
|
v.note.value = card['note']
|
|
print("Doublon probable (note mise à jour): ", card["prenom"], card["nom"], file=sys.stderr)
|
|
else:
|
|
print("Doublon probable (ignoré): ", card["prenom"], card["nom"], file=sys.stderr)
|
|
continue
|
|
|
|
v = vobject.vCard()
|
|
|
|
v.add ('fn')
|
|
v.fn.value = "{} {}".format(card["prenom"], card["nom"])
|
|
if not is_ascii(v.fn.value):
|
|
v.fn.charset_param = 'UTF-8'
|
|
|
|
v.add ('n')
|
|
v.n.value = vobject.vcard.Name(
|
|
family = card["nom"],
|
|
given = card["prenom"],
|
|
)
|
|
if not is_ascii(card["nom"] + card["prenom"]):
|
|
v.n.charset_param = 'UTF-8'
|
|
|
|
v.add('email')
|
|
v.email.value = card["email"]
|
|
v.email.type_param = 'HOME'
|
|
if not is_ascii(v.email.value):
|
|
v.email.charset_param = 'UTF-8'
|
|
|
|
v.add('categories')
|
|
v.categories.value = ["Personne invitée à Libre à vous !"]
|
|
v.categories.charset_param = 'UTF-8'
|
|
|
|
if card['note']:
|
|
v.add('note')
|
|
v.note.value = card['note']
|
|
if not is_ascii(v.note.value):
|
|
v.note.charset_param = 'UTF-8'
|
|
|
|
vcards[(card["nom"], card["prenom"])] = v
|
|
|
|
for card in vcards.values():
|
|
print(card.serialize())
|