some minor updates
[claws.git] / tools / ldif-to-xml.py
1 #!/usr/bin/env python
2
3 #  * Copyright 2001 Rod Senra <Rodrigo.Senra@ic.unicamp.br>
4 #  *
5 #  * This file is free software; you can redistribute it and/or modify it
6 #  * under the terms of the GNU General Public License as published by
7 #  * the Free Software Foundation; either version 2 of the License, or
8 #  * (at your option) any later version.
9 #  *
10 #  * This program is distributed in the hope that it will be useful, but
11 #  * WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  * General Public License for more details.
14 #  *
15 #  * You should have received a copy of the GNU General Public License
16 #  * along with this program; if not, write to the Free Software
17 #  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #  *
19
20 import re
21 import sys
22
23 header = """<?xml version="1.0" encoding="ISO-8859-1"?>
24 <addressbook>
25
26 <common_address>
27 """
28
29 footer = """
30 </common_address>
31
32 <personal_address>
33 </personal_address>
34
35 </addressbook>
36 """
37
38 def printGroupRec(fd,name,members):
39     """ Print XML group record from r-tuple"""
40     fd.write("    <group name=\"%s\">\n"%(name))
41     for each in members:
42         printRec(fd,each,"        ")
43     fd.write("    </group>\n")
44
45 def printRec(fd,r,ident):
46     """ Print XML group record from r-tuple"""
47     fd.write("%s<item>\n"%(ident) )
48     fd.write("%s    <name>%s</name>\n"%(ident,r[0]))
49     fd.write("%s    <address>%s</address>\n"%(ident,r[1]))
50     fd.write("%s    <remarks>%s</remarks>\n"%(ident,r[2]))
51     fd.write("%s</item>\n"%(ident))
52     
53 outfd = open('addressbook.xml','w')
54
55
56 outfd.write(header)
57 try:
58     rec = {}
59     for line in  open(sys.argv[1]).readlines():
60         line = line[:-1].strip() # clean string
61         if line=='':
62             try:
63                 if rec.has_key('description'):
64                     str = rec['description']
65                 elif rec.has_key('xmozillanickname'):
66                     str = rec['xmozillanickname']
67                 elif rec.has_key('sn'):
68                     str = rec['sn']
69                 else:
70                     str = ''
71                 try:
72                     if rec.has_key('member'):
73                         printGroupRec(outfd,rec['cn'].strip(),rec['member'])
74                     elif rec.has_key('mail'):
75                         printRec(outfd,(rec['cn'].strip(),rec['mail'].strip(),str.strip()),"    ")
76
77                 except KeyError:
78                     pass
79             finally:
80                 del rec
81                 rec = {}
82             continue
83
84         try: # parse line
85             key,value = line.split(':')
86         except:
87             continue
88         if key=='member':
89             name,addr = value.split(',')
90             name = name.split('=')[1].strip()
91             addr = addr.split('=')[1].strip()
92             value = (name,addr,'')
93             if rec.has_key('member'):
94                 rec['member'].append(value)
95             else :
96                 rec['member'] = [value]
97         else:
98             rec[key]=value
99     
100 finally:
101     outfd.write(footer)
102     outfd.close()