ldif-to-xml.py 3.0 KB

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