diff options
-rw-r--r-- | pykolab/tests/create-addressbook-csv.py | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/pykolab/tests/create-addressbook-csv.py b/pykolab/tests/create-addressbook-csv.py new file mode 100644 index 0000000..09c3679 --- /dev/null +++ b/pykolab/tests/create-addressbook-csv.py @@ -0,0 +1,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 |