Compte-rendu WE admin sys

De April MediaWiki
Aller à la navigationAller à la recherche

Migration des contact de dotProjet vers vCard pour import dans OwnCloud

Nous avons utilisé un script python :

import csv
import vobject
import re

def are_null (row, args):
  for arg in args:
    if arg not in row:
      return True
    if row[arg] != "":
      return False
  return True

def add_name (v, row):
  v.add ('n')
  v.n.value = vobject.vcard.Name(
      family      = row["First Name"],
      given       = row["Last Name"],
      additional  = row["Middle Name"],
      prefix      = row["Title"],
      suffix      = row["Suffix"],
      )

def add_notes (v, row):
  if are_null (row, ["Notes"]):
    return
  v.add ('note')
  v.note.value = row["Notes"]

def add_anniversary (v, row):
  if are_null (row, ["Birthday"]):
    return
  v.add ('bday')
  v.bday.value = row["Birthday"]

def add_title (v, row):
  v.add ('fn')
  v.fn.value = "%s %s" % (row["First Name"], row["Last Name"])

def add_organization (v, row):
  if are_null (row, ["Company", "Department", "Job Title"]):
    return
  v.add ('org')
  v.org.value = [row["Company"], row["Department"], row ["Job Title"]]

def add_address (v, row, pre, typ):
  if are_null (row, [pre+" Street", pre+" City"]):
    return
  v.add ('adr')
  v.adr.type_param = typ
  v.adr.value = vobject.vcard.Address(
      street =  row[pre+" Street"]   + "\n " +
                row[pre+" Street 2"] + "\n " +
                row[pre+" Street 3"],
      city =    row[pre+" City"],
      region =  row[pre+" State"],
      code =    row[pre+" Postal Code"],
      country = row[pre+" Country"],
      )

def add_telephone (v, row, pre, typ):
  if are_null (row, [pre+" Phone"]):
    return
  v.add('tel')
  v.tel.type_param = typ
  v.tel.value = row[pre+" Phone"]

def add_fax (v, row, pre, typ):
  if are_null (row, [pre+" Fax"]):
    return
  v.add('fax')
  v.fax.type_param = typ
  v.fax.value = row[pre+" Fax"]

def unfuck_emails (s):
  t= re.split ("[<>]", s)
  for field in t:
    if field.find ('@') > -1:
      return field.strip()
  # should raise ValueError("Badly splitted: " + s)
  return s

def add_email (v, row, suf):
    dn=  "E-mail" + suf + " Display Name"
    ema= "E-mail" + suf + " Address"
    emt= "E-mail" + suf + " Type"
    if row[ema] != "":
      v.add('email')
      v.email.value = "%s <%s>" % (row[dn], unfuck_emails (row[ema]))
      if row[emt] != "SMTP":
        v.email.type_param = row[emt]

with open('APRILContacts-unix-utf8.csv', 'rb') as csvfile:
  reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
  for row in reader:
    for key in row:
      if type (row[key]) is str:
        row[key]= row[key].decode('utf8')

    v= vobject.vCard()
    
    add_title (v, row)
    add_name  (v, row)
    add_notes (v, row)
    add_anniversary  (v, row)
    add_organization (v, row)

    for (i,o) in [("Business", "WORK"), ("Home", "HOME"), ("Other", "OTHER")]:
      add_address (v, row, i, o)
      add_telephone (v, row, i, o)
      add_telephone (v, row, i+" 2", o+" 2")
      add_fax (v, row, i, o)

    for (i,o) in [("Mobile", "CELL")]:
      add_telephone (v, row, i, o)

    for suf in ["", " 2", " 3"]:
      add_email (v, row, suf)

    print v.serialize()

Pour l'utiliser:

  • exporter les contacts depuis dotProjet en CSV ;
  • lancer le script : python dotp2vcard.py > contacts.vcard