summaryrefslogtreecommitdiffstats
path: root/pykolab/tests/create-addressbook-csv.py
blob: 09c3679dfe05cee2de621f17a44b2c7276fb80ec (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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2010 Kolab Systems AG (http://www.kolabsys.com)
#
# Paul James Adams <adams a kolabsys.com>
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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; version 2 only
#
# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import os, random, sys

alphabet = "abcdefghijklmnopqrstuvwxwz"

COLUMNS = {
        "Formatted Name": 'required',
        "Honorific Prefixes": 'optional_10',
        "Given Name": 'required',
        "Additional Names": '',
        "Family Name": 'required',
        "Honorific Suffixes": 'optional_10',
        "Nick Name": '',
        "Birthday": '',
        "Anniversary": '',
        "EMail (preferred)": 'optional_email',
        "EMail (2)": 'optional_email_7',
        "EMail (3)": 'optional_email_21',
        "EMail (4)": 'optional_email_150',
        "Home Address Street": '',
        "Home Address Post Office Box": '',
        "Home Address City": '',
        "Home Address State": '',
        "Home Address Zip Code": '',
        "Home Address Country": '',
        "Home Address Label": '',
        "Business Address Street": '',
        "Business Address Post Office Box": '',
        "Business Address City": '',
        "Business Address State": '',
        "Business Address Zip Code": '',
        "Business Address Country": '',
        "Business Address Label": '',
        "Home Phone": '',
        "Business Phone": '',
        "Mobile Phone": '',
        "Home Fax": '',
        "Business Fax": '',
        "Car Phone": '',
        "ISDN": '',
        "Pager": '',
        "Mail Client": 'optional_3',
        "Title": '',
        "Role": '',
        "Organization": 'optional_3',
        "Note": '',
        "Homepage": ''
    }

ROW_FORMAT = """%(Formatted Name)s,%(Honorific Prefixes)s,%(Given Name)s,%(Additional Names)s,%(Family Name)s,%(Honorific Suffixes)s,%(Nick Name)s,%(Birthday)s,%(Anniversary)s,%(EMail (preferred))s,%(EMail (2))s,%(EMail (3))s,%(EMail (4))s,%(Home Address Street)s,%(Home Address Post Office Box)s,%(Home Address City)s,%(Home Address State)s,%(Home Address Zip Code)s,%(Home Address Country)s,%(Home Address Label)s,%(Business Address Street)s,%(Business Address Post Office Box)s,%(Business Address City)s,%(Business Address State)s,%(Business Address Zip Code)s,%(Business Address Country)s,%(Business Address Label)s,%(Home Phone)s,%(Business Phone)s,%(Mobile Phone)s,%(Home Fax)s,%(Business Fax)s,%(Car Phone)s,%(ISDN)s,%(Pager)s,%(Mail Client)s,%(Title)s,%(Role)s,%(Organization)s,%(Note)s,%(Homepage)s"""

def random_string(components=1, bitsize=[4, 8], delimiter=' ', tolower=False, nonascii=True):
    #print "getting components %d" % components
    #print "bitsize: %r" % bitsize
    bitsize.sort()
    #print "asd: %r" % random.randint(bitsize[0], bitsize[1])

    return_str = ""
    for component in range(0, components, 1):
        #print 'running for component %d' % component
        if component == 0:
            return_str = "%s" %(''.join(random.sample(alphabet, random.randint(bitsize[0], bitsize[1]))).capitalize())
        else:
            return_str = "%s%s%s" %(return_str,delimiter,''.join(random.sample(alphabet, random.randint(bitsize[0], bitsize[1]))).capitalize())

    if tolower:
        return_str = return_str.lower()

    return return_str

if __name__ == "__main__":
    wanted_num = int(sys.argv[1])

    key_num = 1
    for key in COLUMNS.keys():
        if key_num == 1:
            columns_line = '"%s"' % key
        elif key_num == len(COLUMNS.keys()):
            columns_line = '%s,"%s"' %(columns_line,key)
        else:
            columns_line = '%s,"%s"' %(columns_line,key)

        key_num += 1

    print ROW_FORMAT.replace('%(','"').replace(')s','"')

    num = 0
    while num < wanted_num:

        # Copy the dict over.
        row = {}

        # Iterate over the items in the row and find the required ones, then
        # set them! Otherwise, do something arbitrary.
        for item in COLUMNS.keys():
            if COLUMNS[item] == '':
                row[item] = '""'
                continue

            elif COLUMNS[item] == "required":
                    row[item] = '"%s"' %(random_string(components=random.randint(2,3),bitsize=list([random.randint(3,6), random.randint(4,8)])))
            elif COLUMNS[item] == "optional_3":
                if random.randint(1,3) == 3:
                    row[item] = '"%s"' %(random_string(components=random.randint(2,3),bitsize=list([random.randint(3,6), random.randint(4,8)])))
                else:
                    row[item] = '""'
            elif COLUMNS[item] == "optional_10":
                if random.randint(1,10) == 10:
                    row[item] = '"%s."' %(random_string(components=1,bitsize=list([2,2])))
                else:
                    row[item] = '""'
            elif COLUMNS[item].split('_')[1] == 'email':
                if COLUMNS[item] == "optional_email":
                    if random.randint(1,2) % 2 == 0:
                        row[item] = '"%s.%s"' %(
                                                    random_string(
                                                        components=2,
                                                        bitsize=list([random.randint(3,6), random.randint(4,8)]),
                                                        delimiter='@',
                                                        tolower=True,
                                                        nonascii=False),
                                                    list(["org","net","com"])[random.randint(0,2)]
                                                )
                    else:
                        row[item] = ''
                else:
                    rand = int(COLUMNS[item].split('_')[2])
                    if random.randint(1,rand) == rand:
                        row[item] = '"%s.%s"' %(
                                                    random_string(
                                                        components=2,
                                                        bitsize=list([random.randint(3,6), random.randint(4,8)]),
                                                        delimiter='@',
                                                        tolower=True,
                                                        nonascii=False),
                                                    list(["org","net","com"])[random.randint(0,2)]
                                                )
                    else:
                        row[item] = '""'
            else:
                row[item] = '""'

        print ROW_FORMAT % row

        num += 1