summaryrefslogtreecommitdiffstats
path: root/idp/csv_to_ldif.py
blob: 87edbc7aa6984007e74fe0c5df891b2bb726e3ee (plain)
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
#!/usr/bin/python

import sys

collectivity_siret_ou_dict = {}

def collectivity_label_to_ou(name):
    return name.lower().replace(' ', '-').replace("'", "-")

def collectivities_csv_to_ldif(csv_filename, ldif_fields):
    csv_file = open(csv_filename)
    ldif_file = open(csv_filename.replace('.csv', '.ldif'), 'w')

    # Skip first line which only indicates field names
    csv_file.readline()
    for csv_line in csv_file.readlines():
        csv_fields = csv_line.strip().split(';')
        collectivity_ou = collectivity_label_to_ou(csv_fields[2])
        # Fill this dictionnary which will be used for parsing agents file
        collectivity_siret_ou_dict[csv_fields[0]] = collectivity_ou
        # Write collectivities ldif file
        ldif_file.write('dn: ou=%s, o=pratic,dc=entrouvert,dc=com\n' % collectivity_ou)
        ldif_file.write('objectClass: cdg59collectivity\n')
        ldif_file.write('ou: %s\n' % collectivity_ou)
        for i in range(0, len(ldif_fields)):
            if ldif_fields[i] is None and csv_fields[i]:
                print >> sys.stderr, 'Warning - undefined field : %s' % csv_fields[i]
            elif csv_fields[i]:
                ldif_file.write('%s: %s\n' % (ldif_fields[i], csv_fields[i]))
        ldif_file.write('\n')

    csv_file.close()
    ldif_file.close()

def agent_uid(gn, sn):
    return "%s.%s" % (gn.lower().replace(' ', '-'), sn.lower().replace(' ', '-'))

def agents_csv_to_ldif(csv_filename, collectivities_csv_filename, ldif_fields):
    csv_file = open(csv_filename)
    ldif_file = open(csv_filename.replace('.csv', '.ldif'), 'w')

    latest_line = ''
    # Skip first line which only indicates field names
    csv_file.readline()
    for csv_line in csv_file.readlines():
        csv_fields = csv_line.strip().split(';')
        siret = csv_fields[0]
        sn = csv_fields[2].capitalize()
        gn = csv_fields[3].capitalize()
        uid = agent_uid(gn, sn)
        dn = 'dn: uid=%s, ou=%s, o=pratic,dc=entrouvert,dc=com\n' % (uid, collectivity_siret_ou_dict[siret])
        if dn == latest_line:
            continue
        ldif_file.write(dn)
        latest_line = dn
        ldif_file.write('objectClass: cdg59agent\n')
        ldif_file.write('uid: %s\n' % uid)
        ldif_file.write('cn: %s %s\n' % (gn, sn))
        ldif_file.write('gn: %s\n' % gn)
        ldif_file.write('sn: %s\n' % sn)
        for i in range(0, len(ldif_fields)):
            if ldif_fields[i] and csv_fields[i]:
                ldif_file.write('%s: %s\n' % (ldif_fields[i], csv_fields[i]))
        ldif_file.write('\n')

    csv_file.close()
    ldif_file.close()


fields = ('cdg59siretCode', 'cdg59collectivitySirhCode', 'cdg59collectivitySirhLabel', 'cn', 'cdg59regionCode', 'cdg59departementCode', 'cdg59arrondissementCode', 'cdg59cantonCode', 'cdg59inseeCode', 'cdg59streetNumber', None, 'street', 'postalCode', 'cdg59distOffice', 'cdg59addressCompl', 'cdg59addressMention', None, 'telephoneNumber', 'facsimileTelephoneNumber', 'mail', 'cdg59URL')
collectivities_csv_to_ldif('Collectivites_CAPH.csv', fields)

fields = (None, 'cdg59agentSirhCode', None, None, 'employeeType')
agents_csv_to_ldif('Agents_CAPH.csv', 'Collectivites_CAPH.csv', fields)