-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldif2vcf.py
executable file
·150 lines (116 loc) · 4.96 KB
/
ldif2vcf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Auteur: manuel BERROCAL
# Date: 17-10-2010
"""
Convertisseur de fichier ldif -> vcf
Lit un fichier ldif et le convertit au format vcf (pour roundcube)
Roundcube ne lit que les fichiers vcf pour importer des contacts
Thunderbird n'exporte qu'en ldif ou cvs
Ce programme permet dimporter dans roundcube les contacts de thunderbird.
ATTENTION: il n'y a pas de synchronisation entre les deux!!!
"""
# ldif2vcf.py
#
# Copyright 2010 manuel BERROCAL <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os, os.path, sys
# Permet d'être certain que les fichier situés dans le même répertoire
# seront trouvés en toute circonstance
HERE = os.path.dirname(sys.argv[0])
APPDIR = os.path.abspath(HERE)
sys.path.insert(0, APPDIR)
os.chdir(APPDIR)
def create_vcard(nom, prenom, nomaffiche, mail, telephoneNumber, fileout):
"""
Crée le format vcard avec les éléments transmis et l'enregistre dans le
fichier si le fichier de sortie a été renseigné
"""
if fileout == None:
print "BEGIN:VCARD"
print "VERSION:3.0"
print "FN:%s" % nomaffiche
print "N:%s;%s;;;" % (nom, prenom)
print "EMAIL;type=INTERNET;type=HOME;type=pref:%s" % mail
print "TEL;TYPE=HOME,VOICE:%s" % telephoneNumber
print "END:VCARD\n"
else:
sortie = open(fileout, 'a')
sortie.write("BEGIN:VCARD\n")
sortie.write("VERSION:3.0\n")
sortie.write("FN:%s\n" % nomaffiche)
sortie.write("N:%s;%s;;;\n" % (nom, prenom))
sortie.write("EMAIL;type=INTERNET;type=HOME;type=pref:%s\n" % mail)
sortie.write("TEL;TYPE=HOME,VOICE:%s\n" % telephoneNumber)
sortie.write("END:VCARD\n")
sortie.close()
def main(fichdif, fichout):
"""
Lancement du programme principal
"""
if not os.path.isfile(fichdif):
print "File %s not found. Exiting." % fichdif
sys.exit(1)
if not fichout == None:
sortie = open(fichout, 'w')
sortie.close()
# c'est ici que sont définis les champs recherchés. Ils sont stockés
# dans des variables qui seront transmises à la fonction qui crée le
# fichier vcard
for elem in open(fichdif).readlines():
if not "objectclass:" in elem and not "telephoneNumber:" in elem \
and not "mobile:" in elem:
if "dn: " in elem:
nom = ""
prenom = ""
mail = ""
nomaffiche = ""
telephoneNumber = ""
elif "mail: " in elem:
mail = elem.split(":")[1].strip()
elif "givenName:" in elem:
prenom = elem.split(":")[1].strip()
elif "sn:" in elem:
nom = elem.split(":")[1].strip()
elif "cn:" in elem:
nomaffiche = elem.split(":")[1].strip()
elif "telephoneNumber:" in elem:
telephoneNumber = elem.split(":")[1].strip()
elif "modifytimestamp:" in elem:
# le "modifytimestamp" est la dernière information de la fiche
# de chaque contact. Une fois sur ce champ, on crée la fiche
# vcard avec les variables contenant les infos relevées
create_vcard(nom, prenom, nomaffiche, mail, telephoneNumber, fichout)
if __name__ == '__main__':
from optparse import OptionParser
USAGE_ = "see %prog --help for help \nCe programme permet de convertir un \
fichier au format ldif vers le format vcf pour roundcube"
PARSER = OptionParser(usage = USAGE_ , version = "0.101017-0")
PARSER.add_option("-f", "--file", dest = "file", default = None,
help = "Fichier ldif à traiter",
metavar = "FILE")
PARSER.add_option("-o", "--output", dest = "output", default = None,
help = "Indique la sortie du logiciel. Par défaut \
la sortie standard.")
(OPTIONS, ARGS) = PARSER.parse_args()
if OPTIONS.file == None or OPTIONS.file == "":
print "You MUST specify a ldif file to convert. Exiting."
print "See help for more information."
sys.exit(1)
else:
main(OPTIONS.file, OPTIONS.output)